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.
15 # `nitx`, a command tool that displays useful data about Nit code
19 # * Display documentation from name/namespace
20 # * Find type usage in parameters, returns and news.
21 # * Find usage (calls) of a specific property.
22 # * Find source code related to class/property by its name.
23 # * Display inheritance lists
30 redef class ToolContext
32 # Used to shortcut the prompt and display directly the result in console.
33 var opt_command
= new OptionString("Nitx command to perform", "-c", "--command")
35 # Compute and use the catalog
36 var opt_catalog
= new OptionBool("Use catalog", "--catalog")
38 init do option_context
.add_option
(opt_command
, opt_catalog
)
41 # Nitx handles console queries
43 # Using `prompt`, the command line can be turned on an interactive tool.
46 # ModelView that contains the informations to display
49 # ModelBuilder to access AST nodes
50 var modelbuilder
: ModelBuilder
53 var catalog
: nullable Catalog = null is optional
55 # Do not use colors in the output
56 var no_color
= false is optional
58 # Displays the welcome message and start prompt.
64 # Displays the welcome message and the list of loaded modules.
66 print
"Welcome in the Nit Index!"
68 print
"Loaded packages:\n"
69 var cmd
= new CmdModelEntities(view
, kind
= "packages")
71 for mpackage
in cmd
.results
.as(not null) do
72 print
" * {mpackage.full_name}"
77 # Displays the list of available queries.
79 # TODO automatize that
81 for usage
, doc
in parser
.commands_usage
do
82 var l
= usage
.length
/ 8
83 print
"\t{usage}{"\t" * (3 - l)}{doc}"
86 print
"\t:h\t\t\tdisplay this help message"
87 print
"\t:q\t\t\tquit interactive mode"
91 # Prompts the user for a query.
93 var line
= readline
(">> ", true)
100 # Parser used to process doc commands
101 var parser
= new CommandParser(view
, modelbuilder
, catalog
) is lazy
103 # Processes the query string and performs it.
104 fun do_command
(str
: String) do
107 else if str
== ":h" then
111 parser
.execute
(str
, no_color
)
116 # Build the catalog for Nitx
117 private fun build_catalog
(view
: ModelView) do
119 for p
in view
.mpackages
do
122 modelbuilder
.scan_group
(g
)
125 for gg
in p
.mgroups
do for m
in gg
.mmodules
do
126 for im
in m
.in_importation
.direct_greaters
do
128 if ip
== null or ip
== p
then continue
134 for mpackage
in view
.mpackages
do
135 package_page
(mpackage
)
137 mpackage_stats
(mpackage
)
143 var toolcontext
= new ToolContext
144 var tpl
= new Template
145 tpl
.add
"Usage: nitx [OPTION]... <file.nit>... [query]\n"
146 tpl
.add
"Displays pieces of API information from Nit source files."
147 toolcontext
.tooldescription
= tpl
.write_to_string
150 toolcontext
.process_options
(args
)
151 var arguments
= toolcontext
.option_context
.rest
154 var model
= new Model
155 var mbuilder
= new ModelBuilder(model
, toolcontext
)
156 var mmodules
= mbuilder
.parse_full
(arguments
)
159 if mmodules
.is_empty
then return
161 toolcontext
.run_global_phases
(mmodules
)
162 var mainmodule
= toolcontext
.make_main_module
(mmodules
)
165 var view
= new ModelView(model
, mainmodule
)
167 if toolcontext
.opt_catalog
.value
then
168 catalog
= new Catalog(mbuilder
)
169 catalog
.build_catalog
(view
)
173 var nitx
= new Nitx(view
, mbuilder
, catalog
, toolcontext
.opt_no_color
.value
)
174 var q
= toolcontext
.opt_command
.value
175 if q
!= null then # shortcut prompt