nitx: redo search for MType in new calls
[nit.git] / src / nitx.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 # `nitx`, is a command tool that displays useful informations about the code.
16 #
17 # Features:
18 #
19 # * Display comment from name/namespace
20 # * Display documentation page from Nitdoc in console
21 # * Find type usage in parameters, returns and news.
22 module nitx
23
24 import modelbuilder
25 import doc::doc_phases::doc_console
26
27 redef class ToolContext
28
29 # Nittx generation phase.
30 var docx: Phase = new NitxPhase(self, null)
31
32 # Used to shortcut the prompt and display directly the result in console.
33 var opt_query = new OptionString("Nitx query to perform", "-q", "--query")
34
35 init do option_context.add_option opt_query
36 end
37
38 # Nitx phase explores the model and prepares the console rendering.
39 private class NitxPhase
40 super Phase
41 redef fun process_mainmodule(mainmodule, mmodules)
42 do
43 var doc = new DocModel(mainmodule.model, mainmodule)
44
45 var phases = [
46 new ExtractionPhase(toolcontext, doc),
47 new MakePagePhase(toolcontext, doc),
48 new ConcernsPhase(toolcontext, doc),
49 new StructurePhase(toolcontext, doc): DocPhase]
50
51 for phase in phases do
52 toolcontext.info("# {phase.class_name}", 1)
53 phase.apply
54 end
55
56 # start nitx
57 var nitx = new Nitx(toolcontext, doc)
58 var q = toolcontext.opt_query.value
59 if q != null then # shortcut prompt
60 print ""
61 nitx.do_query(q)
62 return
63 end
64 nitx.start
65 end
66 end
67
68 # build toolcontext
69 var toolcontext = new ToolContext
70 var tpl = new Template
71 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
72 tpl.add "Displays specific pieces of API information from Nit source files."
73 toolcontext.tooldescription = tpl.write_to_string
74
75 # process options
76 toolcontext.process_options(args)
77 var arguments = toolcontext.option_context.rest
78
79 # build model
80 var model = new Model
81 var mbuilder = new ModelBuilder(model, toolcontext)
82 var mmodules = mbuilder.parse_full(arguments)
83
84 # process
85 if mmodules.is_empty then return
86 mbuilder.run_phases
87 toolcontext.run_global_phases(mmodules)