Merge: nitweb: use doc-commands
[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 # * MPackage: `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 # * MPackage: `package 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 # * MPackage: `mpackage`
74 # * MGroup: `mpackage(::group)`
75 # * MModule: `mgroup::mmodule`
76 # * MClass: `mpackage::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 `self` as a list element that can be displayed in console.
98 #
99 # Displays `cs_icon`, `cs_name`, `cs_short_comment, `cs_namespace`,
100 # `cs_declaration` and `cs_location`.
101 fun cs_list_item: String do
102 var tpl = new FlatBuffer
103 tpl.append " {cs_visibility_color(cs_icon).bold} {cs_name.blue.bold}"
104 var comment = cs_short_comment
105 if comment != null then
106 tpl.append " # {comment}".green
107 end
108 tpl.append "\n "
109 tpl.append cs_namespace.gray.bold
110 tpl.append "\n "
111 tpl.append cs_declaration
112 tpl.append "\n "
113 tpl.append cs_location.gray
114 return tpl.write_to_string
115 end
116
117 # Returns `self` as a short list element that can be displayed in console.
118 # Displays `cs_icon`, `cs_name`, and `cs_short_comment.
119 fun cs_short_list_item: String do
120 var tpl = new FlatBuffer
121 tpl.append " {cs_visibility_color(cs_icon).bold} {cs_name.blue.bold}"
122 var comment = cs_short_comment
123 if comment != null then
124 tpl.append " # {comment}".green
125 end
126 return tpl.write_to_string
127 end
128
129 # ASCII icon to be displayed in front of the list item.
130 fun cs_icon: String do return "*"
131
132 # Source code location of this MEntity formatted for console.
133 fun cs_location: String is abstract
134
135 # Sets text color depending on visibility.
136 #
137 # See module `console`.
138 fun cs_visibility_color(string: String): String do return string.green
139
140 # Source code associated to this MEntity.
141 #
142 # Uses `cs_location` to locate the source code.
143 fun cs_source_code: String do
144 # FIXME up location to mentity
145 var loc = new Location.from_string(cs_location)
146 var fr = new FileReader.open(loc.file.filename)
147 var content = new FlatBuffer
148 var i = 0
149 while not fr.eof do
150 i += 1
151 var line = fr.read_line
152 if i < loc.line_start or i > loc.line_end then continue
153 # FIXME add nitlight for console
154 content.append "{line}\n"
155 end
156 fr.close
157 return content.write_to_string
158 end
159 end
160
161 redef class MPackage
162 redef var cs_modifiers = ["package"]
163 redef fun cs_namespace do return cs_name
164 redef fun cs_icon do return "P"
165 redef fun cs_location do return root.mmodules.first.location.to_s
166 end
167
168 redef class MGroup
169 redef var cs_modifiers = ["group"]
170 redef fun cs_icon do return "G"
171
172 # Depends if `self` is root or not.
173 #
174 # * If root `mpackage`.
175 # * Else `mpackage::self`.
176 redef fun cs_namespace do
177 var tpl = new FlatBuffer
178 tpl.append mpackage.cs_namespace
179 if mpackage.root != self then
180 tpl.append "::"
181 tpl.append cs_name
182 end
183 return tpl.write_to_string
184 end
185
186 redef fun cs_location do return mmodules.first.location.to_s
187 end
188
189 redef class MModule
190 redef var cs_modifiers = ["module"]
191 redef fun cs_icon do return "M"
192
193 # Depends if `self` belongs to a MGroup.
194 #
195 # * If mgroup `mgroup::self`.
196 # * Else `self`.
197 redef fun cs_namespace do
198 var tpl = new FlatBuffer
199 if mgroup != null then
200 tpl.append mgroup.cs_namespace
201 tpl.append "::"
202 end
203 tpl.append cs_name
204 return tpl.write_to_string
205 end
206
207 redef fun cs_location do return location.to_s
208 end
209
210 redef class MClass
211 redef fun cs_icon do return intro.cs_icon
212
213 # Format: `Foo[E]`
214 redef var cs_name is lazy do
215 var tpl = new FlatBuffer
216 tpl.append name
217 if arity > 0 then
218 tpl.append "["
219 var parameter_names = new Array[String]
220 for p in mparameters do
221 parameter_names.add(p.cs_name)
222 end
223 tpl.append parameter_names.join(", ")
224 tpl.append "]"
225 end
226 return tpl.write_to_string
227 end
228
229 redef fun cs_modifiers do return intro.cs_modifiers
230 redef fun cs_declaration do return intro.cs_declaration
231
232 # Returns `mpackage::self`.
233 redef fun cs_namespace do
234 var tpl = new FlatBuffer
235 tpl.append intro_mmodule.mgroup.mpackage.cs_namespace
236 tpl.append "::"
237 tpl.append cs_name
238 return tpl.write_to_string
239 end
240
241 # Returns `intro.cs_short_signature`.
242 fun cs_short_signature: String do return intro.cs_short_signature
243
244 # Returns `intro.cs_signature`.
245 fun cs_signature: String do return intro.cs_signature
246
247 redef fun cs_visibility_color(string) do
248 if visibility == private_visibility then
249 return string.red
250 else if visibility == protected_visibility then
251 return string.yellow
252 end
253 return string.green
254 end
255
256 redef fun cs_location do return intro.location.to_s
257 end
258
259 redef class MClassDef
260 redef fun cs_icon do return "C"
261
262 # Depends if `self` is an intro or not.
263 #
264 # * If intro contains the visibility and kind.
265 # * If redef contains the `redef` keyword and kind.
266 redef fun cs_modifiers do
267 var res = new Array[String]
268 if not is_intro then
269 res.add "redef"
270 else
271 if mclass.visibility != public_visibility then
272 res.add mclass.visibility.to_s
273 end
274 end
275 res.add mclass.kind.to_s
276 return res
277 end
278
279 # Depends if `self` is an intro or not.
280 #
281 # For intro: `private abstract class Foo[E: Object]`
282 # For redef: `redef class Foo[E]`
283 redef fun cs_declaration do
284 var tpl = new FlatBuffer
285 tpl.append cs_modifiers.join(" ")
286 tpl.append " "
287 tpl.append cs_name
288 if is_intro then
289 tpl.append cs_signature
290 else
291 tpl.append cs_short_signature
292 end
293 return tpl.write_to_string.write_to_string.write_to_string
294 end
295
296 # Returns `mmodule::self`
297 redef fun cs_namespace do
298 var tpl = new FlatBuffer
299 tpl.append mmodule.cs_namespace
300 tpl.append "::"
301 tpl.append mclass.cs_name
302 return tpl.write_to_string.write_to_string
303 end
304
305 # Returns the MClassDef generic signature without static bounds.
306 fun cs_short_signature: String do
307 var tpl = new FlatBuffer
308 var mparameters = mclass.mparameters
309 if not mparameters.is_empty then
310 tpl.append "["
311 for i in [0..mparameters.length[ do
312 tpl.append mparameters[i].cs_name
313 if i < mparameters.length - 1 then tpl.append ", "
314 end
315 tpl.append "]"
316 end
317 return tpl.write_to_string
318 end
319
320 # Returns the MClassDef generic signature with static bounds.
321 fun cs_signature: String do
322 var tpl = new FlatBuffer
323 var mparameters = mclass.mparameters
324 if not mparameters.is_empty then
325 tpl.append "["
326 for i in [0..mparameters.length[ do
327 tpl.append "{mparameters[i].cs_name}: "
328 tpl.append bound_mtype.arguments[i].cs_signature
329 if i < mparameters.length - 1 then tpl.append ", "
330 end
331 tpl.append "]"
332 end
333 return tpl.write_to_string
334 end
335
336 redef fun cs_location do return location.to_s
337 end
338
339 redef class MProperty
340 redef fun cs_modifiers do return intro.cs_modifiers
341 redef fun cs_declaration do return intro.cs_declaration
342 redef fun cs_icon do return intro.cs_icon
343
344 # Returns `mclass::self`.
345 redef fun cs_namespace do
346 var tpl = new FlatBuffer
347 tpl.append intro_mclassdef.mclass.cs_namespace
348 tpl.append "::"
349 tpl.append intro.cs_name
350 return tpl.write_to_string
351 end
352
353 # Returns `intro.cs_short_signature`.
354 fun cs_short_signature: String do return intro.cs_short_signature
355
356 # Returns `intro.cs_signature`.
357 fun cs_signature: String do return intro.cs_signature
358
359 redef fun cs_visibility_color(string) do
360 if visibility == private_visibility then
361 return string.red
362 else if visibility == protected_visibility then
363 return string.yellow
364 end
365 return string.green
366 end
367
368 # Returns `intro.cs_location`.
369 redef fun cs_location do return intro.location.to_s
370 end
371
372 redef class MPropDef
373 # Depends if `self` is an intro or not.
374 #
375 # * If intro contains the visibility and kind.
376 # * If redef contains the `redef` keyword and kind.
377 redef fun cs_modifiers do
378 var res = new Array[String]
379 if not is_intro then
380 res.add "redef"
381 else
382 if mproperty.visibility != public_visibility then
383 res.add mproperty.visibility.to_s
384 end
385 end
386 return res
387 end
388
389 # Depends if `self` is an intro or not.
390 #
391 # For intro: `private fun foo(e: Object): Bar is abstract`
392 # For redef: `redef fun foo(e) is cached`
393 redef fun cs_declaration do
394 var tpl = new FlatBuffer
395 tpl.append cs_modifiers.join(" ")
396 tpl.append " "
397 if is_intro then
398 tpl.append cs_name
399 tpl.append cs_signature
400 else
401 tpl.append mproperty.intro.cs_name
402 tpl.append cs_short_signature
403 end
404 return tpl.write_to_string
405 end
406
407 # Returns `mclassdef::self`
408 redef fun cs_namespace do
409 var tpl = new FlatBuffer
410 tpl.append mclassdef.cs_namespace
411 tpl.append "::"
412 tpl.append cs_name
413 return tpl.write_to_string
414 end
415
416 redef fun cs_location do return location.to_s
417
418 # Returns the MPropdDef signature without static types.
419 fun cs_short_signature: String is abstract
420
421 # Returns the MPropDef signature with static types.
422 fun cs_signature: String is abstract
423 end
424
425 redef class MAttributeDef
426
427 redef fun cs_modifiers do
428 var res = super
429 res.add "var"
430 return res
431 end
432
433 redef fun cs_short_signature do return ""
434
435 redef fun cs_signature do
436 var tpl = new FlatBuffer
437 if static_mtype != null then
438 tpl.append ": "
439 tpl.append static_mtype.cs_signature
440 end
441 return tpl.write_to_string
442 end
443
444 redef fun cs_icon do return "A"
445 end
446
447 redef class MMethodDef
448
449 redef fun cs_modifiers do
450 if mproperty.is_init then
451 var res = new Array[String]
452 if mproperty.visibility != public_visibility then
453 res.add mproperty.visibility.to_s
454 end
455 return res
456 end
457 var res = super
458 if is_abstract then
459 res.add "abstract"
460 else if is_intern then
461 res.add "intern"
462 end
463 res.add "fun"
464 return res
465 end
466
467 redef fun cs_declaration do
468 if mproperty.is_init then
469 var tpl = new FlatBuffer
470 if not cs_modifiers.is_empty then
471 tpl.append cs_modifiers.join(" ")
472 tpl.append " "
473 end
474 tpl.append cs_name
475 tpl.append cs_signature
476 return tpl.write_to_string
477 end
478 return super
479 end
480
481 redef fun cs_short_signature do
482 if mproperty.is_root_init then
483 return new_msignature.cs_short_signature
484 end
485 return msignature.cs_short_signature
486 end
487
488 redef fun cs_signature do
489 if mproperty.is_root_init then
490 return new_msignature.cs_signature
491 end
492 return msignature.cs_signature
493 end
494
495 redef fun cs_icon do
496 if mproperty.is_init then
497 return "I"
498 end
499 return "F"
500 end
501 end
502
503 redef class MVirtualTypeDef
504
505 redef fun cs_modifiers do
506 var res = super
507 res.add "type"
508 return res
509 end
510
511 # Short signature for `MVirtualType` is always empty.
512 redef fun cs_short_signature do return ""
513
514 redef fun cs_signature do
515 var tpl = new FlatBuffer
516 if bound == null then return tpl.write_to_string
517 tpl.append ": "
518 tpl.append bound.cs_signature
519 return tpl.write_to_string
520 end
521 redef fun cs_icon do return "V"
522 end
523
524 redef class MType
525 # Returns the signature of this type whithout bounds.
526 fun cs_short_signature: String is abstract
527
528 # Returns the signature of this type.
529 fun cs_signature: String is abstract
530
531 redef fun cs_icon do return "T"
532 end
533
534 redef class MClassType
535 redef fun cs_short_signature do return cs_name
536 redef fun cs_signature do return cs_name
537 end
538
539 redef class MNullableType
540
541 redef fun cs_short_signature do
542 var tpl = new FlatBuffer
543 tpl.append "nullable "
544 tpl.append mtype.cs_short_signature
545 return tpl.write_to_string
546 end
547
548 redef fun cs_signature do
549 var tpl = new FlatBuffer
550 tpl.append "nullable "
551 tpl.append mtype.cs_signature
552 return tpl.write_to_string
553 end
554 end
555
556 redef class MGenericType
557 redef fun cs_short_signature do
558 var tpl = new FlatBuffer
559 tpl.append name
560 tpl.append "["
561 for i in [0..arguments.length[ do
562 tpl.append arguments[i].cs_short_signature
563 if i < arguments.length - 1 then tpl.append ", "
564 end
565 tpl.append "]"
566 return tpl.write_to_string
567 end
568
569 redef fun cs_signature do
570 var tpl = new FlatBuffer
571 tpl.append mclass.name
572 tpl.append "["
573 for i in [0..arguments.length[ do
574 tpl.append arguments[i].cs_signature
575 if i < arguments.length - 1 then tpl.append ", "
576 end
577 tpl.append "]"
578 return tpl.write_to_string
579 end
580 end
581
582 redef class MParameterType
583 redef fun cs_short_signature do return cs_name
584 redef fun cs_signature do return cs_name
585 end
586
587 redef class MVirtualType
588 redef fun cs_signature do return cs_name
589 end
590
591 redef class MSignature
592
593 redef fun cs_short_signature do
594 var tpl = new FlatBuffer
595 if not mparameters.is_empty then
596 tpl.append "("
597 for i in [0..mparameters.length[ do
598 tpl.append mparameters[i].cs_short_signature
599 if i < mparameters.length - 1 then tpl.append ", "
600 end
601 tpl.append ")"
602 end
603 return tpl.write_to_string
604 end
605
606 redef fun cs_signature do
607 var tpl = new FlatBuffer
608 if not mparameters.is_empty then
609 tpl.append "("
610 for i in [0..mparameters.length[ do
611 tpl.append mparameters[i].cs_signature
612 if i < mparameters.length - 1 then tpl.append ", "
613 end
614 tpl.append ")"
615 end
616 if return_mtype != null then
617 tpl.append ": "
618 tpl.append return_mtype.cs_signature
619 end
620 return tpl.write_to_string
621 end
622 end
623
624 redef class MParameter
625
626 # Returns `self` name and ellipsys if any.
627 fun cs_short_signature: String do
628 var tpl = new FlatBuffer
629 tpl.append name
630 if is_vararg then tpl.append "..."
631 return tpl.write_to_string
632 end
633
634 # Returns `self` name with it's static type and ellipsys if any.
635 fun cs_signature: String do
636 var tpl = new FlatBuffer
637 tpl.append "{name}: "
638 tpl.append mtype.cs_signature
639 if is_vararg then tpl.append "..."
640 return tpl.write_to_string
641 end
642 end
643
644 ################################################################################
645 # Additions to `model_ext`.
646
647 redef class MRawType
648 redef fun cs_signature do
649 var tpl = new FlatBuffer
650
651 for part in parts do
652 if part.target != null then
653 tpl.append part.target.as(not null).cs_name
654 else
655 tpl.append part.text
656 end
657 end
658 return tpl.write_to_string
659 end
660 end
661
662 redef class MInnerClass
663 redef fun cs_signature do return inner.cs_signature
664 end
665
666 redef class MInnerClassDef
667 redef fun cs_signature do return inner.cs_signature
668 end