src/test_model_index: add interactive mode
[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_query = new OptionString("String to search", "-q", "--query")
21 var opt_name_prefix = new OptionBool("", "--name-prefix")
22 var opt_full_name_prefix = new OptionBool("", "--full-name-prefix")
23 var opt_name_similarity = new OptionBool("", "--name-similarity")
24 var opt_full_name_similarity = new OptionBool("", "--full-name-similarity")
25 var opt_name = new OptionBool("", "--name")
26 var opt_full_name = new OptionBool("", "--full-name")
27
28 redef init do
29 option_context.add_option(opt_query)
30 option_context.add_option(opt_name_prefix, opt_full_name_prefix)
31 option_context.add_option(opt_name_similarity, opt_full_name_similarity)
32 option_context.add_option(opt_name, opt_full_name)
33 end
34 end
35
36 redef class MEntity
37 fun color: String do
38 if visibility == public_visibility then
39 return name.green
40 else if visibility == protected_visibility then
41 return name.yellow
42 else
43 return name.red
44 end
45 end
46 end
47
48 fun search(index: ModelIndex, toolcontext: ToolContext, query: String) do
49 print "# {query}\n"
50
51 var res
52 if toolcontext.opt_name_prefix.value then
53 res = index.find_by_name_prefix(query)
54 else if toolcontext.opt_full_name_prefix.value then
55 res = index.find_by_full_name_prefix(query)
56 else if toolcontext.opt_name_similarity.value then
57 res = index.find_by_name_similarity(query)
58 else if toolcontext.opt_full_name_similarity.value then
59 res = index.find_by_full_name_similarity(query)
60 else if toolcontext.opt_name.value then
61 res = index.find_by_name(query)
62 else if toolcontext.opt_full_name.value then
63 res = index.find_by_full_name(query)
64 else
65 res = index.find(query)
66 end
67
68 res = res.sort(new ScoreComparator, new MEntityComparator).
69 uniq.
70 limit(10).
71 sort(new VisibilityComparator, new NameComparator)
72
73 for e in res do
74 if toolcontext.opt_no_color.value then
75 print " * {e.score}: {e.mentity.name} ({e.mentity.full_name})"
76 else
77 print " * {e.score}: {e.mentity.color} ({e.mentity.full_name})"
78 end
79 end
80 end
81
82 # build toolcontext
83 var toolcontext = new ToolContext
84 toolcontext.process_options(args)
85 var args = toolcontext.option_context.rest
86
87 if args.is_empty then
88 print "usage: test_model_index nitfiles..."
89 exit 1
90 return
91 end
92
93 # build model
94 var model = new Model
95 var mbuilder = new ModelBuilder(model, toolcontext)
96 var mmodules = mbuilder.parse_full(args)
97
98 # process
99 if mmodules.is_empty then return
100 mbuilder.run_phases
101 toolcontext.run_global_phases(mmodules)
102
103 # Build index
104 var filters = new ModelFilter(
105 private_visibility,
106 accept_fictive = false,
107 accept_test = false)
108 var index = new ModelIndex
109 for mentity in model.collect_mentities(filters) do
110 if mentity isa MClassDef or mentity isa MPropDef then continue
111 index.index(mentity)
112 end
113
114 var query = toolcontext.opt_query.value
115 if query == null then
116 print "# Interactive mode, type `:q` to quit\n\n"
117 printn "> "
118 var line = stdin.read_line
119 while line != ":q" do
120 print ""
121 search(index, toolcontext, line.trim)
122 print ""
123 printn "> "
124 line = stdin.read_line
125 end
126 return
127 end
128
129 search(index, toolcontext, query)