nitx: introduce code search
[nit.git] / src / doc / console_templates / console_model.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Console templates for Nit model MEntities.
16 #
17 # This module introduces console rendering services in model entities.
18 module console_model
19
20 import console
21 import doc_base
22 import ordered_tree
23
24 redef class MDoc
25 # Returns the full comment formatted for the console.
26 fun cs_comment: String do
27 var res = new FlatBuffer
28 for line in content do
29 res.append " {line}\n"
30 end
31 return res.write_to_string
32 end
33
34 # Returns the synopsys formatted for the console.
35 fun cs_short_comment: String do return content.first
36 end
37
38 redef class MEntity
39
40 # Returns the mentity name with short signature.
41 #
42 # * MProject: `foo`
43 # * MGroup: `foo`
44 # * MModule: `foo`
45 # * MClass: `Foo[E]`
46 # * MClassDef: `Foo[E]`
47 # * MProperty: `foo(e)`
48 # * MPropdef: `foo(e)`
49 var cs_name: String is lazy do return name
50
51 # Returns the list of keyword used in `self` declaration.
52 fun cs_modifiers: Array[String] is abstract
53
54 # Returns the complete MEntity declaration (modifiers + name + signature).
55 #
56 # * MProject: `project foo`
57 # * MGroup: `group foo`
58 # * MModule: `module foo`
59 # * MClass: `private abstract class Foo[E: Object]`
60 # * MClassDef: `redef class Foo[E]`
61 # * MProperty: `private fun foo(e: Object): Int`
62 # * MPropdef: `redef fun foo(e)`
63 var cs_declaration: String is lazy do
64 var tpl = new FlatBuffer
65 tpl.append cs_modifiers.join(" ")
66 tpl.append " "
67 tpl.append cs_name
68 return tpl.write_to_string
69 end
70
71 # Returns `self` namespace formatted for console.
72 #
73 # * MProject: `mproject`
74 # * MGroup: `mproject(::group)`
75 # * MModule: `mgroup::mmodule`
76 # * MClass: `mproject::mclass`
77 # * MClassDef: `mmodule::mclassdef`
78 # * MProperty: `mclass::mprop`
79 # * MPropdef: `mclassdef:mpropdef`
80 fun cs_namespace: String is abstract
81
82 # Returns the comment of this MEntity formatted for console.
83 var cs_comment: nullable String is lazy do
84 var mdoc = mdoc_or_fallback
85 if mdoc == null then return null
86 # FIXME add markdown for console
87 return mdoc.cs_comment
88 end
89
90 # Returns the comment of this MEntity formatted for console.
91 var cs_short_comment: nullable String is lazy do
92 var mdoc = mdoc_or_fallback
93 if mdoc == null then return null
94 return mdoc.cs_short_comment
95 end
96
97 # Returns 1self` as a list element that can be displayed in console.
98 fun cs_list_item: String do
99 var tpl = new FlatBuffer
100 tpl.append " {cs_visibility_color(cs_icon).bold} {cs_name.blue.bold}"
101 var comment = cs_short_comment
102 if comment != null then
103 tpl.append " # {comment}".green
104 end
105 tpl.append "\n "
106 tpl.append cs_namespace.gray.bold
107 tpl.append "\n "
108 tpl.append cs_declaration
109 tpl.append "\n "
110 tpl.append cs_location.gray
111 return tpl.write_to_string
112 end
113
114 # ASCII icon to be displayed in front of the list item.
115 fun cs_icon: String do return "*"
116
117 # Source code location of this MEntity formatted for console.
118 fun cs_location: String is abstract
119
120 # Sets text color depending on visibility.
121 #
122 # See module `console`.
123 fun cs_visibility_color(string: String): String do return string.green
124
125 # Source code associated to this MEntity.
126 #
127 # Uses `cs_location` to locate the source code.
128 fun cs_source_code: String do
129 # FIXME up location to mentity
130 var loc = new Location.from_string(cs_location)
131 var fr = new FileReader.open(loc.file.filename)
132 var content = new FlatBuffer
133 var i = 0
134 while not fr.eof do
135 i += 1
136 var line = fr.read_line
137 if i < loc.line_start or i > loc.line_end then continue
138 # FIXME add nitlight for console
139 content.append "{line}\n"
140 end
141 fr.close
142 return content.write_to_string
143 end
144 end
145
146 redef class MProject
147 redef var cs_modifiers = ["project"]
148 redef fun cs_namespace do return cs_name
149 redef fun cs_icon do return "P"
150 redef fun cs_location do return root.mmodules.first.location.to_s
151 end
152
153 redef class MGroup
154 redef var cs_modifiers = ["group"]
155 redef fun cs_icon do return "G"
156
157 # Depends if `self` is root or not.
158 #
159 # * If root `mproject`.
160 # * Else `mproject::self`.
161 redef fun cs_namespace do
162 var tpl = new FlatBuffer
163 tpl.append mproject.cs_namespace
164 if mproject.root != self then
165 tpl.append "::"
166 tpl.append cs_name
167 end
168 return tpl.write_to_string
169 end
170
171 redef fun cs_location do return mmodules.first.location.to_s
172 end
173
174 redef class MModule
175 redef var cs_modifiers = ["module"]
176 redef fun cs_icon do return "M"
177
178 # Depends if `self` belongs to a MGroup.
179 #
180 # * If mgroup `mgroup::self`.
181 # * Else `self`.
182 redef fun cs_namespace do
183 var tpl = new FlatBuffer
184 if mgroup != null then
185 tpl.append mgroup.cs_namespace
186 tpl.append "::"
187 end
188 tpl.append cs_name
189 return tpl.write_to_string
190 end
191
192 redef fun cs_location do return location.to_s
193 end
194
195 redef class MClass
196 redef fun mdoc_or_fallback do return intro.mdoc
197 redef fun cs_icon do return intro.cs_icon
198
199 # Format: `Foo[E]`
200 redef var cs_name is lazy do
201 var tpl = new FlatBuffer
202 tpl.append name
203 if arity > 0 then
204 tpl.append "["
205 var parameter_names = new Array[String]
206 for p in mparameters do
207 parameter_names.add(p.cs_name)
208 end
209 tpl.append parameter_names.join(", ")
210 tpl.append "]"
211 end
212 return tpl.write_to_string
213 end
214
215 redef fun cs_modifiers do return intro.cs_modifiers
216 redef fun cs_declaration do return intro.cs_declaration
217
218 # Returns `mproject::self`.
219 redef fun cs_namespace do
220 var tpl = new FlatBuffer
221 tpl.append intro_mmodule.mgroup.mproject.cs_namespace
222 tpl.append "::"
223 tpl.append cs_name
224 return tpl.write_to_string
225 end
226
227 # Returns `intro.cs_short_signature`.
228 fun cs_short_signature: String do return intro.cs_short_signature
229
230 # Returns `intro.cs_signature`.
231 fun cs_signature: String do return intro.cs_signature
232
233 redef fun cs_visibility_color(string) do
234 if visibility == private_visibility then
235 return string.red
236 else if visibility == protected_visibility then
237 return string.yellow
238 end
239 return string.green
240 end
241
242 redef fun cs_location do return intro.location.to_s
243 end
244
245 redef class MClassDef
246 redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
247 redef fun cs_icon do return "C"
248
249 # Depends if `self` is an intro or not.
250 #
251 # * If intro contains the visibility and kind.
252 # * If redef contains the `redef` keyword and kind.
253 redef fun cs_modifiers do
254 var res = new Array[String]
255 if not is_intro then
256 res.add "redef"
257 else
258 if mclass.visibility != public_visibility then
259 res.add mclass.visibility.to_s
260 end
261 end
262 res.add mclass.kind.to_s
263 return res
264 end
265
266 # Depends if `self` is an intro or not.
267 #
268 # For intro: `private abstract class Foo[E: Object]`
269 # For redef: `redef class Foo[E]`
270 redef fun cs_declaration do
271 var tpl = new FlatBuffer
272 tpl.append cs_modifiers.join(" ")
273 tpl.append " "
274 tpl.append cs_name
275 if is_intro then
276 tpl.append cs_signature
277 else
278 tpl.append cs_short_signature
279 end
280 return tpl.write_to_string.write_to_string.write_to_string
281 end
282
283 # Returns `mmodule::self`
284 redef fun cs_namespace do
285 var tpl = new FlatBuffer
286 tpl.append mmodule.cs_namespace
287 tpl.append "::"
288 tpl.append mclass.cs_name
289 return tpl.write_to_string.write_to_string
290 end
291
292 # Returns the MClassDef generic signature without static bounds.
293 fun cs_short_signature: String do
294 var tpl = new FlatBuffer
295 var mparameters = mclass.mparameters
296 if not mparameters.is_empty then
297 tpl.append "["
298 for i in [0..mparameters.length[ do
299 tpl.append mparameters[i].cs_name
300 if i < mparameters.length - 1 then tpl.append ", "
301 end
302 tpl.append "]"
303 end
304 return tpl.write_to_string
305 end
306
307 # Returns the MClassDef generic signature with static bounds.
308 fun cs_signature: String do
309 var tpl = new FlatBuffer
310 var mparameters = mclass.mparameters
311 if not mparameters.is_empty then
312 tpl.append "["
313 for i in [0..mparameters.length[ do
314 tpl.append "{mparameters[i].cs_name}: "
315 tpl.append bound_mtype.arguments[i].cs_signature
316 if i < mparameters.length - 1 then tpl.append ", "
317 end
318 tpl.append "]"
319 end
320 return tpl.write_to_string
321 end
322
323 redef fun cs_location do return location.to_s
324 end
325
326 redef class MProperty
327 redef fun mdoc_or_fallback do return intro.mdoc
328 redef fun cs_modifiers do return intro.cs_modifiers
329 redef fun cs_declaration do return intro.cs_declaration
330 redef fun cs_icon do return intro.cs_icon
331
332 # Returns `mclass::self`.
333 redef fun cs_namespace do
334 var tpl = new FlatBuffer
335 tpl.append intro_mclassdef.mclass.cs_namespace
336 tpl.append "::"
337 tpl.append intro.cs_name
338 return tpl.write_to_string
339 end
340
341 # Returns `intro.cs_short_signature`.
342 fun cs_short_signature: String do return intro.cs_short_signature
343
344 # Returns `intro.cs_signature`.
345 fun cs_signature: String do return intro.cs_signature
346
347 redef fun cs_visibility_color(string) do
348 if visibility == private_visibility then
349 return string.red
350 else if visibility == protected_visibility then
351 return string.yellow
352 end
353 return string.green
354 end
355
356 # Returns `intro.cs_location`.
357 redef fun cs_location do return intro.location.to_s
358 end
359
360 redef class MPropDef
361 redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
362
363 # Depends if `self` is an intro or not.
364 #
365 # * If intro contains the visibility and kind.
366 # * If redef contains the `redef` keyword and kind.
367 redef fun cs_modifiers do
368 var res = new Array[String]
369 if not is_intro then
370 res.add "redef"
371 else
372 if mproperty.visibility != public_visibility then
373 res.add mproperty.visibility.to_s
374 end
375 end
376 return res
377 end
378
379 # Depends if `self` is an intro or not.
380 #
381 # For intro: `private fun foo(e: Object): Bar is abstract`
382 # For redef: `redef fun foo(e) is cached`
383 redef fun cs_declaration do
384 var tpl = new FlatBuffer
385 tpl.append cs_modifiers.join(" ")
386 tpl.append " "
387 if is_intro then
388 tpl.append cs_name
389 tpl.append cs_signature
390 else
391 tpl.append mproperty.intro.cs_name
392 tpl.append cs_short_signature
393 end
394 return tpl.write_to_string
395 end
396
397 # Returns `mclassdef::self`
398 redef fun cs_namespace do
399 var tpl = new FlatBuffer
400 tpl.append mclassdef.cs_namespace
401 tpl.append "::"
402 tpl.append cs_name
403 return tpl.write_to_string
404 end
405
406 redef fun cs_location do return location.to_s
407
408 # Returns the MPropdDef signature without static types.
409 fun cs_short_signature: String is abstract
410
411 # Returns the MPropDef signature with static types.
412 fun cs_signature: String is abstract
413 end
414
415 redef class MAttributeDef
416
417 redef fun cs_modifiers do
418 var res = super
419 res.add "var"
420 return res
421 end
422
423 redef fun cs_short_signature do return ""
424
425 redef fun cs_signature do
426 var tpl = new FlatBuffer
427 if static_mtype != null then
428 tpl.append ": "
429 tpl.append static_mtype.cs_signature
430 end
431 return tpl.write_to_string
432 end
433
434 redef fun cs_icon do return "A"
435 end
436
437 redef class MMethodDef
438
439 redef fun cs_modifiers do
440 if mproperty.is_init then
441 var res = new Array[String]
442 if mproperty.visibility != public_visibility then
443 res.add mproperty.visibility.to_s
444 end
445 return res
446 end
447 var res = super
448 if is_abstract then
449 res.add "abstract"
450 else if is_intern then
451 res.add "intern"
452 end
453 res.add "fun"
454 return res
455 end
456
457 redef fun cs_declaration do
458 if mproperty.is_init then
459 var tpl = new FlatBuffer
460 if not cs_modifiers.is_empty then
461 tpl.append cs_modifiers.join(" ")
462 tpl.append " "
463 end
464 tpl.append cs_name
465 tpl.append cs_signature
466 return tpl.write_to_string
467 end
468 return super
469 end
470
471 redef fun cs_short_signature do
472 if mproperty.is_root_init then
473 return new_msignature.cs_short_signature
474 end
475 return msignature.cs_short_signature
476 end
477
478 redef fun cs_signature do
479 if mproperty.is_root_init then
480 return new_msignature.cs_signature
481 end
482 return msignature.cs_signature
483 end
484
485 redef fun cs_icon do
486 if mproperty.is_init then
487 return "I"
488 end
489 return "F"
490 end
491 end
492
493 redef class MVirtualTypeDef
494
495 redef fun cs_modifiers do
496 var res = super
497 res.add "type"
498 return res
499 end
500
501 # Short signature for `MVirtualType` is always empty.
502 redef fun cs_short_signature do return ""
503
504 redef fun cs_signature do
505 var tpl = new FlatBuffer
506 if bound == null then return tpl.write_to_string
507 tpl.append ": "
508 tpl.append bound.cs_signature
509 return tpl.write_to_string
510 end
511 redef fun cs_icon do return "V"
512 end
513
514 redef class MType
515 # Returns the signature of this type whithout bounds.
516 fun cs_short_signature: String is abstract
517
518 # Returns the signature of this type.
519 fun cs_signature: String is abstract
520
521 redef fun cs_icon do return "T"
522 end
523
524 redef class MClassType
525 redef fun cs_short_signature do return cs_name
526 redef fun cs_signature do return cs_name
527 end
528
529 redef class MNullableType
530
531 redef fun cs_short_signature do
532 var tpl = new FlatBuffer
533 tpl.append "nullable "
534 tpl.append mtype.cs_short_signature
535 return tpl.write_to_string
536 end
537
538 redef fun cs_signature do
539 var tpl = new FlatBuffer
540 tpl.append "nullable "
541 tpl.append mtype.cs_signature
542 return tpl.write_to_string
543 end
544 end
545
546 redef class MGenericType
547 redef fun cs_short_signature do
548 var tpl = new FlatBuffer
549 tpl.append name
550 tpl.append "["
551 for i in [0..arguments.length[ do
552 tpl.append arguments[i].cs_short_signature
553 if i < arguments.length - 1 then tpl.append ", "
554 end
555 tpl.append "]"
556 return tpl.write_to_string
557 end
558
559 redef fun cs_signature do
560 var tpl = new FlatBuffer
561 tpl.append mclass.name
562 tpl.append "["
563 for i in [0..arguments.length[ do
564 tpl.append arguments[i].cs_signature
565 if i < arguments.length - 1 then tpl.append ", "
566 end
567 tpl.append "]"
568 return tpl.write_to_string
569 end
570 end
571
572 redef class MParameterType
573 redef fun cs_short_signature do return cs_name
574 redef fun cs_signature do return cs_name
575 end
576
577 redef class MVirtualType
578 redef fun cs_signature do return cs_name
579 end
580
581 redef class MSignature
582
583 redef fun cs_short_signature do
584 var tpl = new FlatBuffer
585 if not mparameters.is_empty then
586 tpl.append "("
587 for i in [0..mparameters.length[ do
588 tpl.append mparameters[i].cs_short_signature
589 if i < mparameters.length - 1 then tpl.append ", "
590 end
591 tpl.append ")"
592 end
593 return tpl.write_to_string
594 end
595
596 redef fun cs_signature do
597 var tpl = new FlatBuffer
598 if not mparameters.is_empty then
599 tpl.append "("
600 for i in [0..mparameters.length[ do
601 tpl.append mparameters[i].cs_signature
602 if i < mparameters.length - 1 then tpl.append ", "
603 end
604 tpl.append ")"
605 end
606 if return_mtype != null then
607 tpl.append ": "
608 tpl.append return_mtype.cs_signature
609 end
610 return tpl.write_to_string
611 end
612 end
613
614 redef class MParameter
615
616 # Returns `self` name and ellipsys if any.
617 fun cs_short_signature: String do
618 var tpl = new FlatBuffer
619 tpl.append name
620 if is_vararg then tpl.append "..."
621 return tpl.write_to_string
622 end
623
624 # Returns `self` name with it's static type and ellipsys if any.
625 fun cs_signature: String do
626 var tpl = new FlatBuffer
627 tpl.append "{name}: "
628 tpl.append mtype.cs_signature
629 if is_vararg then tpl.append "..."
630 return tpl.write_to_string
631 end
632 end
633
634 ################################################################################
635 # Additions to `model_ext`.
636
637 redef class MRawType
638 redef fun cs_signature do
639 var tpl = new FlatBuffer
640
641 for part in parts do
642 if part.target != null then
643 tpl.append part.target.as(not null).cs_name
644 else
645 tpl.append part.text
646 end
647 end
648 return tpl.write_to_string
649 end
650 end
651
652 redef class MInnerClass
653 redef fun cs_signature do return inner.cs_signature
654 end
655
656 redef class MInnerClassDef
657 redef fun cs_signature do return inner.cs_signature
658 end