ni: now display FT and VT on class doc
[nit.git] / src / ni.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module ni
18
19 import model_utils
20
21 private class Pager
22 var content: String = ""
23 fun add(text: String) do addn("{text}\n")
24 fun addn(text: String) do content += text.escape
25 fun add_rule do add("\n---\n")
26 fun render do sys.system("echo \"{content}\" | pager -r")
27 end
28
29 class NitIndex
30 private var toolcontext: ToolContext
31 private var model: Model
32 private var mbuilder: ModelBuilder
33 private var mainmodule: MModule
34 private var arguments: Array[String]
35
36 init(toolcontext: ToolContext) do
37 # We need a model to collect stufs
38 self.toolcontext = toolcontext
39 self.arguments = toolcontext.option_context.rest
40
41 if arguments.length > 2 then
42 print "usage: ni path/to/module.nit [expression]"
43 toolcontext.option_context.usage
44 exit(1)
45 end
46
47 model = new Model
48 mbuilder = new ModelBuilder(model, toolcontext)
49
50 # Here we load an process std modules
51 #var dir = "NIT_DIR".environ
52 #var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"])
53 var mmodules = mbuilder.parse_and_build([arguments.first])
54 if mmodules.is_empty then return
55 mbuilder.full_propdef_semantic_analysis
56 assert mmodules.length == 1
57 self.mainmodule = mmodules.first
58 end
59
60 fun start do
61 if arguments.length == 1 then
62 welcome
63 prompt
64 else
65 seek(arguments[1])
66 end
67 end
68
69 fun welcome do
70 print "Welcome in Nit Index.\n"
71 print "Loaded modules"
72 for m in mbuilder.nmodules do
73 print " - {m.mmodule.name}"
74 end
75 print "\nEnter the module, class or property name you want to look up."
76 print "Enter a blank line to exit.\n"
77 end
78
79 fun prompt do
80 printn ">> "
81 seek(stdin.read_line)
82 end
83
84 fun seek(entry: String) do
85 # quitting?
86 if entry.is_empty then exit(0)
87
88 var flag = false
89 for n in mbuilder.nmodules do
90 var m = n.mmodule
91 if m.name == entry then
92 flag = true
93 module_fulldoc(n)
94 end
95 end
96 for c in model.mclasses do
97 if c.name == entry then
98 if not mbuilder.mclassdef2nclassdef[c.intro] isa AStdClassdef then continue
99 flag = true
100 doc_class(c)
101 end
102 end
103 var matches = new List[MProperty]
104 for p in model.mproperties do
105 if p.name == entry then
106 flag = true
107 matches.add(p)
108 end
109 end
110 #if not matches.is_empty then doc_properties
111
112 if not flag then print "Nothing known about '{entry}'"
113 if arguments.length == 1 then prompt
114 end
115
116 private fun module_fulldoc(nmodule: AModule) do
117 var mmodule = nmodule.mmodule
118 var pager = new Pager
119 pager.add("# module {mmodule.name}\n".bold)
120 pager.add("import {mmodule.in_importation.direct_greaters.join(", ")}")
121 pager.add_rule
122 pager.addn(nmodule.comment.green)
123 pager.add_rule
124
125 var cats = new HashMap[String, Collection[MClass]]
126 cats["introduced classes"] = mmodule.intro_mclasses
127 cats["refined classes"] = mmodule.redef_mclasses
128 cats["inherited classes"] = mmodule.imported_mclasses
129
130 for cat, list in cats do
131 if not list.is_empty then
132 pager.add("# {cat}\n".bold)
133 for mclass in list do
134 var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
135 if not nclass.short_comment.is_empty then
136 pager.add("\t# {nclass.short_comment}")
137 end
138 if cat == "refined classes" then
139 pager.add("\tredef {mclass.short_doc}")
140 else
141 pager.add("\t{mclass.short_doc}")
142 end
143 if not mclass.intro_mmodule == mmodule then
144 pager.add("\t\tintroduced in {mmodule.full_name}::{mclass}".gray)
145 end
146 pager.add("")
147 end
148 end
149 end
150 pager.add_rule
151 pager.render
152 end
153
154 fun doc_class(mclass: MClass) do
155 var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
156 var pager = new Pager
157 pager.add("# {mclass.intro_mmodule.public_owner.name}::{mclass.name}\n".bold)
158 pager.add("{mclass.short_doc} ")
159 pager.add_rule
160 pager.addn(nclass.comment.green)
161 pager.add_rule
162 if not mclass.parameter_types.is_empty then
163 pager.add("# formal types\n".bold)
164 for ft, bound in mclass.parameter_types do
165 pager.add("\t{ft.to_s.green}: {bound}")
166 pager.add("")
167 end
168 end
169 if not mclass.virtual_types.is_empty then
170 pager.add("# virtual types\n".bold)
171 for vt in mclass.virtual_types do
172 if vt.visibility.to_s == "public" then pager.add("\t{vt.to_s.green}: {vt.intro.bound.to_s}")
173 if vt.visibility.to_s == "private" then pager.add("\t{vt.to_s.red}: {vt.intro.bound.to_s}")
174 if vt.visibility.to_s == "protected" then pager.add("\t{vt.to_s.yellow}: {vt.intro.bound.to_s}")
175 if vt.intro_mclassdef != mclass.intro then
176 pager.add("\t\tintroduced in {vt.intro_mclassdef.namespace}::{vt}".gray)
177 end
178 pager.add("")
179 end
180 end
181 pager.add_rule
182
183 var cats = new HashMap[String, Collection[MMethod]]
184 cats["constructors"] = mclass.constructors
185 cats["introduced methods"] = mclass.intro_methods
186 cats["refined methods"] = mclass.redef_methods
187 cats["inherited methods"] = mclass.inherited_methods
188
189 for cat, list in cats do
190 if not list.is_empty then
191 pager.add("# {cat}\n".bold)
192 for mmethod in list do
193 #TODO verifier cast
194 var nmethod = mbuilder.mpropdef2npropdef[mmethod.intro].as(AMethPropdef)
195 if not nmethod.short_comment.is_empty then
196 pager.add("\t# {nmethod.short_comment}")
197 end
198 if cat == "refined methods" then
199 pager.add("\tredef {nmethod}")
200 else
201 pager.add("\t{nmethod}")
202 end
203 if not mmethod.intro_mclassdef == mclass.intro then
204 pager.add("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray)
205 end
206 pager.add("")
207 end
208 end
209 end
210 pager.render
211 end
212 end
213
214 # Printing facilities
215
216 redef class MClass
217
218 redef fun to_s: String do
219 if arity > 0 then
220 return "{name}[{intro.parameter_names.join(", ")}]"
221 else
222 return name
223 end
224 end
225
226 private fun short_doc: String do
227 var ret = ""
228 if is_interface then ret = "interface {ret}"
229 if is_enum then ret = "enum {ret}"
230 if is_class then ret = "class {ret}"
231 if is_abstract then ret = "abstract {ret}"
232 if visibility.to_s == "public" then ret = "{ret}{to_s.green}"
233 if visibility.to_s == "private" then ret = "{ret}{to_s.red}"
234 if visibility.to_s == "protected" then ret = "{ret}{to_s.yellow}"
235 ret = "{ret} super {parents.join(", ")}"
236 return ret
237 end
238 end
239
240 redef class MClassDef
241 private fun namespace: String do
242 return "{mmodule.full_name}::{mclass.name}"
243 end
244 end
245
246 redef class AModule
247 private fun comment: String do
248 var ret = ""
249 for t in n_moduledecl.n_doc.n_comment do
250 ret += "{t.text.replace("# ", "")}"
251 end
252 return ret
253 end
254 end
255
256 redef class AStdClassdef
257 private fun comment: String do
258 var ret = ""
259 if n_doc != null then
260 for t in n_doc.n_comment do
261 var txt = t.text.replace("# ", "")
262 txt = txt.replace("#", "")
263 ret += "{txt}"
264 end
265 end
266 return ret
267 end
268
269 private fun short_comment: String do
270 var ret = ""
271 if n_doc != null then
272 var txt = n_doc.n_comment.first.text
273 txt = txt.replace("# ", "")
274 txt = txt.replace("\n", "")
275 ret += txt
276 end
277 return ret
278 end
279 end
280
281 redef class AMethPropdef
282 private fun short_comment: String do
283 var ret = ""
284 if n_doc != null then
285 var txt = n_doc.n_comment.first.text
286 txt = txt.replace("# ", "")
287 txt = txt.replace("\n", "")
288 ret += txt
289 end
290 return ret
291 end
292
293 redef fun to_s do
294 var ret = ""
295 if not mpropdef.mproperty.is_init then
296 ret = "fun "
297 end
298 if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{mpropdef.mproperty.name.green}"
299 if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{mpropdef.mproperty.name.red}"
300 if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{mpropdef.mproperty.name.yellow}"
301 if n_signature != null then ret = "{ret}{n_signature.to_s}"
302 if n_kwredef != null then ret = "redef {ret}"
303 if self isa ADeferredMethPropdef then ret = "{ret} is abstract"
304 if self isa AInternMethPropdef then ret = "{ret} is intern"
305 if self isa AExternMethPropdef then ret = "{ret} is extern"
306 return ret
307 end
308 end
309
310 redef class ASignature
311 redef fun to_s do
312 #TODO closures
313 var ret = ""
314 if not n_params.is_empty then
315 ret = "{ret}({n_params.join(", ")})"
316 end
317 if n_type != null then ret += ": {n_type.to_s}"
318 return ret
319 end
320 end
321
322 redef class AParam
323 redef fun to_s do
324 var ret = "{n_id.text}"
325 if n_type != null then
326 ret = "{ret}: {n_type.to_s}"
327 if n_dotdotdot != null then ret = "{ret}..."
328 end
329 return ret
330 end
331 end
332
333 redef class AType
334 redef fun to_s do
335 var ret = n_id.text
336 if n_kwnullable != null then ret = "nullable {ret}"
337 if not n_types.is_empty then ret = "{ret}[{n_types.join(", ")}]"
338 return ret
339 end
340 end
341
342 # Redef String class to add a function to color the string
343 redef class String
344
345 private fun add_escape_char(escapechar: String): String do
346 return "{escapechar}{self}\\033[0m"
347 end
348
349 private fun esc: Char do return 27.ascii
350 private fun red: String do return add_escape_char("{esc}[1;31m")
351 private fun yellow: String do return add_escape_char("{esc}[1;33m")
352 private fun green: String do return add_escape_char("{esc}[1;32m")
353 private fun blue: String do return add_escape_char("{esc}[1;34m")
354 private fun cyan: String do return add_escape_char("{esc}[1;36m")
355 private fun gray: String do return add_escape_char("{esc}[30;1m")
356 private fun bold: String do return add_escape_char("{esc}[1m")
357 private fun underline: String do return add_escape_char("{esc}[4m")
358
359 private fun escape: String
360 do
361 var b = new Buffer
362 for c in self do
363 if c == '\n' then
364 b.append("\\n")
365 else if c == '\0' then
366 b.append("\\0")
367 else if c == '"' then
368 b.append("\\\"")
369 else if c == '\\' then
370 b.append("\\\\")
371 else if c == '`' then
372 b.append("'")
373 else if c.ascii < 32 then
374 b.append("\\{c.ascii.to_base(8, false)}")
375 else
376 b.add(c)
377 end
378 end
379 return b.to_s
380 end
381 end
382
383 # Create a tool context to handle options and paths
384 var toolcontext = new ToolContext
385 toolcontext.process_options
386
387 # Here we launch the nit index
388 var ni = new NitIndex(toolcontext)
389 ni.start
390
391 # TODO seek methods
392 # TODO lister les methods qui retournent un certain type
393 # TODO lister les methods qui utilisent un certain type
394 # TODO lister les sous-types connus d'une type
395 # TODO sorter par ordre alphabétique