a_star: don't crash on deserialization errors and limit static types
[nit.git] / src / test_model_index.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import frontend
16 import model_index
17 import console
18
19 redef class ToolContext
20 var opt_name_prefix = new OptionBool("", "--name-prefix")
21 var opt_full_name_prefix = new OptionBool("", "--full-name-prefix")
22 var opt_name_similarity = new OptionBool("", "--name-similarity")
23 var opt_full_name_similarity = new OptionBool("", "--full-name-similarity")
24 var opt_name = new OptionBool("", "--name")
25 var opt_full_name = new OptionBool("", "--full-name")
26
27 redef init do
28 option_context.add_option(opt_name_prefix, opt_full_name_prefix)
29 option_context.add_option(opt_name_similarity, opt_full_name_similarity)
30 option_context.add_option(opt_name, opt_full_name)
31 end
32 end
33
34 redef class MEntity
35 fun color: String do
36 if visibility == public_visibility then
37 return name.green
38 else if visibility == protected_visibility then
39 return name.yellow
40 else
41 return name.red
42 end
43 end
44 end
45
46 # build toolcontext
47 var toolcontext = new ToolContext
48 toolcontext.process_options(args)
49 var args = toolcontext.option_context.rest
50
51 if not args.length == 2 then
52 print "usage: test_model_index <nitfile> <search_query>"
53 exit 1
54 end
55
56 # build model
57 var model = new Model
58 var mbuilder = new ModelBuilder(model, toolcontext)
59 var mmodules = mbuilder.parse_full([args.first])
60
61 # process
62 if mmodules.is_empty then return
63 mbuilder.run_phases
64 toolcontext.run_global_phases(mmodules)
65
66 # Build index
67 var index = new ModelIndex
68 for mentity in model.private_view.mentities do
69 if mentity isa MClassDef or mentity isa MPropDef then continue
70 index.index(mentity)
71 end
72
73 var q = args[1]
74
75 print "# {q}\n"
76
77 var res
78 if toolcontext.opt_name_prefix.value then
79 res = index.find_by_name_prefix(q)
80 else if toolcontext.opt_full_name_prefix.value then
81 res = index.find_by_full_name_prefix(q)
82 else if toolcontext.opt_name_similarity.value then
83 res = index.find_by_name_similarity(q)
84 else if toolcontext.opt_full_name_similarity.value then
85 res = index.find_by_full_name_similarity(q)
86 else if toolcontext.opt_name.value then
87 res = index.find_by_name(q)
88 else if toolcontext.opt_full_name.value then
89 res = index.find_by_full_name(q)
90 else
91 res = index.find(q)
92 end
93
94 res = res.sort(new ScoreComparator, new MEntityComparator).
95 uniq.
96 limit(10).
97 sort(new VisibilityComparator, new NameComparator)
98
99 for e in res do
100 if toolcontext.opt_no_color.value then
101 print " * {e.score}: {e.mentity.name} ({e.mentity.full_name})"
102 else
103 print " * {e.score}: {e.mentity.color} ({e.mentity.full_name})"
104 end
105 end