5d693b134253bbcb9afba407190828db4204ed34
[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 `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 MProject
162 redef var cs_modifiers = ["project"]
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 `mproject`.
175 # * Else `mproject::self`.
176 redef fun cs_namespace do
177 var tpl = new FlatBuffer
178 tpl.append mproject.cs_namespace
179 if mproject.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 mdoc_or_fallback do return intro.mdoc
212 redef fun cs_icon do return intro.cs_icon
213
214 # Format: `Foo[E]`
215 redef var cs_name is lazy do
216 var tpl = new FlatBuffer
217 tpl.append name
218 if arity > 0 then
219 tpl.append "["
220 var parameter_names = new Array[String]
221 for p in mparameters do
222 parameter_names.add(p.cs_name)
223 end
224 tpl.append parameter_names.join(", ")
225 tpl.append "]"
226 end
227 return tpl.write_to_string
228 end
229
230 redef fun cs_modifiers do return intro.cs_modifiers
231 redef fun cs_declaration do return intro.cs_declaration
232
233 # Returns `mproject::self`.
234 redef fun cs_namespace do
235 var tpl = new FlatBuffer
236 tpl.append intro_mmodule.mgroup.mproject.cs_namespace
237 tpl.append "::"
238 tpl.append cs_name
239 return tpl.write_to_string
240 end
241
242 # Returns `intro.cs_short_signature`.
243 fun cs_short_signature: String do return intro.cs_short_signature
244
245 # Returns `intro.cs_signature`.
246 fun cs_signature: String do return intro.cs_signature
247
248 redef fun cs_visibility_color(string) do
249 if visibility == private_visibility then
250 return string.red
251 else if visibility == protected_visibility then
252 return string.yellow
253 end
254 return string.green
255 end
256
257 redef fun cs_location do return intro.location.to_s
258 end
259
260 redef class MClassDef
261 redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
262 redef fun cs_icon do return "C"
263
264 # Depends if `self` is an intro or not.
265 #
266 # * If intro contains the visibility and kind.
267 # * If redef contains the `redef` keyword and kind.
268 redef fun cs_modifiers do
269 var res = new Array[String]
270 if not is_intro then
271 res.add "redef"
272 else
273 if mclass.visibility != public_visibility then
274 res.add mclass.visibility.to_s
275 end
276 end
277 res.add mclass.kind.to_s
278 return res
279 end
280
281 # Depends if `self` is an intro or not.
282 #
283 # For intro: `private abstract class Foo[E: Object]`
284 # For redef: `redef class Foo[E]`
285 redef fun cs_declaration do
286 var tpl = new FlatBuffer
287 tpl.append cs_modifiers.join(" ")
288 tpl.append " "
289 tpl.append cs_name
290 if is_intro then
291 tpl.append cs_signature
292 else
293 tpl.append cs_short_signature
294 end
295 return tpl.write_to_string.write_to_string.write_to_string
296 end
297
298 # Returns `mmodule::self`
299 redef fun cs_namespace do
300 var tpl = new FlatBuffer
301 tpl.append mmodule.cs_namespace
302 tpl.append "::"
303 tpl.append mclass.cs_name
304 return tpl.write_to_string.write_to_string
305 end
306
307 # Returns the MClassDef generic signature without static bounds.
308 fun cs_short_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 if i < mparameters.length - 1 then tpl.append ", "
316 end
317 tpl.append "]"
318 end
319 return tpl.write_to_string
320 end
321
322 # Returns the MClassDef generic signature with static bounds.
323 fun cs_signature: String do
324 var tpl = new FlatBuffer
325 var mparameters = mclass.mparameters
326 if not mparameters.is_empty then
327 tpl.append "["
328 for i in [0..mparameters.length[ do
329 tpl.append "{mparameters[i].cs_name}: "
330 tpl.append bound_mtype.arguments[i].cs_signature
331 if i < mparameters.length - 1 then tpl.append ", "
332 end
333 tpl.append "]"
334 end
335 return tpl.write_to_string
336 end
337
338 redef fun cs_location do return location.to_s
339 end
340
341 redef class MProperty
342 redef fun mdoc_or_fallback do return intro.mdoc
343 redef fun cs_modifiers do return intro.cs_modifiers
344 redef fun cs_declaration do return intro.cs_declaration
345 redef fun cs_icon do return intro.cs_icon
346
347 # Returns `mclass::self`.
348 redef fun cs_namespace do
349 var tpl = new FlatBuffer
350 tpl.append intro_mclassdef.mclass.cs_namespace
351 tpl.append "::"
352 tpl.append intro.cs_name
353 return tpl.write_to_string
354 end
355
356 # Returns `intro.cs_short_signature`.
357 fun cs_short_signature: String do return intro.cs_short_signature
358
359 # Returns `intro.cs_signature`.
360 fun cs_signature: String do return intro.cs_signature
361
362 redef fun cs_visibility_color(string) do
363 if visibility == private_visibility then
364 return string.red
365 else if visibility == protected_visibility then
366 return string.yellow
367 end
368 return string.green
369 end
370
371 # Returns `intro.cs_location`.
372 redef fun cs_location do return intro.location.to_s
373 end
374
375 redef class MPropDef
376 redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
377
378 # Depends if `self` is an intro or not.
379 #
380 # * If intro contains the visibility and kind.
381 # * If redef contains the `redef` keyword and kind.
382 redef fun cs_modifiers do
383 var res = new Array[String]
384 if not is_intro then
385 res.add "redef"
386 else
387 if mproperty.visibility != public_visibility then
388 res.add mproperty.visibility.to_s
389 end
390 end
391 return res
392 end
393
394 # Depends if `self` is an intro or not.
395 #
396 # For intro: `private fun foo(e: Object): Bar is abstract`
397 # For redef: `redef fun foo(e) is cached`
398 redef fun cs_declaration do
399 var tpl = new FlatBuffer
400 tpl.append cs_modifiers.join(" ")
401 tpl.append " "
402 if is_intro then
403 tpl.append cs_name
404 tpl.append cs_signature
405 else
406 tpl.append mproperty.intro.cs_name
407 tpl.append cs_short_signature
408 end
409 return tpl.write_to_string
410 end
411
412 # Returns `mclassdef::self`
413 redef fun cs_namespace do
414 var tpl = new FlatBuffer
415 tpl.append mclassdef.cs_namespace
416 tpl.append "::"
417 tpl.append cs_name
418 return tpl.write_to_string
419 end
420
421 redef fun cs_location do return location.to_s
422
423 # Returns the MPropdDef signature without static types.
424 fun cs_short_signature: String is abstract
425
426 # Returns the MPropDef signature with static types.
427 fun cs_signature: String is abstract
428 end
429
430 redef class MAttributeDef
431
432 redef fun cs_modifiers do
433 var res = super
434 res.add "var"
435 return res
436 end
437
438 redef fun cs_short_signature do return ""
439
440 redef fun cs_signature do
441 var tpl = new FlatBuffer
442 if static_mtype != null then
443 tpl.append ": "
444 tpl.append static_mtype.cs_signature
445 end
446 return tpl.write_to_string
447 end
448
449 redef fun cs_icon do return "A"
450 end
451
452 redef class MMethodDef
453
454 redef fun cs_modifiers do
455 if mproperty.is_init then
456 var res = new Array[String]
457 if mproperty.visibility != public_visibility then
458 res.add mproperty.visibility.to_s
459 end
460 return res
461 end
462 var res = super
463 if is_abstract then
464 res.add "abstract"
465 else if is_intern then
466 res.add "intern"
467 end
468 res.add "fun"
469 return res
470 end
471
472 redef fun cs_declaration do
473 if mproperty.is_init then
474 var tpl = new FlatBuffer
475 if not cs_modifiers.is_empty then
476 tpl.append cs_modifiers.join(" ")
477 tpl.append " "
478 end
479 tpl.append cs_name
480 tpl.append cs_signature
481 return tpl.write_to_string
482 end
483 return super
484 end
485
486 redef fun cs_short_signature do
487 if mproperty.is_root_init then
488 return new_msignature.cs_short_signature
489 end
490 return msignature.cs_short_signature
491 end
492
493 redef fun cs_signature do
494 if mproperty.is_root_init then
495 return new_msignature.cs_signature
496 end
497 return msignature.cs_signature
498 end
499
500 redef fun cs_icon do
501 if mproperty.is_init then
502 return "I"
503 end
504 return "F"
505 end
506 end
507
508 redef class MVirtualTypeDef
509
510 redef fun cs_modifiers do
511 var res = super
512 res.add "type"
513 return res
514 end
515
516 # Short signature for `MVirtualType` is always empty.
517 redef fun cs_short_signature do return ""
518
519 redef fun cs_signature do
520 var tpl = new FlatBuffer
521 if bound == null then return tpl.write_to_string
522 tpl.append ": "
523 tpl.append bound.cs_signature
524 return tpl.write_to_string
525 end
526 redef fun cs_icon do return "V"
527 end
528
529 redef class MType
530 # Returns the signature of this type whithout bounds.
531 fun cs_short_signature: String is abstract
532
533 # Returns the signature of this type.
534 fun cs_signature: String is abstract
535
536 redef fun cs_icon do return "T"
537 end
538
539 redef class MClassType
540 redef fun cs_short_signature do return cs_name
541 redef fun cs_signature do return cs_name
542 end
543
544 redef class MNullableType
545
546 redef fun cs_short_signature do
547 var tpl = new FlatBuffer
548 tpl.append "nullable "
549 tpl.append mtype.cs_short_signature
550 return tpl.write_to_string
551 end
552
553 redef fun cs_signature do
554 var tpl = new FlatBuffer
555 tpl.append "nullable "
556 tpl.append mtype.cs_signature
557 return tpl.write_to_string
558 end
559 end
560
561 redef class MGenericType
562 redef fun cs_short_signature do
563 var tpl = new FlatBuffer
564 tpl.append name
565 tpl.append "["
566 for i in [0..arguments.length[ do
567 tpl.append arguments[i].cs_short_signature
568 if i < arguments.length - 1 then tpl.append ", "
569 end
570 tpl.append "]"
571 return tpl.write_to_string
572 end
573
574 redef fun cs_signature do
575 var tpl = new FlatBuffer
576 tpl.append mclass.name
577 tpl.append "["
578 for i in [0..arguments.length[ do
579 tpl.append arguments[i].cs_signature
580 if i < arguments.length - 1 then tpl.append ", "
581 end
582 tpl.append "]"
583 return tpl.write_to_string
584 end
585 end
586
587 redef class MParameterType
588 redef fun cs_short_signature do return cs_name
589 redef fun cs_signature do return cs_name
590 end
591
592 redef class MVirtualType
593 redef fun cs_signature do return cs_name
594 end
595
596 redef class MSignature
597
598 redef fun cs_short_signature do
599 var tpl = new FlatBuffer
600 if not mparameters.is_empty then
601 tpl.append "("
602 for i in [0..mparameters.length[ do
603 tpl.append mparameters[i].cs_short_signature
604 if i < mparameters.length - 1 then tpl.append ", "
605 end
606 tpl.append ")"
607 end
608 return tpl.write_to_string
609 end
610
611 redef fun cs_signature do
612 var tpl = new FlatBuffer
613 if not mparameters.is_empty then
614 tpl.append "("
615 for i in [0..mparameters.length[ do
616 tpl.append mparameters[i].cs_signature
617 if i < mparameters.length - 1 then tpl.append ", "
618 end
619 tpl.append ")"
620 end
621 if return_mtype != null then
622 tpl.append ": "
623 tpl.append return_mtype.cs_signature
624 end
625 return tpl.write_to_string
626 end
627 end
628
629 redef class MParameter
630
631 # Returns `self` name and ellipsys if any.
632 fun cs_short_signature: String do
633 var tpl = new FlatBuffer
634 tpl.append name
635 if is_vararg then tpl.append "..."
636 return tpl.write_to_string
637 end
638
639 # Returns `self` name with it's static type and ellipsys if any.
640 fun cs_signature: String do
641 var tpl = new FlatBuffer
642 tpl.append "{name}: "
643 tpl.append mtype.cs_signature
644 if is_vararg then tpl.append "..."
645 return tpl.write_to_string
646 end
647 end
648
649 ################################################################################
650 # Additions to `model_ext`.
651
652 redef class MRawType
653 redef fun cs_signature do
654 var tpl = new FlatBuffer
655
656 for part in parts do
657 if part.target != null then
658 tpl.append part.target.as(not null).cs_name
659 else
660 tpl.append part.text
661 end
662 end
663 return tpl.write_to_string
664 end
665 end
666
667 redef class MInnerClass
668 redef fun cs_signature do return inner.cs_signature
669 end
670
671 redef class MInnerClassDef
672 redef fun cs_signature do return inner.cs_signature
673 end