1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Console templates for Nit model MEntities.
17 # This module introduces console rendering services in model entities.
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"
31 return res
.write_to_string
34 # Returns the synopsys formatted for the console.
35 fun cs_short_comment
: String do return content
.first
40 # Returns the mentity name with short signature.
46 # * MClassDef: `Foo[E]`
47 # * MProperty: `foo(e)`
48 # * MPropdef: `foo(e)`
49 var cs_name
: String is lazy
do return name
51 # Returns the list of keyword used in `self` declaration.
52 fun cs_modifiers
: Array[String] is abstract
54 # Returns the complete MEntity declaration (modifiers + name + signature).
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
(" ")
68 return tpl
.write_to_string
71 # Returns `self` namespace formatted for console.
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
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
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
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
106 tpl
.append cs_namespace
.gray
.bold
108 tpl
.append cs_declaration
110 tpl
.append cs_location
.gray
111 return tpl
.write_to_string
114 # ASCII icon to be displayed in front of the list item.
115 fun cs_icon
: String do return "*"
117 # Source code location of this MEntity formatted for console.
118 fun cs_location
: String is abstract
120 # Sets text color depending on visibility.
122 # See module `console`.
123 fun cs_visibility_color
(string
: String): String do return string
.green
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
134 redef var cs_modifiers
= ["group"]
135 redef fun cs_icon
do return "G"
137 # Depends if `self` is root or not.
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
148 return tpl
.write_to_string
151 redef fun cs_location
do return mmodules
.first
.location
.to_s
155 redef var cs_modifiers
= ["module"]
156 redef fun cs_icon
do return "M"
158 # Depends if `self` belongs to a MGroup.
160 # * If mgroup `mgroup::self`.
162 redef fun cs_namespace
do
163 var tpl
= new FlatBuffer
164 if mgroup
!= null then
165 tpl
.append mgroup
.cs_namespace
169 return tpl
.write_to_string
172 redef fun cs_location
do return location
.to_s
176 redef fun mdoc_or_fallback
do return intro
.mdoc
177 redef fun cs_icon
do return intro
.cs_icon
180 redef var cs_name
is lazy
do
181 var tpl
= new FlatBuffer
185 var parameter_names
= new Array[String]
186 for p
in mparameters
do
187 parameter_names
.add
(p
.cs_name
)
189 tpl
.append parameter_names
.join
(", ")
192 return tpl
.write_to_string
195 redef fun cs_modifiers
do return intro
.cs_modifiers
196 redef fun cs_declaration
do return intro
.cs_declaration
198 # Returns `mproject::self`.
199 redef fun cs_namespace
do
200 var tpl
= new FlatBuffer
201 tpl
.append intro_mmodule
.mgroup
.mproject
.cs_namespace
204 return tpl
.write_to_string
207 # Returns `intro.cs_short_signature`.
208 fun cs_short_signature
: String do return intro
.cs_short_signature
210 # Returns `intro.cs_signature`.
211 fun cs_signature
: String do return intro
.cs_signature
213 redef fun cs_visibility_color
(string
) do
214 if visibility
== private_visibility
then
216 else if visibility
== protected_visibility
then
222 redef fun cs_location
do return intro
.location
.to_s
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"
229 # Depends if `self` is an intro or not.
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]
238 if mclass
.visibility
!= public_visibility
then
239 res
.add mclass
.visibility
.to_s
242 res
.add mclass
.kind
.to_s
246 # Depends if `self` is an intro or not.
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
(" ")
256 tpl
.append cs_signature
258 tpl
.append cs_short_signature
260 return tpl
.write_to_string
.write_to_string
.write_to_string
263 # Returns `mmodule::self`
264 redef fun cs_namespace
do
265 var tpl
= new FlatBuffer
266 tpl
.append mmodule
.cs_namespace
268 tpl
.append mclass
.cs_name
269 return tpl
.write_to_string
.write_to_string
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
278 for i
in [0..mparameters
.length
[ do
279 tpl
.append mparameters
[i
].cs_name
280 if i
< mparameters
.length
- 1 then tpl
.append
", "
284 return tpl
.write_to_string
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
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
", "
300 return tpl
.write_to_string
303 redef fun cs_location
do return location
.to_s
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
312 # Returns `mclass::self`.
313 redef fun cs_namespace
do
314 var tpl
= new FlatBuffer
315 tpl
.append intro_mclassdef
.mclass
.cs_namespace
317 tpl
.append intro
.cs_name
318 return tpl
.write_to_string
321 # Returns `intro.cs_short_signature`.
322 fun cs_short_signature
: String do return intro
.cs_short_signature
324 # Returns `intro.cs_signature`.
325 fun cs_signature
: String do return intro
.cs_signature
327 redef fun cs_visibility_color
(string
) do
328 if visibility
== private_visibility
then
330 else if visibility
== protected_visibility
then
336 # Returns `intro.cs_location`.
337 redef fun cs_location
do return intro
.location
.to_s
341 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
343 # Depends if `self` is an intro or not.
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]
352 if mproperty
.visibility
!= public_visibility
then
353 res
.add mproperty
.visibility
.to_s
359 # Depends if `self` is an intro or not.
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
(" ")
369 tpl
.append cs_signature
371 tpl
.append mproperty
.intro
.cs_name
372 tpl
.append cs_short_signature
374 return tpl
.write_to_string
377 # Returns `mclassdef::self`
378 redef fun cs_namespace
do
379 var tpl
= new FlatBuffer
380 tpl
.append mclassdef
.cs_namespace
383 return tpl
.write_to_string
386 redef fun cs_location
do return location
.to_s
388 # Returns the MPropdDef signature without static types.
389 fun cs_short_signature
: String is abstract
391 # Returns the MPropDef signature with static types.
392 fun cs_signature
: String is abstract
395 redef class MAttributeDef
397 redef fun cs_modifiers
do
403 redef fun cs_short_signature
do return ""
405 redef fun cs_signature
do
406 var tpl
= new FlatBuffer
407 if static_mtype
!= null then
409 tpl
.append static_mtype
.cs_signature
411 return tpl
.write_to_string
414 redef fun cs_icon
do return "A"
417 redef class MMethodDef
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
430 else if is_intern
then
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
(" ")
445 tpl
.append cs_signature
446 return tpl
.write_to_string
451 redef fun cs_short_signature
do
452 if mproperty
.is_root_init
then
453 return new_msignature
.cs_short_signature
455 return msignature
.cs_short_signature
458 redef fun cs_signature
do
459 if mproperty
.is_root_init
then
460 return new_msignature
.cs_signature
462 return msignature
.cs_signature
466 if mproperty
.is_init
then
473 redef class MVirtualTypeDef
475 redef fun cs_modifiers
do
481 # Short signature for `MVirtualType` is always empty.
482 redef fun cs_short_signature
do return ""
484 redef fun cs_signature
do
485 var tpl
= new FlatBuffer
486 if bound
== null then return tpl
.write_to_string
488 tpl
.append bound
.cs_signature
489 return tpl
.write_to_string
491 redef fun cs_icon
do return "V"
495 # Returns the signature of this type whithout bounds.
496 fun cs_short_signature
: String is abstract
498 # Returns the signature of this type.
499 fun cs_signature
: String is abstract
501 redef fun cs_icon
do return "T"
504 redef class MClassType
505 redef fun cs_short_signature
do return cs_name
506 redef fun cs_signature
do return cs_name
509 redef class MNullableType
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
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
526 redef class MGenericType
527 redef fun cs_short_signature
do
528 var tpl
= new FlatBuffer
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
", "
536 return tpl
.write_to_string
539 redef fun cs_signature
do
540 var tpl
= new FlatBuffer
541 tpl
.append mclass
.name
543 for i
in [0..arguments
.length
[ do
544 tpl
.append arguments
[i
].cs_signature
545 if i
< arguments
.length
- 1 then tpl
.append
", "
548 return tpl
.write_to_string
552 redef class MParameterType
553 redef fun cs_short_signature
do return cs_name
554 redef fun cs_signature
do return cs_name
557 redef class MVirtualType
558 redef fun cs_signature
do return cs_name
561 redef class MSignature
563 redef fun cs_short_signature
do
564 var tpl
= new FlatBuffer
565 if not mparameters
.is_empty
then
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
", "
573 return tpl
.write_to_string
576 redef fun cs_signature
do
577 var tpl
= new FlatBuffer
578 if not mparameters
.is_empty
then
580 for i
in [0..mparameters
.length
[ do
581 tpl
.append mparameters
[i
].cs_signature
582 if i
< mparameters
.length
- 1 then tpl
.append
", "
586 if return_mtype
!= null then
588 tpl
.append return_mtype
.cs_signature
590 return tpl
.write_to_string
594 redef class MParameter
596 # Returns `self` name and ellipsys if any.
597 fun cs_short_signature
: String do
598 var tpl
= new FlatBuffer
600 if is_vararg
then tpl
.append
"..."
601 return tpl
.write_to_string
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
614 ################################################################################
615 # Additions to `model_ext`.
618 redef fun cs_signature
do
619 var tpl
= new FlatBuffer
622 if part
.target
!= null then
623 tpl
.append part
.target
.as(not null).cs_name
628 return tpl
.write_to_string
632 redef class MInnerClass
633 redef fun cs_signature
do return inner
.cs_signature
636 redef class MInnerClassDef
637 redef fun cs_signature
do return inner
.cs_signature