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
)
165 var cats
= new HashMap[String, Collection[MMethod]]
166 cats
["constructors"] = mclass
.constructors
167 cats
["introduced methods"] = mclass
.intro_methods
168 cats
["refined methods"] = mclass
.redef_methods
169 cats
["inherited methods"] = mclass
.inherited_methods
171 for cat
, list
in cats
do
172 if not list
.is_empty
then
173 pager
.add
("# {cat}\n".bold
)
174 for mmethod
in list
do
176 var nmethod
= mbuilder
.mpropdef2npropdef
[mmethod
.intro
].as(AMethPropdef)
177 if not nmethod
.short_comment
.is_empty
then
178 pager
.add
("\t# {nmethod.short_comment}")
180 if cat
== "refined methods" then
181 pager
.add
("\tredef {nmethod}")
183 pager
.add
("\t{nmethod}")
185 if not mmethod
.intro_mclassdef
== mclass
.intro
then
186 pager
.add
("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray
)
196 # Printing facilities
200 redef fun to_s
: String do
202 return "{name}[{intro.parameter_names.join(", ")}]"
208 private fun short_doc
: String do
210 if is_interface
then ret
= "interface {ret}"
211 if is_enum
then ret
= "enum {ret}"
212 if is_class
then ret
= "class {ret}"
213 if is_abstract
then ret
= "abstract {ret}"
214 if visibility
.to_s
== "public" then ret
= "{ret}{to_s.green}"
215 if visibility
.to_s
== "private" then ret
= "{ret}{to_s.red}"
216 if visibility
.to_s
== "protected" then ret
= "{ret}{to_s.yellow}"
217 ret
= "{ret} super {parents.join(", ")}"
222 redef class MClassDef
223 private fun namespace
: String do
224 return "{mmodule.full_name}::{mclass.name}"
229 private fun comment
: String do
231 for t
in n_moduledecl
.n_doc
.n_comment
do
232 ret
+= "{t.text.replace("# ", "")}"
238 redef class AStdClassdef
239 private fun comment
: String do
241 if n_doc
!= null then
242 for t
in n_doc
.n_comment
do
243 var txt
= t
.text
.replace
("# ", "")
244 txt
= txt
.replace
("#", "")
251 private fun short_comment
: String do
253 if n_doc
!= null then
254 var txt
= n_doc
.n_comment
.first
.text
255 txt
= txt
.replace
("# ", "")
256 txt
= txt
.replace
("\n", "")
263 redef class AMethPropdef
264 private fun short_comment
: String do
266 if n_doc
!= null then
267 var txt
= n_doc
.n_comment
.first
.text
268 txt
= txt
.replace
("# ", "")
269 txt
= txt
.replace
("\n", "")
277 if not mpropdef
.mproperty
.is_init
then
280 if mpropdef
.mproperty
.visibility
.to_s
== "public" then ret
= "{ret}{mpropdef.mproperty.name.green}"
281 if mpropdef
.mproperty
.visibility
.to_s
== "private" then ret
= "{ret}{mpropdef.mproperty.name.red}"
282 if mpropdef
.mproperty
.visibility
.to_s
== "protected" then ret
= "{ret}{mpropdef.mproperty.name.yellow}"
283 if n_signature
!= null then ret
= "{ret}{n_signature.to_s}"
284 if n_kwredef
!= null then ret
= "redef {ret}"
285 if self isa ADeferredMethPropdef then ret
= "{ret} is abstract"
286 if self isa AInternMethPropdef then ret
= "{ret} is intern"
287 if self isa AExternMethPropdef then ret
= "{ret} is extern"
292 redef class ASignature
296 if not n_params
.is_empty
then
297 ret
= "{ret}({n_params.join(", ")})"
299 if n_type
!= null then ret
+= ": {n_type.to_s}"
306 var ret
= "{n_id.text}"
307 if n_type
!= null then
308 ret
= "{ret}: {n_type.to_s}"
309 if n_dotdotdot
!= null then ret
= "{ret}..."
318 if n_kwnullable
!= null then ret
= "nullable {ret}"
319 if not n_types
.is_empty
then ret
= "{ret}[{n_types.join(", ")}]"
324 # Redef String class to add a function to color the string
327 private fun add_escape_char
(escapechar
: String): String do
328 return "{escapechar}{self}\\033[0m"
331 private fun esc
: Char do return 27.ascii
332 private fun red
: String do return add_escape_char
("{esc}[1;31m")
333 private fun yellow
: String do return add_escape_char
("{esc}[1;33m")
334 private fun green
: String do return add_escape_char
("{esc}[1;32m")
335 private fun blue
: String do return add_escape_char
("{esc}[1;34m")
336 private fun cyan
: String do return add_escape_char
("{esc}[1;36m")
337 private fun gray
: String do return add_escape_char
("{esc}[30;1m")
338 private fun bold
: String do return add_escape_char
("{esc}[1m")
339 private fun underline
: String do return add_escape_char
("{esc}[4m")
341 private fun escape
: String
347 else if c
== '\0' then
349 else if c
== '"' then
351 else if c == '\\' then
353 else if c == '`' then
355 else if c.ascii < 32 then
356 b.append("\\{c.ascii.to_base(8, false)}")
365 # Create a tool context to handle options and paths
366 var toolcontext = new ToolContext
367 toolcontext.process_options
369 # Here we launch the nit index
370 var ni = new NitIndex(toolcontext)
374 # TODO lister les methods qui retournent un certain type
375 # TODO lister les methods qui utilisent un certain type
376 # TODO lister les sous-types connus d'une
type
377 # TODO sorter par ordre alphabétique