1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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")
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]
36 init(toolcontext
: ToolContext) do
37 # We need a model to collect stufs
38 self.toolcontext
= toolcontext
39 self.toolcontext
.option_context
.options
.clear
40 self.arguments
= toolcontext
.option_context
.rest
42 if arguments
.is_empty
or arguments
.length
> 2 then
43 print
"usage: ni path/to/module.nit [expression]"
44 toolcontext
.option_context
.usage
49 mbuilder
= new ModelBuilder(model
, toolcontext
)
51 # Here we load an process std modules
52 #var dir = "NIT_DIR".environ
53 #var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"])
54 var mmodules
= mbuilder
.parse_and_build
([arguments
.first
])
55 if mmodules
.is_empty
then return
56 mbuilder
.full_propdef_semantic_analysis
57 assert mmodules
.length
== 1
58 self.mainmodule
= mmodules
.first
62 if arguments
.length
== 1 then
71 print
"Welcome in Nit Index.\n"
72 print
"Loaded modules"
73 for m
in mbuilder
.nmodules
do
74 print
" - {m.mmodule.name}"
76 print
"\nEnter the module, class or property name you want to look up."
77 print
"Enter a blank line to exit.\n"
85 fun seek
(entry
: String) do
86 if entry
.is_empty
then exit
(0)
89 if entry
.has_prefix
("return:") then
90 var ret
= entry
.split_with
(":")[1].replace
(" ", "")
91 var matches
= seek_returns
(ret
)
92 if not matches
.is_empty
then
94 props_fulldoc
(matches
)
96 else if entry
.has_prefix
("param:") then
97 var param
= entry
.split_with
(":")[1].replace
(" ", "")
98 var matches
= seek_params
(param
)
99 if not matches
.is_empty
then
101 props_fulldoc
(matches
)
105 var mmatches
= new List[MModule]
106 for m
in model
.mmodules
do
107 if m
.name
== entry
then
112 if not mmatches
.is_empty
then modules_fulldoc
(mmatches
)
114 var cmatches
= new List[MClass]
115 for c
in model
.mclasses
do
116 if c
.name
== entry
then
121 if not cmatches
.is_empty
then classes_fulldoc
(cmatches
)
122 # seek for properties
123 var matches
= new List[MProperty]
124 for p
in model
.mproperties
do
125 if p
.name
== entry
then
130 if not matches
.is_empty
then props_fulldoc
(matches
)
133 if not flag
then print
"Nothing known about '{entry}'"
134 if arguments
.length
== 1 then prompt
137 private fun modules_fulldoc
(mmodules
: List[MModule]) do
138 var pager
= new Pager
139 for mmodule
in mmodules
do
140 var nmodule
= mbuilder
.mmodule2nmodule
[mmodule
]
141 pager
.add
("# module {mmodule.namespace}\n".bold
)
142 if not mmodule
.in_importation
.direct_greaters
.is_empty
then
143 pager
.add
("import ".bold
+ "{mmodule.in_importation.direct_greaters.join(", ")}\n")
145 if not mmodule
.in_importation
.direct_smallers
.is_empty
then
146 pager
.add
("known clients: ".bold
+ "{mmodule.in_importation.direct_smallers.join(", ")}\n")
149 pager
.addn
(nmodule
.comment
.green
)
152 var cats
= new HashMap[String, Collection[MClass]]
153 cats
["introduced classes"] = mmodule
.intro_mclasses
154 cats
["refined classes"] = mmodule
.redef_mclasses
155 cats
["imported classes"] = mmodule
.imported_mclasses
157 for cat
, list
in cats
do
158 if not list
.is_empty
then
159 pager
.add
("\n# {cat}".bold
)
161 var sorted
= new Array[MClass]
163 var sorter
= new ComparableSorter[MClass]
165 for mclass
in sorted
do
166 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
168 if not nclass
.short_comment
.is_empty
then
169 pager
.add
("\t# {nclass.short_comment}")
171 if cat
== "refined classes" then
172 pager
.add
("\tredef {mclass.short_doc}")
174 pager
.add
("\t{mclass.short_doc}")
176 if cat
!= "introduced classes" then
177 pager
.add
("\t\t" + "introduced in {mmodule.full_name}::{mclass}".gray
)
179 for mclassdef
in mclass
.mclassdefs
do
180 if mclassdef
!= mclass
.intro
then
181 pager
.add
("\t\t" + "refined in {mclassdef.namespace}".gray
)
192 private fun classes_fulldoc
(mclasses
: List[MClass]) do
193 var pager
= new Pager
194 for mclass
in mclasses
do
195 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
197 pager
.add
("# {mclass.namespace}\n".bold
)
198 pager
.add
("{mclass.short_doc}")
200 pager
.addn
(nclass
.comment
.green
)
202 if not mclass
.parameter_types
.is_empty
then
203 pager
.add
("# formal types".bold
)
204 for ft
, bound
in mclass
.parameter_types
do
206 pager
.add
("\t{ft.to_s.green}: {bound}")
209 if not mclass
.virtual_types
.is_empty
then
210 pager
.add
("# virtual types".bold
)
211 for vt
in mclass
.virtual_types
do
213 vt_fulldoc
(pager
, vt
)
218 var cats
= new HashMap[String, Collection[MMethod]]
219 cats
["constructors"] = mclass
.constructors
220 cats
["introduced methods"] = mclass
.intro_methods
221 cats
["refined methods"] = mclass
.redef_methods
222 cats
["inherited methods"] = mclass
.inherited_methods
224 for cat
, list
in cats
do
225 if not list
.is_empty
then
227 var sorted
= new Array[MMethod]
229 var sorter
= new ComparableSorter[MMethod]
231 pager
.add
("\n# {cat}".bold
)
232 for mprop
in sorted
do
234 method_fulldoc
(pager
, mprop
)
243 private fun props_fulldoc
(raw_mprops
: List[MProperty]) do
244 var pager
= new Pager
246 var cats
= new HashMap[MClass, Array[MProperty]]
247 for mprop
in raw_mprops
do
248 if not mbuilder
.mpropdef2npropdef
.has_key
(mprop
.intro
) then continue
249 if mprop
isa MAttribute then continue
250 var mclass
= mprop
.intro_mclassdef
.mclass
251 if not cats
.has_key
(mclass
) then cats
[mclass
] = new Array[MProperty]
252 cats
[mclass
].add
(mprop
)
255 var sorter
= new ComparableSorter[MClass]
256 var sorted
= new Array[MClass]
257 sorted
.add_all
(cats
.keys
)
260 for mclass
in sorted
do
261 var mprops
= cats
[mclass
]
262 pager
.add
("# {mclass.namespace}".bold
)
263 var sorterp
= new ComparableSorter[MProperty]
265 for mprop
in mprops
do
266 if mprop
isa MMethod and mbuilder
.mpropdef2npropdef
.has_key
(mprop
.intro
) then
268 method_fulldoc
(pager
, mprop
)
269 else if mprop
isa MVirtualTypeProp then
271 vt_fulldoc
(pager
, mprop
)
279 private fun seek_returns
(entry
: String): List[MProperty] do
280 # TODO how to match with generic types?
281 var matches
= new List[MProperty]
282 for mprop
in model
.mproperties
do
283 var intro
= mprop
.intro
284 if intro
isa MMethodDef then
285 if intro
.msignature
.return_mtype
!= null and intro
.msignature
.return_mtype
.to_s
== entry
then matches
.add
(mprop
)
286 else if intro
isa MAttributeDef then
287 if intro
.static_mtype
.to_s
== entry
then matches
.add
(mprop
)
293 private fun seek_params
(entry
: String): List[MProperty] do
294 # TODO how to match with generic types?
295 var matches
= new List[MProperty]
296 for mprop
in model
.mproperties
do
297 var intro
= mprop
.intro
298 if intro
isa MMethodDef then
299 var mparameters
= intro
.msignature
.mparameters
300 for mparameter
in mparameters
do
301 if mparameter
.mtype
.to_s
== entry
then matches
.add
(mprop
)
303 else if intro
isa MAttributeDef then
304 if intro
.static_mtype
.to_s
== entry
then matches
.add
(mprop
)
310 private fun method_fulldoc
(pager
: Pager, mprop
: MMethod) do
311 if mbuilder
.mpropdef2npropdef
.has_key
(mprop
.intro
) then
312 var nprop
= mbuilder
.mpropdef2npropdef
[mprop
.intro
]
313 if not nprop
.short_comment
.is_empty
then
314 pager
.add
("\t# {nprop.short_comment}")
316 if nprop
isa AAttrPropdef then
317 pager
.add
("\t{nprop.read_accessor}")
318 pager
.add
("\t{nprop.write_accessor}")
319 else if nprop
isa AMethPropdef then
320 pager
.add
("\t{nprop}")
322 pager
.add
("\t\t" + "introduced in {mprop.intro_mclassdef.namespace}".gray
)
323 for mpropdef
in mprop
.mpropdefs
do
324 if mpropdef
!= mprop
.intro
then
325 pager
.add
("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray
)
331 private fun vt_fulldoc
(pager
: Pager, vt
: MVirtualTypeProp) do
332 pager
.add
("\t{vt.short_doc}")
333 pager
.add
("\t\t" + "introduced in {vt.intro_mclassdef.namespace}::{vt}".gray
)
334 for mpropdef
in vt
.mpropdefs
do
335 if mpropdef
!= vt
.intro
then
336 pager
.add
("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray
)
342 # Printing facilities
346 redef type OTHER: MModule
347 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
349 private fun namespace
: String do
356 redef type OTHER: MClass
357 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
359 redef fun to_s
: String do
361 return "{name}[{intro.parameter_names.join(", ")}]"
367 private fun short_doc
: String do
369 if is_interface
then ret
= "interface {ret}"
370 if is_enum
then ret
= "enum {ret}"
371 if is_class
then ret
= "class {ret}"
372 if is_abstract
then ret
= "abstract {ret}"
373 if visibility
.to_s
== "public" then ret
= "{ret}{to_s.green}"
374 if visibility
.to_s
== "private" then ret
= "{ret}{to_s.red}"
375 if visibility
.to_s
== "protected" then ret
= "{ret}{to_s.yellow}"
376 if not parents
.is_empty
then
377 ret
= "{ret} super {parents.join(", ")}"
382 private fun namespace
: String do
383 return "{intro_mmodule.public_owner.name}::{name}"
387 redef class MClassDef
388 private fun namespace
: String do
389 return "{mmodule.full_name}::{mclass.name}"
393 redef class MProperty
395 redef type OTHER: MProperty
396 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
399 redef class MVirtualTypeProp
400 private fun short_doc
: String do
402 if visibility
.to_s
== "public" then ret
= "{to_s.green}: {intro.bound.to_s}"
403 if visibility
.to_s
== "private" then ret
= "\t{to_s.red}: {intro.bound.to_s}"
404 if visibility
.to_s
== "protected" then ret
= "\t{to_s.yellow}: {intro.bound.to_s}"
410 private fun comment
: String do
412 for t
in n_moduledecl
.n_doc
.n_comment
do
413 ret
+= "{t.text.replace("# ", "")}"
419 redef class AStdClassdef
420 private fun comment
: String do
422 if n_doc
!= null then
423 for t
in n_doc
.n_comment
do
424 var txt
= t
.text
.replace
("# ", "")
425 txt
= txt
.replace
("#", "")
432 private fun short_comment
: String do
434 if n_doc
!= null then
435 var txt
= n_doc
.n_comment
.first
.text
436 txt
= txt
.replace
("# ", "")
437 txt
= txt
.replace
("\n", "")
445 private fun comment
: String do
447 if n_doc
!= null then
448 for t
in n_doc
.n_comment
do
449 var txt
= t
.text
.replace
("# ", "")
450 txt
= txt
.replace
("#", "")
457 private fun short_comment
: String do
459 if n_doc
!= null then
460 var txt
= n_doc
.n_comment
.first
.text
461 txt
= txt
.replace
("# ", "")
462 txt
= txt
.replace
("\n", "")
469 redef class AAttrPropdef
470 private fun read_accessor
: String do
472 #FIXME bug with standard::stream::FDStream::fd
473 var name
= mreadpropdef
.mproperty
.name
474 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{name.green}"
475 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{name.red}"
476 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{name.yellow}"
477 ret
= "{ret}: {n_type.to_s}"
478 if n_kwredef
!= null then ret
= "redef {ret}"
482 private fun write_accessor
: String do
484 var name
= "{mreadpropdef.mproperty.name}="
485 if n_readable
!= null and n_readable
.n_visibility
!= null then
486 if n_readable
.n_visibility
isa APublicVisibility then ret
= "{ret}{name.green}"
487 if n_readable
.n_visibility
isa APrivateVisibility then ret
= "{ret}{name.red}"
488 if n_readable
.n_visibility
isa AProtectedVisibility then ret
= "{ret}{name.yellow}"
490 ret
= "{ret}{name.red}"
492 ret
= "{ret}({mreadpropdef.mproperty.name}: {n_type.to_s})"
493 if n_kwredef
!= null then ret
= "redef {ret}"
498 redef class AMethPropdef
501 if not mpropdef
.mproperty
.is_init
then
504 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{mpropdef.mproperty.name.green}"
505 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{mpropdef.mproperty.name.red}"
506 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{mpropdef.mproperty.name.yellow}"
507 if n_signature
!= null then ret
= "{ret}{n_signature.to_s}"
508 if n_kwredef
!= null then ret
= "redef {ret}"
509 if self isa ADeferredMethPropdef then ret
= "{ret} is abstract"
510 if self isa AInternMethPropdef then ret
= "{ret} is intern"
511 if self isa AExternMethPropdef then ret
= "{ret} is extern"
516 redef class ASignature
520 if not n_params
.is_empty
then
521 ret
= "{ret}({n_params.join(", ")})"
523 if n_type
!= null then ret
+= ": {n_type.to_s}"
530 var ret
= "{n_id.text}"
531 if n_type
!= null then
532 ret
= "{ret}: {n_type.to_s}"
533 if n_dotdotdot
!= null then ret
= "{ret}..."
542 if n_kwnullable
!= null then ret
= "nullable {ret}"
543 if not n_types
.is_empty
then ret
= "{ret}[{n_types.join(", ")}]"
548 # Redef String class to add a function to color the string
551 private fun add_escape_char
(escapechar
: String): String do
552 return "{escapechar}{self}\\033[0m"
555 private fun esc
: Char do return 27.ascii
556 private fun red
: String do return add_escape_char
("{esc}[1;31m")
557 private fun yellow
: String do return add_escape_char
("{esc}[1;33m")
558 private fun green
: String do return add_escape_char
("{esc}[1;32m")
559 private fun blue
: String do return add_escape_char
("{esc}[1;34m")
560 private fun cyan
: String do return add_escape_char
("{esc}[1;36m")
561 private fun gray
: String do return add_escape_char
("{esc}[30;1m")
562 private fun bold
: String do return add_escape_char
("{esc}[1m")
563 private fun underline
: String do return add_escape_char
("{esc}[4m")
565 private fun escape
: String
571 else if c
== '\0' then
573 else if c
== '"' then
575 else if c == '\\' then
577 else if c == '`' then
579 else if c.ascii < 32 then
580 b.append("\\{c.ascii.to_base(8, false)}")
589 # Create a tool context to handle options and paths
590 var toolcontext = new ToolContext
591 toolcontext.process_options
593 # Here we launch the nit index
594 var ni = new NitIndex(toolcontext)
597 # TODO seek subclasses and super classes <.<class> >.<class>
598 # TODO seek subclasses and super types <:<type> >:<type>
599 # TODO seek with regexp
600 # TODO standardize namespaces with private option