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.arguments
= toolcontext
.option_context
.rest
41 if arguments
.length
> 2 then
42 print
"usage: ni path/to/module.nit [expression]"
43 toolcontext
.option_context
.usage
48 mbuilder
= new ModelBuilder(model
, toolcontext
)
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
61 if arguments
.length
== 1 then
70 print
"Welcome in Nit Index.\n"
71 print
"Loaded modules"
72 for m
in mbuilder
.nmodules
do
73 print
" - {m.mmodule.name}"
75 print
"\nEnter the module, class or property name you want to look up."
76 print
"Enter a blank line to exit.\n"
84 fun seek
(entry
: String) do
85 if entry
.is_empty
then exit
(0)
88 if entry
.has_prefix
("return:") then
89 var ret
= entry
.split_with
(":")[1].replace
(" ", "")
90 var matches
= seek_returns
(ret
)
91 if not matches
.is_empty
then props_fulldoc
(matches
)
92 else if entry
.has_prefix
("param:") then
93 var param
= entry
.split_with
(":")[1].replace
(" ", "")
94 var matches
= seek_params
(param
)
95 if not matches
.is_empty
then props_fulldoc
(matches
)
98 var mmatches
= new List[MModule]
99 for m
in model
.mmodules
do
100 if m
.name
== entry
then
105 if not mmatches
.is_empty
then modules_fulldoc
(mmatches
)
107 var cmatches
= new List[MClass]
108 for c
in model
.mclasses
do
109 if c
.name
== entry
then
114 if not cmatches
.is_empty
then classes_fulldoc
(cmatches
)
115 # seek for properties
116 var matches
= new List[MProperty]
117 for p
in model
.mproperties
do
118 if p
.name
== entry
then
123 if not matches
.is_empty
then props_fulldoc
(matches
)
126 if not flag
then print
"Nothing known about '{entry}'"
127 if arguments
.length
== 1 then prompt
130 private fun modules_fulldoc
(mmodules
: List[MModule]) do
131 var pager
= new Pager
132 for mmodule
in mmodules
do
133 var nmodule
= mbuilder
.mmodule2nmodule
[mmodule
]
134 pager
.add
("# module {mmodule.namespace}\n".bold
)
135 if not mmodule
.in_importation
.direct_greaters
.is_empty
then
136 pager
.add
("import ".bold
+ "{mmodule.in_importation.direct_greaters.join(", ")}\n")
138 if not mmodule
.in_importation
.direct_smallers
.is_empty
then
139 pager
.add
("known clients: ".bold
+ "{mmodule.in_importation.direct_smallers.join(", ")}\n")
142 pager
.addn
(nmodule
.comment
.green
)
145 var cats
= new HashMap[String, Collection[MClass]]
146 cats
["introduced classes"] = mmodule
.intro_mclasses
147 cats
["refined classes"] = mmodule
.redef_mclasses
148 cats
["imported classes"] = mmodule
.imported_mclasses
150 for cat
, list
in cats
do
151 if not list
.is_empty
then
152 pager
.add
("\n# {cat}".bold
)
154 var sorted
= new Array[MClass]
156 var sorter
= new ComparableSorter[MClass]
158 for mclass
in sorted
do
159 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
161 if not nclass
.short_comment
.is_empty
then
162 pager
.add
("\t# {nclass.short_comment}")
164 if cat
== "refined classes" then
165 pager
.add
("\tredef {mclass.short_doc}")
167 pager
.add
("\t{mclass.short_doc}")
169 if cat
!= "introduced classes" then
170 pager
.add
("\t\t" + "introduced in {mmodule.full_name}::{mclass}".gray
)
172 for mclassdef
in mclass
.mclassdefs
do
173 if mclassdef
!= mclass
.intro
then
174 pager
.add
("\t\t" + "refined in {mclassdef.namespace}".gray
)
185 private fun classes_fulldoc
(mclasses
: List[MClass]) do
186 var pager
= new Pager
187 for mclass
in mclasses
do
188 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
190 pager
.add
("# {mclass.namespace}\n".bold
)
191 pager
.add
("{mclass.short_doc}")
193 pager
.addn
(nclass
.comment
.green
)
195 if not mclass
.parameter_types
.is_empty
then
196 pager
.add
("# formal types".bold
)
197 for ft
, bound
in mclass
.parameter_types
do
199 pager
.add
("\t{ft.to_s.green}: {bound}")
202 if not mclass
.virtual_types
.is_empty
then
203 pager
.add
("# virtual types".bold
)
204 for vt
in mclass
.virtual_types
do
206 vt_fulldoc
(pager
, vt
)
211 var cats
= new HashMap[String, Collection[MMethod]]
212 cats
["constructors"] = mclass
.constructors
213 cats
["introduced methods"] = mclass
.intro_methods
214 cats
["refined methods"] = mclass
.redef_methods
215 cats
["inherited methods"] = mclass
.inherited_methods
217 for cat
, list
in cats
do
218 if not list
.is_empty
then
220 var sorted
= new Array[MMethod]
222 var sorter
= new ComparableSorter[MMethod]
224 pager
.add
("\n# {cat}".bold
)
225 for mprop
in sorted
do
227 method_fulldoc
(pager
, mprop
)
236 private fun props_fulldoc
(raw_mprops
: List[MProperty]) do
237 var pager
= new Pager
239 var cats
= new HashMap[MModule, Array[MProperty]]
240 for mprop
in raw_mprops
do
241 var mmodule
= mprop
.intro_mclassdef
.mmodule
242 if not cats
.has_key
(mmodule
) then cats
[mmodule
] = new Array[MProperty]
243 cats
[mmodule
].add
(mprop
)
246 var sorter
= new ComparableSorter[MModule]
247 var sorted
= new Array[MModule]
248 sorted
.add_all
(cats
.keys
)
251 for mmodule
in sorted
do
252 var mprops
= cats
[mmodule
]
253 pager
.add
("# {mmodule.namespace}".bold
)
254 var sorterp
= new ComparableSorter[MProperty]
256 for mprop
in mprops
do
257 if mprop
isa MMethod and mbuilder
.mpropdef2npropdef
.has_key
(mprop
.intro
) then
259 method_fulldoc
(pager
, mprop
)
260 else if mprop
isa MVirtualTypeProp then
262 vt_fulldoc
(pager
, mprop
)
270 private fun seek_returns
(entry
: String): List[MProperty] do
271 # TODO how to match with generic types?
272 var matches
= new List[MProperty]
273 for mprop
in model
.mproperties
do
274 var intro
= mprop
.intro
275 if intro
isa MMethodDef then
276 if intro
.msignature
.return_mtype
!= null and intro
.msignature
.return_mtype
.to_s
== entry
then matches
.add
(mprop
)
277 else if intro
isa MAttributeDef then
278 if intro
.static_mtype
.to_s
== entry
then matches
.add
(mprop
)
284 private fun seek_params
(entry
: String): List[MProperty] do
285 # TODO how to match with generic types?
286 var matches
= new List[MProperty]
287 for mprop
in model
.mproperties
do
288 var intro
= mprop
.intro
289 if intro
isa MMethodDef then
290 var mparameters
= intro
.msignature
.mparameters
291 for mparameter
in mparameters
do
292 if mparameter
.mtype
.to_s
== entry
then matches
.add
(mprop
)
294 else if intro
isa MAttributeDef then
295 if intro
.static_mtype
.to_s
== entry
then matches
.add
(mprop
)
301 private fun method_fulldoc
(pager
: Pager, mprop
: MMethod) do
302 if mbuilder
.mpropdef2npropdef
.has_key
(mprop
.intro
) then
303 var nprop
= mbuilder
.mpropdef2npropdef
[mprop
.intro
]
304 if not nprop
.short_comment
.is_empty
then
305 pager
.add
("\t# {nprop.short_comment}")
307 if nprop
isa AAttrPropdef then
308 pager
.add
("\t{nprop.read_accessor}")
309 pager
.add
("\t{nprop.write_accessor}")
310 else if nprop
isa AMethPropdef then
311 pager
.add
("\t{nprop}")
313 pager
.add
("\t\t" + "introduced in {mprop.intro_mclassdef.namespace}".gray
)
314 for mpropdef
in mprop
.mpropdefs
do
315 if mpropdef
!= mprop
.intro
then
316 pager
.add
("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray
)
322 private fun vt_fulldoc
(pager
: Pager, vt
: MVirtualTypeProp) do
323 pager
.add
("\t{vt.short_doc}")
324 pager
.add
("\t\t" + "introduced in {vt.intro_mclassdef.namespace}::{vt}".gray
)
325 for mpropdef
in vt
.mpropdefs
do
326 if mpropdef
!= vt
.intro
then
327 pager
.add
("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray
)
333 # Printing facilities
337 redef type OTHER: MModule
338 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
340 private fun namespace
: String do
347 redef type OTHER: MClass
348 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
350 redef fun to_s
: String do
352 return "{name}[{intro.parameter_names.join(", ")}]"
358 private fun short_doc
: String do
360 if is_interface
then ret
= "interface {ret}"
361 if is_enum
then ret
= "enum {ret}"
362 if is_class
then ret
= "class {ret}"
363 if is_abstract
then ret
= "abstract {ret}"
364 if visibility
.to_s
== "public" then ret
= "{ret}{to_s.green}"
365 if visibility
.to_s
== "private" then ret
= "{ret}{to_s.red}"
366 if visibility
.to_s
== "protected" then ret
= "{ret}{to_s.yellow}"
367 if not parents
.is_empty
then
368 ret
= "{ret} super {parents.join(", ")}"
373 private fun namespace
: String do
374 return "{intro_mmodule.public_owner.name}::{name}"
378 redef class MClassDef
379 private fun namespace
: String do
380 return "{mmodule.full_name}::{mclass.name}"
384 redef class MProperty
386 redef type OTHER: MProperty
387 redef fun <(other
: OTHER): Bool do return self.name
< other
.name
390 redef class MVirtualTypeProp
391 private fun short_doc
: String do
393 if visibility
.to_s
== "public" then ret
= "{to_s.green}: {intro.bound.to_s}"
394 if visibility
.to_s
== "private" then ret
= "\t{to_s.red}: {intro.bound.to_s}"
395 if visibility
.to_s
== "protected" then ret
= "\t{to_s.yellow}: {intro.bound.to_s}"
401 private fun comment
: String do
403 for t
in n_moduledecl
.n_doc
.n_comment
do
404 ret
+= "{t.text.replace("# ", "")}"
410 redef class AStdClassdef
411 private fun comment
: String do
413 if n_doc
!= null then
414 for t
in n_doc
.n_comment
do
415 var txt
= t
.text
.replace
("# ", "")
416 txt
= txt
.replace
("#", "")
423 private fun short_comment
: String do
425 if n_doc
!= null then
426 var txt
= n_doc
.n_comment
.first
.text
427 txt
= txt
.replace
("# ", "")
428 txt
= txt
.replace
("\n", "")
436 private fun short_comment
: String is abstract
439 redef class AAttrPropdef
440 redef fun short_comment
do
442 if n_doc
!= null then
443 var txt
= n_doc
.n_comment
.first
.text
444 txt
= txt
.replace
("# ", "")
445 txt
= txt
.replace
("\n", "")
451 private fun read_accessor
: String do
453 #FIXME bug with standard::stream::FDStream::fd
454 var name
= mreadpropdef
.mproperty
.name
455 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{name.green}"
456 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{name.red}"
457 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{name.yellow}"
458 ret
= "{ret}: {n_type.to_s}"
459 if n_kwredef
!= null then ret
= "redef {ret}"
463 private fun write_accessor
: String do
465 var name
= "{mreadpropdef.mproperty.name}="
466 if n_readable
!= null and n_readable
.n_visibility
!= null then
467 if n_readable
.n_visibility
isa APublicVisibility then ret
= "{ret}{name.green}"
468 if n_readable
.n_visibility
isa APrivateVisibility then ret
= "{ret}{name.red}"
469 if n_readable
.n_visibility
isa AProtectedVisibility then ret
= "{ret}{name.yellow}"
471 ret
= "{ret}{name.red}"
473 ret
= "{ret}({mreadpropdef.mproperty.name}: {n_type.to_s})"
474 if n_kwredef
!= null then ret
= "redef {ret}"
479 redef class AMethPropdef
480 redef fun short_comment
do
482 if n_doc
!= null then
483 var txt
= n_doc
.n_comment
.first
.text
484 txt
= txt
.replace
("# ", "")
485 txt
= txt
.replace
("\n", "")
493 if not mpropdef
.mproperty
.is_init
then
496 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{mpropdef.mproperty.name.green}"
497 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{mpropdef.mproperty.name.red}"
498 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{mpropdef.mproperty.name.yellow}"
499 if n_signature
!= null then ret
= "{ret}{n_signature.to_s}"
500 if n_kwredef
!= null then ret
= "redef {ret}"
501 if self isa ADeferredMethPropdef then ret
= "{ret} is abstract"
502 if self isa AInternMethPropdef then ret
= "{ret} is intern"
503 if self isa AExternMethPropdef then ret
= "{ret} is extern"
508 redef class ASignature
512 if not n_params
.is_empty
then
513 ret
= "{ret}({n_params.join(", ")})"
515 if n_type
!= null then ret
+= ": {n_type.to_s}"
522 var ret
= "{n_id.text}"
523 if n_type
!= null then
524 ret
= "{ret}: {n_type.to_s}"
525 if n_dotdotdot
!= null then ret
= "{ret}..."
534 if n_kwnullable
!= null then ret
= "nullable {ret}"
535 if not n_types
.is_empty
then ret
= "{ret}[{n_types.join(", ")}]"
540 # Redef String class to add a function to color the string
543 private fun add_escape_char
(escapechar
: String): String do
544 return "{escapechar}{self}\\033[0m"
547 private fun esc
: Char do return 27.ascii
548 private fun red
: String do return add_escape_char
("{esc}[1;31m")
549 private fun yellow
: String do return add_escape_char
("{esc}[1;33m")
550 private fun green
: String do return add_escape_char
("{esc}[1;32m")
551 private fun blue
: String do return add_escape_char
("{esc}[1;34m")
552 private fun cyan
: String do return add_escape_char
("{esc}[1;36m")
553 private fun gray
: String do return add_escape_char
("{esc}[30;1m")
554 private fun bold
: String do return add_escape_char
("{esc}[1m")
555 private fun underline
: String do return add_escape_char
("{esc}[4m")
557 private fun escape
: String
563 else if c
== '\0' then
565 else if c
== '"' then
567 else if c == '\\' then
569 else if c == '`' then
571 else if c.ascii < 32 then
572 b.append("\\{c.ascii.to_base(8, false)}")
581 # Create a tool context to handle options and paths
582 var toolcontext = new ToolContext
583 toolcontext.process_options
585 # Here we launch the nit index
586 var ni = new NitIndex(toolcontext)
589 # TODO seek subclasses and super classes <.<class> >.<class>
590 # TODO seek subclasses and super types <:<type> >:<type>
591 # TODO seek with regexp
592 # TODO standardize namespaces with private option