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