1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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")
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
)
38 if visibility
== public_visibility
then
40 else if visibility
== protected_visibility
then
49 var toolcontext
= new ToolContext
50 toolcontext
.process_options
(args
)
51 var args
= toolcontext
.option_context
.rest
54 print
"usage: test_model_index nitfiles..."
61 var mbuilder
= new ModelBuilder(model
, toolcontext
)
62 var mmodules
= mbuilder
.parse_full
(args
)
65 if mmodules
.is_empty
then return
67 toolcontext
.run_global_phases
(mmodules
)
70 var filters
= new ModelFilter(
72 accept_fictive
= false,
74 var index
= new ModelIndex
75 for mentity
in model
.collect_mentities
(filters
) do
76 if mentity
isa MClassDef or mentity
isa MPropDef then continue
80 var query
= toolcontext
.opt_query
.value
82 # TODO add interactive mode
83 print
"usage: test_model_index nitfiles... -q query"
91 if toolcontext
.opt_name_prefix
.value
then
92 res
= index
.find_by_name_prefix
(query
)
93 else if toolcontext
.opt_full_name_prefix
.value
then
94 res
= index
.find_by_full_name_prefix
(query
)
95 else if toolcontext
.opt_name_similarity
.value
then
96 res
= index
.find_by_name_similarity
(query
)
97 else if toolcontext
.opt_full_name_similarity
.value
then
98 res
= index
.find_by_full_name_similarity
(query
)
99 else if toolcontext
.opt_name
.value
then
100 res
= index
.find_by_name
(query
)
101 else if toolcontext
.opt_full_name
.value
then
102 res
= index
.find_by_full_name
(query
)
104 res
= index
.find
(query
)
107 res
= res
.sort
(new ScoreComparator, new MEntityComparator).
110 sort
(new VisibilityComparator, new NameComparator)
113 if toolcontext
.opt_no_color
.value
then
114 print
" * {e.score}: {e.mentity.name} ({e.mentity.full_name})"
116 print
" * {e.score}: {e.mentity.color} ({e.mentity.full_name})"