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_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")
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
)
36 if visibility
== public_visibility
then
38 else if visibility
== protected_visibility
then
47 var toolcontext
= new ToolContext
48 toolcontext
.process_options
(args
)
49 var args
= toolcontext
.option_context
.rest
51 if not args
.length
== 2 then
52 print
"usage: test_model_index <nitfile> <search_query>"
58 var mbuilder
= new ModelBuilder(model
, toolcontext
)
59 var mmodules
= mbuilder
.parse_full
([args
.first
])
62 if mmodules
.is_empty
then return
64 toolcontext
.run_global_phases
(mmodules
)
65 var mainmodule
= toolcontext
.make_main_module
(mmodules
)
68 var filters
= new ModelFilter(
70 accept_fictive
= false,
72 var view
= new ModelView(model
, mainmodule
, filters
)
73 var index
= new ModelIndex
74 for mentity
in view
.mentities
do
75 if mentity
isa MClassDef or mentity
isa MPropDef then continue
84 if toolcontext
.opt_name_prefix
.value
then
85 res
= index
.find_by_name_prefix
(q
)
86 else if toolcontext
.opt_full_name_prefix
.value
then
87 res
= index
.find_by_full_name_prefix
(q
)
88 else if toolcontext
.opt_name_similarity
.value
then
89 res
= index
.find_by_name_similarity
(q
)
90 else if toolcontext
.opt_full_name_similarity
.value
then
91 res
= index
.find_by_full_name_similarity
(q
)
92 else if toolcontext
.opt_name
.value
then
93 res
= index
.find_by_name
(q
)
94 else if toolcontext
.opt_full_name
.value
then
95 res
= index
.find_by_full_name
(q
)
100 res
= res
.sort
(new ScoreComparator, new MEntityComparator).
103 sort
(new VisibilityComparator, new NameComparator)
106 if toolcontext
.opt_no_color
.value
then
107 print
" * {e.score}: {e.mentity.name} ({e.mentity.full_name})"
109 print
" * {e.score}: {e.mentity.color} ({e.mentity.full_name})"