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
86 if entry
.is_empty
then exit
(0)
89 for m
in model
.mmodules
do
90 if m
.name
== entry
then
95 for c
in model
.mclasses
do
96 if c
.name
== entry
then
97 if not mbuilder
.mclassdef2nclassdef
[c
.intro
] isa AStdClassdef then continue
102 var matches
= new List[MProperty]
103 for p
in model
.mproperties
do
104 if p
.name
== entry
then
109 if not matches
.is_empty
then props_fulldoc
(matches
)
111 if not flag
then print
"Nothing known about '{entry}'"
112 if arguments
.length
== 1 then prompt
115 private fun module_fulldoc
(mmodule
: MModule) do
116 var nmodule
= mbuilder
.mmodule2nmodule
[mmodule
]
117 var pager
= new Pager
118 pager
.add
("# module {mmodule.name}\n".bold
)
119 pager
.add
("import {mmodule.in_importation.direct_greaters.join(", ")}")
121 pager
.addn
(nmodule
.comment
.green
)
124 var cats
= new HashMap[String, Collection[MClass]]
125 cats
["introduced classes"] = mmodule
.intro_mclasses
126 cats
["refined classes"] = mmodule
.redef_mclasses
127 cats
["inherited classes"] = mmodule
.imported_mclasses
129 for cat
, list
in cats
do
130 if not list
.is_empty
then
131 pager
.add
("# {cat}\n".bold
)
132 for mclass
in list
do
133 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
134 if not nclass
.short_comment
.is_empty
then
135 pager
.add
("\t# {nclass.short_comment}")
137 if cat
== "refined classes" then
138 pager
.add
("\tredef {mclass.short_doc}")
140 pager
.add
("\t{mclass.short_doc}")
142 if not mclass
.intro_mmodule
== mmodule
then
143 pager
.add
("\t\tintroduced in {mmodule.full_name}::{mclass}".gray
)
153 private fun class_fulldoc
(mclass
: MClass) do
154 var nclass
= mbuilder
.mclassdef2nclassdef
[mclass
.intro
].as(AStdClassdef)
155 var pager
= new Pager
156 pager
.add
("# {mclass.intro_mmodule.public_owner.name}::{mclass.name}\n".bold
)
157 pager
.add
("{mclass.short_doc} ")
159 pager
.addn
(nclass
.comment
.green
)
161 if not mclass
.parameter_types
.is_empty
then
162 pager
.add
("# formal types\n".bold
)
163 for ft
, bound
in mclass
.parameter_types
do
164 pager
.add
("\t{ft.to_s.green}: {bound}")
168 if not mclass
.virtual_types
.is_empty
then
169 pager
.add
("# virtual types\n".bold
)
170 for vt
in mclass
.virtual_types
do
171 if vt
.visibility
.to_s
== "public" then pager
.add
("\t{vt.to_s.green}: {vt.intro.bound.to_s}")
172 if vt
.visibility
.to_s
== "private" then pager
.add
("\t{vt.to_s.red}: {vt.intro.bound.to_s}")
173 if vt
.visibility
.to_s
== "protected" then pager
.add
("\t{vt.to_s.yellow}: {vt.intro.bound.to_s}")
174 if vt
.intro_mclassdef
!= mclass
.intro
then
175 pager
.add
("\t\tintroduced in {vt.intro_mclassdef.namespace}::{vt}".gray
)
182 var cats
= new HashMap[String, Collection[MMethod]]
183 cats
["constructors"] = mclass
.constructors
184 cats
["introduced methods"] = mclass
.intro_methods
185 cats
["refined methods"] = mclass
.redef_methods
186 cats
["inherited methods"] = mclass
.inherited_methods
188 for cat
, list
in cats
do
189 if not list
.is_empty
then
190 pager
.add
("# {cat}\n".bold
)
191 for mmethod
in list
do
193 var nmethod
= mbuilder
.mpropdef2npropdef
[mmethod
.intro
].as(AMethPropdef)
194 if not nmethod
.short_comment
.is_empty
then
195 pager
.add
("\t# {nmethod.short_comment}")
197 if cat
== "refined methods" then
198 pager
.add
("\tredef {nmethod}")
200 pager
.add
("\t{nmethod}")
202 if not mmethod
.intro_mclassdef
== mclass
.intro
then
203 pager
.add
("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray
)
212 private fun props_fulldoc
(mprops
: List[MProperty]) do
213 var pager
= new Pager
214 for mprop
in mprops
do
215 var nprop
= mbuilder
.mpropdef2npropdef
[mprop
.intro
]
216 if nprop
isa AMethPropdef then
217 pager
.add
("# {mprop.intro_mclassdef.namespace.bold}\n")
218 if not nprop
.short_comment
.is_empty
then
219 pager
.add
("\t# {nprop.short_comment}")
221 pager
.add
("\t{nprop}")
222 pager
.add
("\t\t" + "introduced in {mprop.intro_mclassdef.namespace}".gray
)
223 for mpropdef
in mprop
.mpropdefs
do
224 if mpropdef
!= mprop
.intro
then
225 pager
.add
("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray
)
235 # Printing facilities
239 redef fun to_s
: String do
241 return "{name}[{intro.parameter_names.join(", ")}]"
247 private fun short_doc
: String do
249 if is_interface
then ret
= "interface {ret}"
250 if is_enum
then ret
= "enum {ret}"
251 if is_class
then ret
= "class {ret}"
252 if is_abstract
then ret
= "abstract {ret}"
253 if visibility
.to_s
== "public" then ret
= "{ret}{to_s.green}"
254 if visibility
.to_s
== "private" then ret
= "{ret}{to_s.red}"
255 if visibility
.to_s
== "protected" then ret
= "{ret}{to_s.yellow}"
256 ret
= "{ret} super {parents.join(", ")}"
261 redef class MClassDef
262 private fun namespace
: String do
263 return "{mmodule.full_name}::{mclass.name}"
268 private fun comment
: String do
270 for t
in n_moduledecl
.n_doc
.n_comment
do
271 ret
+= "{t.text.replace("# ", "")}"
277 redef class AStdClassdef
278 private fun comment
: String do
280 if n_doc
!= null then
281 for t
in n_doc
.n_comment
do
282 var txt
= t
.text
.replace
("# ", "")
283 txt
= txt
.replace
("#", "")
290 private fun short_comment
: String do
292 if n_doc
!= null then
293 var txt
= n_doc
.n_comment
.first
.text
294 txt
= txt
.replace
("# ", "")
295 txt
= txt
.replace
("\n", "")
302 redef class AMethPropdef
303 private fun short_comment
: String do
305 if n_doc
!= null then
306 var txt
= n_doc
.n_comment
.first
.text
307 txt
= txt
.replace
("# ", "")
308 txt
= txt
.replace
("\n", "")
316 if not mpropdef
.mproperty
.is_init
then
319 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{mpropdef.mproperty.name.green}"
320 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{mpropdef.mproperty.name.red}"
321 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{mpropdef.mproperty.name.yellow}"
322 if n_signature
!= null then ret
= "{ret}{n_signature.to_s}"
323 if n_kwredef
!= null then ret
= "redef {ret}"
324 if self isa ADeferredMethPropdef then ret
= "{ret} is abstract"
325 if self isa AInternMethPropdef then ret
= "{ret} is intern"
326 if self isa AExternMethPropdef then ret
= "{ret} is extern"
331 redef class ASignature
335 if not n_params
.is_empty
then
336 ret
= "{ret}({n_params.join(", ")})"
338 if n_type
!= null then ret
+= ": {n_type.to_s}"
345 var ret
= "{n_id.text}"
346 if n_type
!= null then
347 ret
= "{ret}: {n_type.to_s}"
348 if n_dotdotdot
!= null then ret
= "{ret}..."
357 if n_kwnullable
!= null then ret
= "nullable {ret}"
358 if not n_types
.is_empty
then ret
= "{ret}[{n_types.join(", ")}]"
363 # Redef String class to add a function to color the string
366 private fun add_escape_char
(escapechar
: String): String do
367 return "{escapechar}{self}\\033[0m"
370 private fun esc
: Char do return 27.ascii
371 private fun red
: String do return add_escape_char
("{esc}[1;31m")
372 private fun yellow
: String do return add_escape_char
("{esc}[1;33m")
373 private fun green
: String do return add_escape_char
("{esc}[1;32m")
374 private fun blue
: String do return add_escape_char
("{esc}[1;34m")
375 private fun cyan
: String do return add_escape_char
("{esc}[1;36m")
376 private fun gray
: String do return add_escape_char
("{esc}[30;1m")
377 private fun bold
: String do return add_escape_char
("{esc}[1m")
378 private fun underline
: String do return add_escape_char
("{esc}[4m")
380 private fun escape
: String
386 else if c
== '\0' then
388 else if c
== '"' then
390 else if c == '\\' then
392 else if c == '`' then
394 else if c.ascii < 32 then
395 b.append("\\{c.ascii.to_base(8, false)}")
404 # Create a tool context to handle options and paths
405 var toolcontext = new ToolContext
406 toolcontext.process_options
408 # Here we launch the nit index
409 var ni = new NitIndex(toolcontext)
413 # TODO lister les methods qui retournent un certain type
414 # TODO lister les methods qui utilisent un certain type
415 # TODO lister les sous-types connus d'une
type
416 # TODO sorter par ordre alphabétique