nitx: exit on EOF
[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`, a command tool that displays useful data about Nit code
16 #
17 # Features:
18 #
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
24 module nitx
25
26 import frontend
27 import doc::term
28 import readline
29
30 redef class ToolContext
31
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")
34
35 # Compute and use the catalog
36 var opt_catalog = new OptionBool("Use catalog", "--catalog")
37
38 init do option_context.add_option(opt_command, opt_catalog)
39 end
40
41 # Nitx handles console queries
42 #
43 # Using `prompt`, the command line can be turned on an interactive tool.
44 class Nitx
45
46 # ModelView that contains the informations to display
47 var view: ModelView
48
49 # ModelBuilder to access AST nodes
50 var modelbuilder: ModelBuilder
51
52 # Catalog if any
53 var catalog: nullable Catalog = null is optional
54
55 # Do not use colors in the output
56 var no_color = false is optional
57
58 # Displays the welcome message and start prompt.
59 fun start do
60 welcome
61 prompt
62 end
63
64 # Displays the welcome message and the list of loaded modules.
65 fun welcome do
66 print "Welcome in the Nit Index!"
67 print ""
68 print "Loaded packages:\n"
69 var cmd = new CmdModelEntities(view, kind = "packages")
70 cmd.init_command
71 for mpackage in cmd.results.as(not null) do
72 print " * {mpackage.full_name}"
73 end
74 help
75 end
76
77 # Displays the list of available queries.
78 fun help do
79 # TODO automatize that
80 print "\nCommands:\n"
81 for usage, doc in parser.commands_usage do
82 var l = usage.length / 8
83 print "\t{usage}{"\t" * (3 - l)}{doc}"
84 end
85 print "\n"
86 print "\t:h\t\t\tdisplay this help message"
87 print "\t:q\t\t\tquit interactive mode"
88 print ""
89 end
90
91 # Prompts the user for a query.
92 fun prompt do
93 var line = readline(">> ", true)
94 if line != null then
95 do_command(line)
96 else
97 # EOF
98 exit 0
99 end
100 prompt
101 end
102
103 # Parser used to process doc commands
104 var parser = new CommandParser(view, modelbuilder, catalog) is lazy
105
106 # Processes the query string and performs it.
107 fun do_command(str: String) do
108 if str == ":q" then
109 exit 0
110 else if str == ":h" then
111 help
112 return
113 end
114 parser.execute(str, no_color)
115 end
116 end
117
118 redef class Catalog
119 # Build the catalog for Nitx
120 private fun build_catalog(view: ModelView) do
121 # Compute the poset
122 for p in view.mpackages do
123 var g = p.root
124 assert g != null
125 modelbuilder.scan_group(g)
126
127 deps.add_node(p)
128 for gg in p.mgroups do for m in gg.mmodules do
129 for im in m.in_importation.direct_greaters do
130 var ip = im.mpackage
131 if ip == null or ip == p then continue
132 deps.add_edge(p, ip)
133 end
134 end
135 end
136 # Build the catalog
137 for mpackage in view.mpackages do
138 package_page(mpackage)
139 git_info(mpackage)
140 mpackage_stats(mpackage)
141 end
142 end
143 end
144
145 # build toolcontext
146 var toolcontext = new ToolContext
147 var tpl = new Template
148 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
149 tpl.add "Displays pieces of API information from Nit source files."
150 toolcontext.tooldescription = tpl.write_to_string
151
152 # process options
153 toolcontext.process_options(args)
154 var arguments = toolcontext.option_context.rest
155
156 # build model
157 var model = new Model
158 var mbuilder = new ModelBuilder(model, toolcontext)
159 var mmodules = mbuilder.parse_full(arguments)
160
161 # process
162 if mmodules.is_empty then return
163 mbuilder.run_phases
164 toolcontext.run_global_phases(mmodules)
165 var mainmodule = toolcontext.make_main_module(mmodules)
166
167 # build views
168 var view = new ModelView(model, mainmodule)
169 var catalog = null
170 if toolcontext.opt_catalog.value then
171 catalog = new Catalog(mbuilder)
172 catalog.build_catalog(view)
173 end
174
175 # start nitx
176 var nitx = new Nitx(view, mbuilder, catalog, toolcontext.opt_no_color.value)
177 var q = toolcontext.opt_command.value
178 if q != null then # shortcut prompt
179 print ""
180 nitx.do_command(q)
181 return
182 end
183 nitx.start