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 n
in mbuilder
.nmodules
do
91 if m
.name
== entry
then
96 for c
in model
.mclasses
do
97 if c
.name
== entry
then
98 if not mbuilder
.mclassdef2nclassdef
[c
.intro
] isa AStdClassdef then continue
103 var matches
= new List[MProperty]
104 for p
in model
.mproperties
do
105 if p
.name
== entry
then
110 #if not matches.is_empty then doc_properties
112 if not flag
then print
"Nothing known about '{entry}'"
113 if arguments
.length
== 1 then prompt
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(", ")}")
122 pager
.addn
(nmodule
.comment
.green
)
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
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}")
138 if cat
== "refined classes" then
139 pager
.add
("\tredef {mclass.short_doc}")
141 pager
.add
("\t{mclass.short_doc}")
143 if not mclass
.intro_mmodule
== mmodule
then
144 pager
.add
("\t\tintroduced in {mmodule.full_name}::{mclass}".gray
)
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} ")
160 pager
.addn
(nclass
.comment
.green
)
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}")
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
)
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
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
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}")
198 if cat
== "refined methods" then
199 pager
.add
("\tredef {nmethod}")
201 pager
.add
("\t{nmethod}")
203 if not mmethod
.intro_mclassdef
== mclass
.intro
then
204 pager
.add
("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray
)
214 # Printing facilities
218 redef fun to_s
: String do
220 return "{name}[{intro.parameter_names.join(", ")}]"
226 private fun short_doc
: String do
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(", ")}"
240 redef class MClassDef
241 private fun namespace
: String do
242 return "{mmodule.full_name}::{mclass.name}"
247 private fun comment
: String do
249 for t
in n_moduledecl
.n_doc
.n_comment
do
250 ret
+= "{t.text.replace("# ", "")}"
256 redef class AStdClassdef
257 private fun comment
: String do
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
("#", "")
269 private fun short_comment
: String do
271 if n_doc
!= null then
272 var txt
= n_doc
.n_comment
.first
.text
273 txt
= txt
.replace
("# ", "")
274 txt
= txt
.replace
("\n", "")
281 redef class AMethPropdef
282 private fun short_comment
: String do
284 if n_doc
!= null then
285 var txt
= n_doc
.n_comment
.first
.text
286 txt
= txt
.replace
("# ", "")
287 txt
= txt
.replace
("\n", "")
295 if not mpropdef
.mproperty
.is_init
then
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"
310 redef class ASignature
314 if not n_params
.is_empty
then
315 ret
= "{ret}({n_params.join(", ")})"
317 if n_type
!= null then ret
+= ": {n_type.to_s}"
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}..."
336 if n_kwnullable
!= null then ret
= "nullable {ret}"
337 if not n_types
.is_empty
then ret
= "{ret}[{n_types.join(", ")}]"
342 # Redef String class to add a function to color the string
345 private fun add_escape_char
(escapechar
: String): String do
346 return "{escapechar}{self}\\033[0m"
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")
359 private fun escape
: String
365 else if c
== '\0' then
367 else if c
== '"' then
369 else if c == '\\' then
371 else if c == '`' then
373 else if c.ascii < 32 then
374 b.append("\\{c.ascii.to_base(8, false)}")
383 # Create a tool context to handle options and paths
384 var toolcontext = new ToolContext
385 toolcontext.process_options
387 # Here we launch the nit index
388 var ni = new NitIndex(toolcontext)
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