ni: now search for properties by their name
[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 props_fulldoc(matches)
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
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}")
220 end
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)
226 end
227 end
228 end
229 pager.add_rule
230 end
231 pager.render
232 end
233 end
234
235 # Printing facilities
236
237 redef class MClass
238
239 redef fun to_s: String do
240 if arity > 0 then
241 return "{name}[{intro.parameter_names.join(", ")}]"
242 else
243 return name
244 end
245 end
246
247 private fun short_doc: String do
248 var ret = ""
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(", ")}"
257 return ret
258 end
259 end
260
261 redef class MClassDef
262 private fun namespace: String do
263 return "{mmodule.full_name}::{mclass.name}"
264 end
265 end
266
267 redef class AModule
268 private fun comment: String do
269 var ret = ""
270 for t in n_moduledecl.n_doc.n_comment do
271 ret += "{t.text.replace("# ", "")}"
272 end
273 return ret
274 end
275 end
276
277 redef class AStdClassdef
278 private fun comment: String do
279 var ret = ""
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("#", "")
284 ret += "{txt}"
285 end
286 end
287 return ret
288 end
289
290 private fun short_comment: String do
291 var ret = ""
292 if n_doc != null then
293 var txt = n_doc.n_comment.first.text
294 txt = txt.replace("# ", "")
295 txt = txt.replace("\n", "")
296 ret += txt
297 end
298 return ret
299 end
300 end
301
302 redef class AMethPropdef
303 private fun short_comment: String do
304 var ret = ""
305 if n_doc != null then
306 var txt = n_doc.n_comment.first.text
307 txt = txt.replace("# ", "")
308 txt = txt.replace("\n", "")
309 ret += txt
310 end
311 return ret
312 end
313
314 redef fun to_s do
315 var ret = ""
316 if not mpropdef.mproperty.is_init then
317 ret = "fun "
318 end
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"
327 return ret
328 end
329 end
330
331 redef class ASignature
332 redef fun to_s do
333 #TODO closures
334 var ret = ""
335 if not n_params.is_empty then
336 ret = "{ret}({n_params.join(", ")})"
337 end
338 if n_type != null then ret += ": {n_type.to_s}"
339 return ret
340 end
341 end
342
343 redef class AParam
344 redef fun to_s do
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}..."
349 end
350 return ret
351 end
352 end
353
354 redef class AType
355 redef fun to_s do
356 var ret = n_id.text
357 if n_kwnullable != null then ret = "nullable {ret}"
358 if not n_types.is_empty then ret = "{ret}[{n_types.join(", ")}]"
359 return ret
360 end
361 end
362
363 # Redef String class to add a function to color the string
364 redef class String
365
366 private fun add_escape_char(escapechar: String): String do
367 return "{escapechar}{self}\\033[0m"
368 end
369
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")
379
380 private fun escape: String
381 do
382 var b = new Buffer
383 for c in self do
384 if c == '\n' then
385 b.append("\\n")
386 else if c == '\0' then
387 b.append("\\0")
388 else if c == '"' then
389 b.append("\\\"")
390 else if c == '\\' then
391 b.append("\\\\")
392 else if c == '`' then
393 b.append("'")
394 else if c.ascii < 32 then
395 b.append("\\{c.ascii.to_base(8, false)}")
396 else
397 b.add(c)
398 end
399 end
400 return b.to_s
401 end
402 end
403
404 # Create a tool context to handle options and paths
405 var toolcontext = new ToolContext
406 toolcontext.process_options
407
408 # Here we launch the nit index
409 var ni = new NitIndex(toolcontext)
410 ni.start
411
412 # TODO seek methods
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