ni: standardize doc method signatures
[nit.git] / src / ni.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 module ni
18
19 import model_utils
20
21 private class Pager
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")
27 end
28
29 class NitIndex
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]
35
36 init(toolcontext: ToolContext) do
37 # We need a model to collect stufs
38 self.toolcontext = toolcontext
39 self.arguments = toolcontext.option_context.rest
40
41 if arguments.length > 2 then
42 print "usage: ni path/to/module.nit [expression]"
43 toolcontext.option_context.usage
44 exit(1)
45 end
46
47 model = new Model
48 mbuilder = new ModelBuilder(model, toolcontext)
49
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
58 end
59
60 fun start do
61 if arguments.length == 1 then
62 welcome
63 prompt
64 else
65 seek(arguments[1])
66 end
67 end
68
69 fun welcome do
70 print "Welcome in Nit Index.\n"
71 print "Loaded modules"
72 for m in mbuilder.nmodules do
73 print " - {m.mmodule.name}"
74 end
75 print "\nEnter the module, class or property name you want to look up."
76 print "Enter a blank line to exit.\n"
77 end
78
79 fun prompt do
80 printn ">> "
81 seek(stdin.read_line)
82 end
83
84 fun seek(entry: String) do
85 # quitting?
86 if entry.is_empty then exit(0)
87
88 var flag = false
89 for m in model.mmodules do
90 if m.name == entry then
91 flag = true
92 module_fulldoc(m)
93 end
94 end
95 for c in model.mclasses do
96 if c.name == entry then
97 if not mbuilder.mclassdef2nclassdef[c.intro] isa AStdClassdef then continue
98 flag = true
99 class_fulldoc(c)
100 end
101 end
102 var matches = new List[MProperty]
103 for p in model.mproperties do
104 if p.name == entry then
105 flag = true
106 matches.add(p)
107 end
108 end
109 #if not matches.is_empty then doc_properties
110
111 if not flag then print "Nothing known about '{entry}'"
112 if arguments.length == 1 then prompt
113 end
114
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(", ")}")
120 pager.add_rule
121 pager.addn(nmodule.comment.green)
122 pager.add_rule
123
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
128
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}")
136 end
137 if cat == "refined classes" then
138 pager.add("\tredef {mclass.short_doc}")
139 else
140 pager.add("\t{mclass.short_doc}")
141 end
142 if not mclass.intro_mmodule == mmodule then
143 pager.add("\t\tintroduced in {mmodule.full_name}::{mclass}".gray)
144 end
145 pager.add("")
146 end
147 end
148 end
149 pager.add_rule
150 pager.render
151 end
152
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} ")
158 pager.add_rule
159 pager.addn(nclass.comment.green)
160 pager.add_rule
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}")
165 pager.add("")
166 end
167 end
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)
176 end
177 pager.add("")
178 end
179 end
180 pager.add_rule
181
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
187
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
192 #TODO verifier cast
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}")
196 end
197 if cat == "refined methods" then
198 pager.add("\tredef {nmethod}")
199 else
200 pager.add("\t{nmethod}")
201 end
202 if not mmethod.intro_mclassdef == mclass.intro then
203 pager.add("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray)
204 end
205 pager.add("")
206 end
207 end
208 end
209 pager.render
210 end
211 end
212
213 # Printing facilities
214
215 redef class MClass
216
217 redef fun to_s: String do
218 if arity > 0 then
219 return "{name}[{intro.parameter_names.join(", ")}]"
220 else
221 return name
222 end
223 end
224
225 private fun short_doc: String do
226 var ret = ""
227 if is_interface then ret = "interface {ret}"
228 if is_enum then ret = "enum {ret}"
229 if is_class then ret = "class {ret}"
230 if is_abstract then ret = "abstract {ret}"
231 if visibility.to_s == "public" then ret = "{ret}{to_s.green}"
232 if visibility.to_s == "private" then ret = "{ret}{to_s.red}"
233 if visibility.to_s == "protected" then ret = "{ret}{to_s.yellow}"
234 ret = "{ret} super {parents.join(", ")}"
235 return ret
236 end
237 end
238
239 redef class MClassDef
240 private fun namespace: String do
241 return "{mmodule.full_name}::{mclass.name}"
242 end
243 end
244
245 redef class AModule
246 private fun comment: String do
247 var ret = ""
248 for t in n_moduledecl.n_doc.n_comment do
249 ret += "{t.text.replace("# ", "")}"
250 end
251 return ret
252 end
253 end
254
255 redef class AStdClassdef
256 private fun comment: String do
257 var ret = ""
258 if n_doc != null then
259 for t in n_doc.n_comment do
260 var txt = t.text.replace("# ", "")
261 txt = txt.replace("#", "")
262 ret += "{txt}"
263 end
264 end
265 return ret
266 end
267
268 private fun short_comment: String do
269 var ret = ""
270 if n_doc != null then
271 var txt = n_doc.n_comment.first.text
272 txt = txt.replace("# ", "")
273 txt = txt.replace("\n", "")
274 ret += txt
275 end
276 return ret
277 end
278 end
279
280 redef class AMethPropdef
281 private fun short_comment: String do
282 var ret = ""
283 if n_doc != null then
284 var txt = n_doc.n_comment.first.text
285 txt = txt.replace("# ", "")
286 txt = txt.replace("\n", "")
287 ret += txt
288 end
289 return ret
290 end
291
292 redef fun to_s do
293 var ret = ""
294 if not mpropdef.mproperty.is_init then
295 ret = "fun "
296 end
297 if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{mpropdef.mproperty.name.green}"
298 if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{mpropdef.mproperty.name.red}"
299 if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{mpropdef.mproperty.name.yellow}"
300 if n_signature != null then ret = "{ret}{n_signature.to_s}"
301 if n_kwredef != null then ret = "redef {ret}"
302 if self isa ADeferredMethPropdef then ret = "{ret} is abstract"
303 if self isa AInternMethPropdef then ret = "{ret} is intern"
304 if self isa AExternMethPropdef then ret = "{ret} is extern"
305 return ret
306 end
307 end
308
309 redef class ASignature
310 redef fun to_s do
311 #TODO closures
312 var ret = ""
313 if not n_params.is_empty then
314 ret = "{ret}({n_params.join(", ")})"
315 end
316 if n_type != null then ret += ": {n_type.to_s}"
317 return ret
318 end
319 end
320
321 redef class AParam
322 redef fun to_s do
323 var ret = "{n_id.text}"
324 if n_type != null then
325 ret = "{ret}: {n_type.to_s}"
326 if n_dotdotdot != null then ret = "{ret}..."
327 end
328 return ret
329 end
330 end
331
332 redef class AType
333 redef fun to_s do
334 var ret = n_id.text
335 if n_kwnullable != null then ret = "nullable {ret}"
336 if not n_types.is_empty then ret = "{ret}[{n_types.join(", ")}]"
337 return ret
338 end
339 end
340
341 # Redef String class to add a function to color the string
342 redef class String
343
344 private fun add_escape_char(escapechar: String): String do
345 return "{escapechar}{self}\\033[0m"
346 end
347
348 private fun esc: Char do return 27.ascii
349 private fun red: String do return add_escape_char("{esc}[1;31m")
350 private fun yellow: String do return add_escape_char("{esc}[1;33m")
351 private fun green: String do return add_escape_char("{esc}[1;32m")
352 private fun blue: String do return add_escape_char("{esc}[1;34m")
353 private fun cyan: String do return add_escape_char("{esc}[1;36m")
354 private fun gray: String do return add_escape_char("{esc}[30;1m")
355 private fun bold: String do return add_escape_char("{esc}[1m")
356 private fun underline: String do return add_escape_char("{esc}[4m")
357
358 private fun escape: String
359 do
360 var b = new Buffer
361 for c in self do
362 if c == '\n' then
363 b.append("\\n")
364 else if c == '\0' then
365 b.append("\\0")
366 else if c == '"' then
367 b.append("\\\"")
368 else if c == '\\' then
369 b.append("\\\\")
370 else if c == '`' then
371 b.append("'")
372 else if c.ascii < 32 then
373 b.append("\\{c.ascii.to_base(8, false)}")
374 else
375 b.add(c)
376 end
377 end
378 return b.to_s
379 end
380 end
381
382 # Create a tool context to handle options and paths
383 var toolcontext = new ToolContext
384 toolcontext.process_options
385
386 # Here we launch the nit index
387 var ni = new NitIndex(toolcontext)
388 ni.start
389
390 # TODO seek methods
391 # TODO lister les methods qui retournent un certain type
392 # TODO lister les methods qui utilisent un certain type
393 # TODO lister les sous-types connus d'une type
394 # TODO sorter par ordre alphabétique