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