aad0b027f0f7a8e1006140f1e514740cf1dd84db
[nit.git] / src / ni.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 # ni or nit index, is a command tool used to display documentation
16 module ni
17
18 import model_utils
19
20 private class Pager
21 var content = new Buffer
22 fun add(text: String) do addn("{text}\n")
23 fun addn(text: String) do content.append(text.escape)
24 fun add_rule do add("\n---\n")
25 fun render do sys.system("echo \"{content}\" | pager -r")
26 end
27
28 # Main class of the nit index tool
29 # NitIndex build the model using the toolcontext argument
30 # then wait for query on std in to display documentation
31 class NitIndex
32 private var toolcontext: ToolContext
33 private var model: Model
34 private var mbuilder: ModelBuilder
35 private var mainmodule: MModule
36 private var arguments: Array[String]
37
38 init(toolcontext: ToolContext) do
39 # We need a model to collect stufs
40 self.toolcontext = toolcontext
41 self.toolcontext.option_context.options.clear
42 self.arguments = toolcontext.option_context.rest
43
44 if arguments.is_empty or arguments.length > 2 then
45 print "usage: ni path/to/module.nit [expression]"
46 toolcontext.option_context.usage
47 exit(1)
48 end
49
50 model = new Model
51 mbuilder = new ModelBuilder(model, toolcontext)
52
53 # Here we load an process std modules
54 #var dir = "NIT_DIR".environ
55 #var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"])
56 var mmodules = mbuilder.parse_and_build([arguments.first])
57 if mmodules.is_empty then return
58 assert mmodules.length == 1
59 self.mainmodule = mmodules.first
60 end
61
62 fun start do
63 if arguments.length == 1 then
64 welcome
65 prompt
66 else
67 seek(arguments[1])
68 end
69 end
70
71 fun welcome do
72 print "Welcome in Nit Index.\n"
73 print "Loaded modules"
74 for m in mbuilder.nmodules do
75 print " - {m.mmodule.name}"
76 end
77 print "\nEnter the module, class or property name you want to look up."
78 print "Enter a blank line to exit.\n"
79 end
80
81 fun prompt do
82 printn ">> "
83 seek(stdin.read_line)
84 end
85
86 fun seek(entry: String) do
87 if entry.is_empty then exit(0)
88 var flag = false
89 # seek return types
90 if entry.has_prefix("return:") then
91 var ret = entry.split_with(":")[1].replace(" ", "")
92 var matches = seek_returns(ret)
93 if not matches.is_empty then
94 flag = true
95 props_fulldoc(matches)
96 end
97 else if entry.has_prefix("param:") then
98 var param = entry.split_with(":")[1].replace(" ", "")
99 var matches = seek_params(param)
100 if not matches.is_empty then
101 flag = true
102 props_fulldoc(matches)
103 end
104 else
105 # seek for modules
106 var mmatches = new List[MModule]
107 for m in model.mmodules do
108 if m.name == entry then
109 flag = true
110 mmatches.add(m)
111 end
112 end
113 if not mmatches.is_empty then modules_fulldoc(mmatches)
114 # seek for classes
115 var cmatches = new List[MClass]
116 for c in model.mclasses do
117 if c.name == entry then
118 flag = true
119 cmatches.add(c)
120 end
121 end
122 if not cmatches.is_empty then classes_fulldoc(cmatches)
123 # seek for properties
124 var matches = new List[MProperty]
125 for p in model.mproperties do
126 if p.name == entry then
127 flag = true
128 matches.add(p)
129 end
130 end
131 if not matches.is_empty then props_fulldoc(matches)
132 end
133 # no matches
134 if not flag then print "Nothing known about '{entry}'"
135 if arguments.length == 1 then prompt
136 end
137
138 private fun modules_fulldoc(mmodules: List[MModule]) do
139 var pager = new Pager
140 for mmodule in mmodules do
141 var nmodule = mbuilder.mmodule2nmodule[mmodule]
142 pager.add("# module {mmodule.namespace}\n".bold)
143 if not mmodule.in_importation.direct_greaters.is_empty then
144 pager.add("import ".bold + "{mmodule.in_importation.direct_greaters.join(", ")}\n")
145 end
146 if not mmodule.in_importation.direct_smallers.is_empty then
147 pager.add("known clients: ".bold + "{mmodule.in_importation.direct_smallers.join(", ")}\n")
148 end
149 pager.add_rule
150 pager.addn(nmodule.n_moduledecl.n_doc.comment.green)
151 pager.add_rule
152
153 var cats = new HashMap[String, Collection[MClass]]
154 cats["introduced classes"] = mmodule.intro_mclasses
155 cats["refined classes"] = mmodule.redef_mclasses
156 cats["imported classes"] = mmodule.imported_mclasses
157
158 for cat, list in cats do
159 if not list.is_empty then
160 pager.add("\n# {cat}".bold)
161 #sort list
162 var sorted = new Array[MClass]
163 sorted.add_all(list)
164 var sorter = new MClassNameSorter
165 sorter.sort(sorted)
166 for mclass in sorted do
167 var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
168 pager.add("")
169 if not nclass.n_doc == null and not nclass.n_doc.short_comment.is_empty then
170 pager.add("\t# {nclass.n_doc.short_comment}")
171 end
172 if cat == "refined classes" then
173 pager.add("\tredef {mclass.short_doc}")
174 else
175 pager.add("\t{mclass.short_doc}")
176 end
177 if cat != "introduced classes" then
178 pager.add("\t\t" + "introduced in {mmodule.full_name}::{mclass}".gray)
179 end
180 for mclassdef in mclass.mclassdefs do
181 if mclassdef != mclass.intro then
182 pager.add("\t\t" + "refined in {mclassdef.namespace}".gray)
183 end
184 end
185 end
186 end
187 end
188 pager.add_rule
189 end
190 pager.render
191 end
192
193 private fun classes_fulldoc(mclasses: List[MClass]) do
194 var pager = new Pager
195 for mclass in mclasses do
196 var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
197
198 pager.add("# {mclass.namespace}\n".bold)
199 pager.add("{mclass.short_doc}")
200 if not nclass.n_doc == null then
201 pager.add_rule
202 pager.addn(nclass.n_doc.comment.green)
203 end
204 pager.add_rule
205 if not mclass.parameter_types.is_empty then
206 pager.add("# formal types".bold)
207 for ft, bound in mclass.parameter_types do
208 pager.add("")
209 pager.add("\t{ft.to_s.green}: {bound}")
210 end
211 end
212 if not mclass.virtual_types.is_empty then
213 pager.add("# virtual types".bold)
214 for vt in mclass.virtual_types do
215 pager.add("")
216 mpropdef_fulldoc(pager, vt.intro)
217 end
218 end
219 pager.add_rule
220
221 var cats = new HashMap[String, Collection[MMethod]]
222 cats["constructors"] = mclass.constructors
223 cats["introduced methods"] = mclass.intro_methods
224 cats["refined methods"] = mclass.redef_methods
225 cats["inherited methods"] = mclass.inherited_methods
226
227 for cat, list in cats do
228 if not list.is_empty then
229 #sort list
230 var sorted = new Array[MMethod]
231 sorted.add_all(list)
232 var sorter = new MPropertyNameSorter
233 sorter.sort(sorted)
234 pager.add("\n# {cat}".bold)
235 for mprop in sorted do
236 pager.add("")
237 mpropdef_fulldoc(pager, mprop.intro)
238 end
239 end
240 end
241 pager.add_rule
242 end
243 pager.render
244 end
245
246 private fun props_fulldoc(raw_mprops: List[MProperty]) do
247 var pager = new Pager
248 # group by module
249 var cats = new HashMap[MClass, Array[MProperty]]
250 for mprop in raw_mprops do
251 if not mbuilder.mpropdef2npropdef.has_key(mprop.intro) then continue
252 if mprop isa MAttribute then continue
253 var mclass = mprop.intro_mclassdef.mclass
254 if not cats.has_key(mclass) then cats[mclass] = new Array[MProperty]
255 cats[mclass].add(mprop)
256 end
257 #sort groups
258 var sorter = new MClassNameSorter
259 var sorted = new Array[MClass]
260 sorted.add_all(cats.keys)
261 sorter.sort(sorted)
262 # display
263 for mclass in sorted do
264 var mprops = cats[mclass]
265 pager.add("# {mclass.namespace}".bold)
266 var sorterp = new MPropertyNameSorter
267 sorterp.sort(mprops)
268 for mprop in mprops do
269 pager.add("")
270 mpropdef_fulldoc(pager, mprop.intro)
271 end
272 pager.add_rule
273 end
274 pager.render
275 end
276
277 private fun seek_returns(entry: String): List[MProperty] do
278 # TODO how to match with generic types?
279 var matches = new List[MProperty]
280 for mprop in model.mproperties do
281 var intro = mprop.intro
282 if intro isa MMethodDef then
283 if intro.msignature.return_mtype != null and intro.msignature.return_mtype.to_s == entry then matches.add(mprop)
284 else if intro isa MAttributeDef then
285 if intro.static_mtype.to_s == entry then matches.add(mprop)
286 end
287 end
288 return matches
289 end
290
291 private fun seek_params(entry: String): List[MProperty] do
292 # TODO how to match with generic types?
293 var matches = new List[MProperty]
294 for mprop in model.mproperties do
295 var intro = mprop.intro
296 if intro isa MMethodDef then
297 var mparameters = intro.msignature.mparameters
298 for mparameter in mparameters do
299 if mparameter.mtype.to_s == entry then matches.add(mprop)
300 end
301 else if intro isa MAttributeDef then
302 if intro.static_mtype.to_s == entry then matches.add(mprop)
303 end
304 end
305 return matches
306 end
307
308 private fun mpropdef_fulldoc(pager: Pager, mpropdef: MPropDef) do
309 if mbuilder.mpropdef2npropdef.has_key(mpropdef) then
310 var nprop = mbuilder.mpropdef2npropdef[mpropdef]
311 if not nprop.n_doc == null and not nprop.n_doc.short_comment.is_empty then
312 pager.add("\t# {nprop.n_doc.short_comment}")
313 end
314 end
315 pager.add("\t{mpropdef}")
316 pager.add("\t\t" + "introduced in {mpropdef.mproperty.intro_mclassdef.namespace}".gray)
317 for mpdef in mpropdef.mproperty.mpropdefs do
318 if not mpdef.is_intro then
319 pager.add("\t\t" + "refined in {mpdef.mclassdef.namespace}".gray)
320 end
321 end
322 end
323 end
324
325 # Printing facilities
326
327 redef class MModule
328 private fun namespace: String do
329 return full_name
330 end
331 end
332
333 redef class MClass
334 redef fun to_s: String do
335 if arity > 0 then
336 return "{name}[{intro.parameter_names.join(", ")}]"
337 else
338 return name
339 end
340 end
341
342 private fun short_doc: String do
343 var ret = ""
344 if is_interface then ret = "interface {ret}"
345 if is_enum then ret = "enum {ret}"
346 if is_class then ret = "class {ret}"
347 if is_abstract then ret = "abstract {ret}"
348 if visibility.to_s == "public" then ret = "{ret}{to_s.green}"
349 if visibility.to_s == "private" then ret = "{ret}{to_s.red}"
350 if visibility.to_s == "protected" then ret = "{ret}{to_s.yellow}"
351 if not parents.is_empty then
352 ret = "{ret} super {parents.join(", ")}"
353 end
354 return ret
355 end
356
357 private fun namespace: String do
358 if not intro_mmodule.public_owner == null then
359 return "{intro_mmodule.public_owner.name}::{name}"
360 else
361 return "{intro_mmodule.name}::{name}"
362 end
363 end
364 end
365
366 redef class MClassDef
367 private fun namespace: String do
368 return "{mmodule.full_name}::{mclass.name}"
369 end
370 end
371
372 redef class MMethodDef
373 redef fun to_s do
374 var res = new Buffer
375 if not is_intro then res.append("redef ")
376 if not mproperty.is_init then res.append("fun ")
377 if mproperty.visibility.to_s == "public" then res.append(mproperty.name.green)
378 if mproperty.visibility.to_s == "private" then res.append(mproperty.name.red)
379 if mproperty.visibility.to_s == "protected" then res.append(mproperty.name.yellow)
380 if msignature != null then res.append(msignature.to_s)
381 # FIXME: modifiers should be accessible via the model
382 #if self isa ADeferredMethPropdef then ret = "{ret} is abstract"
383 #if self isa AInternMethPropdef then ret = "{ret} is intern"
384 #if self isa AExternMethPropdef then ret = "{ret} is extern"
385 return res.to_s
386 end
387 end
388
389 redef class MVirtualTypeDef
390 redef fun to_s do
391 var res = new Buffer
392 if mproperty.visibility.to_s == "public" then res.append(mproperty.name.green)
393 if mproperty.visibility.to_s == "private" then res.append(mproperty.name.red)
394 if mproperty.visibility.to_s == "protected" then res.append(mproperty.name.yellow)
395 res.append(": {bound.to_s}")
396 return res.to_s
397 end
398 end
399
400 redef class MSignature
401 redef fun to_s do
402 var res = new Buffer
403 if not mparameters.is_empty then
404 res.append("(")
405 for i in [0..mparameters.length[ do
406 res.append(mparameters[i].to_s)
407 if i < mparameters.length - 1 then res.append(", ")
408 end
409 res.append(")")
410 end
411 if return_mtype != null then
412 res.append(": {return_mtype.to_s}")
413 end
414 return res.to_s
415 end
416 end
417
418 redef class MParameter
419 redef fun to_s do
420 var res = new Buffer
421 res.append("{name}: {mtype}")
422 if is_vararg then res.append("...")
423 return res.to_s
424 end
425 end
426
427 redef class MNullableType
428 redef fun to_s do return "nullable {mtype}"
429 end
430
431 redef class MGenericType
432 redef fun to_s do
433 var res = new Buffer
434 res.append("{mclass.name}[")
435 for i in [0..arguments.length[ do
436 res.append(arguments[i].to_s)
437 if i < arguments.length - 1 then res.append(", ")
438 end
439 res.append("]")
440 return res.to_s
441 end
442 end
443
444 redef class MParameterType
445 redef fun to_s do return mclass.intro.parameter_names[rank]
446 end
447
448 redef class MVirtualType
449 redef fun to_s do return mproperty.intro.to_s
450 end
451
452 redef class ADoc
453 private fun comment: String do
454 var res = new Buffer
455 for t in n_comment do
456 res.append(t.text.replace("# ", "").replace("#", ""))
457 end
458 return res.to_s
459 end
460
461 private fun short_comment: String do
462 return n_comment.first.text.replace("# ", "").replace("\n", "")
463 end
464 end
465
466 redef class AAttrPropdef
467 private fun read_accessor: String do
468 var ret = "fun "
469 #FIXME bug with standard::stream::FDStream::fd
470 var name = mreadpropdef.mproperty.name
471 if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{name.green}"
472 if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{name.red}"
473 if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{name.yellow}"
474 ret = "{ret}: {n_type.to_s}"
475 if n_kwredef != null then ret = "redef {ret}"
476 return ret
477 end
478
479 private fun write_accessor: String do
480 var ret = "fun "
481 var name = "{mreadpropdef.mproperty.name}="
482 if n_readable != null and n_readable.n_visibility != null then
483 if n_readable.n_visibility isa APublicVisibility then ret = "{ret}{name.green}"
484 if n_readable.n_visibility isa APrivateVisibility then ret = "{ret}{name.red}"
485 if n_readable.n_visibility isa AProtectedVisibility then ret = "{ret}{name.yellow}"
486 else
487 ret = "{ret}{name.red}"
488 end
489 ret = "{ret}({mreadpropdef.mproperty.name}: {n_type.to_s})"
490 if n_kwredef != null then ret = "redef {ret}"
491 return ret
492 end
493 end
494
495 # Redef String class to add a function to color the string
496 redef class String
497
498 private fun add_escape_char(escapechar: String): String do
499 return "{escapechar}{self}\\033[0m"
500 end
501
502 private fun esc: Char do return 27.ascii
503 private fun red: String do return add_escape_char("{esc}[1;31m")
504 private fun yellow: String do return add_escape_char("{esc}[1;33m")
505 private fun green: String do return add_escape_char("{esc}[1;32m")
506 private fun blue: String do return add_escape_char("{esc}[1;34m")
507 private fun cyan: String do return add_escape_char("{esc}[1;36m")
508 private fun gray: String do return add_escape_char("{esc}[30;1m")
509 private fun bold: String do return add_escape_char("{esc}[1m")
510 private fun underline: String do return add_escape_char("{esc}[4m")
511
512 private fun escape: String
513 do
514 var b = new Buffer
515 for c in self do
516 if c == '\n' then
517 b.append("\\n")
518 else if c == '\0' then
519 b.append("\\0")
520 else if c == '"' then
521 b.append("\\\"")
522 else if c == '\\' then
523 b.append("\\\\")
524 else if c == '`' then
525 b.append("'")
526 else if c.ascii < 32 then
527 b.append("\\{c.ascii.to_base(8, false)}")
528 else
529 b.add(c)
530 end
531 end
532 return b.to_s
533 end
534 end
535
536 # Create a tool context to handle options and paths
537 var toolcontext = new ToolContext
538 toolcontext.process_options
539
540 # Here we launch the nit index
541 var ni = new NitIndex(toolcontext)
542 ni.start
543
544 # TODO seek subclasses and super classes <.<class> >.<class>
545 # TODO seek subclasses and super types <:<type> >:<type>
546 # TODO seek with regexp
547 # TODO standardize namespaces with private option