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 # * 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
(" ")
68 return tpl
.write_to_string
71 # Returns `self` namespace formatted for console.
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
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 `self` as a list element that can be displayed in console.
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
109 tpl
.append cs_namespace
.gray
.bold
111 tpl
.append cs_declaration
113 tpl
.append cs_location
.gray
114 return tpl
.write_to_string
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
126 return tpl
.write_to_string
129 # ASCII icon to be displayed in front of the list item.
130 fun cs_icon
: String do return "*"
132 # Source code location of this MEntity formatted for console.
133 fun cs_location
: String is abstract
135 # Sets text color depending on visibility.
137 # See module `console`.
138 fun cs_visibility_color
(string
: String): String do return string
.green
140 # Source code associated to this MEntity.
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
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"
157 return content
.write_to_string
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
169 redef var cs_modifiers
= ["group"]
170 redef fun cs_icon
do return "G"
172 # Depends if `self` is root or not.
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
183 return tpl
.write_to_string
186 redef fun cs_location
do return mmodules
.first
.location
.to_s
190 redef var cs_modifiers
= ["module"]
191 redef fun cs_icon
do return "M"
193 # Depends if `self` belongs to a MGroup.
195 # * If mgroup `mgroup::self`.
197 redef fun cs_namespace
do
198 var tpl
= new FlatBuffer
199 if mgroup
!= null then
200 tpl
.append mgroup
.cs_namespace
204 return tpl
.write_to_string
207 redef fun cs_location
do return location
.to_s
211 redef fun cs_icon
do return intro
.cs_icon
214 redef var cs_name
is lazy
do
215 var tpl
= new FlatBuffer
219 var parameter_names
= new Array[String]
220 for p
in mparameters
do
221 parameter_names
.add
(p
.cs_name
)
223 tpl
.append parameter_names
.join
(", ")
226 return tpl
.write_to_string
229 redef fun cs_modifiers
do return intro
.cs_modifiers
230 redef fun cs_declaration
do return intro
.cs_declaration
232 # Returns `mpackage::self`.
233 redef fun cs_namespace
do
234 var tpl
= new FlatBuffer
235 tpl
.append intro_mmodule
.mgroup
.mpackage
.cs_namespace
238 return tpl
.write_to_string
241 # Returns `intro.cs_short_signature`.
242 fun cs_short_signature
: String do return intro
.cs_short_signature
244 # Returns `intro.cs_signature`.
245 fun cs_signature
: String do return intro
.cs_signature
247 redef fun cs_visibility_color
(string
) do
248 if visibility
== private_visibility
then
250 else if visibility
== protected_visibility
then
256 redef fun cs_location
do return intro
.location
.to_s
259 redef class MClassDef
260 redef fun cs_icon
do return "C"
262 # Depends if `self` is an intro or not.
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]
271 if mclass
.visibility
!= public_visibility
then
272 res
.add mclass
.visibility
.to_s
275 res
.add mclass
.kind
.to_s
279 # Depends if `self` is an intro or not.
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
(" ")
289 tpl
.append cs_signature
291 tpl
.append cs_short_signature
293 return tpl
.write_to_string
.write_to_string
.write_to_string
296 # Returns `mmodule::self`
297 redef fun cs_namespace
do
298 var tpl
= new FlatBuffer
299 tpl
.append mmodule
.cs_namespace
301 tpl
.append mclass
.cs_name
302 return tpl
.write_to_string
.write_to_string
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
311 for i
in [0..mparameters
.length
[ do
312 tpl
.append mparameters
[i
].cs_name
313 if i
< mparameters
.length
- 1 then tpl
.append
", "
317 return tpl
.write_to_string
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
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
", "
333 return tpl
.write_to_string
336 redef fun cs_location
do return location
.to_s
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
344 # Returns `mclass::self`.
345 redef fun cs_namespace
do
346 var tpl
= new FlatBuffer
347 tpl
.append intro_mclassdef
.mclass
.cs_namespace
349 tpl
.append intro
.cs_name
350 return tpl
.write_to_string
353 # Returns `intro.cs_short_signature`.
354 fun cs_short_signature
: String do return intro
.cs_short_signature
356 # Returns `intro.cs_signature`.
357 fun cs_signature
: String do return intro
.cs_signature
359 redef fun cs_visibility_color
(string
) do
360 if visibility
== private_visibility
then
362 else if visibility
== protected_visibility
then
368 # Returns `intro.cs_location`.
369 redef fun cs_location
do return intro
.location
.to_s
373 # Depends if `self` is an intro or not.
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]
382 if mproperty
.visibility
!= public_visibility
then
383 res
.add mproperty
.visibility
.to_s
389 # Depends if `self` is an intro or not.
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
(" ")
399 tpl
.append cs_signature
401 tpl
.append mproperty
.intro
.cs_name
402 tpl
.append cs_short_signature
404 return tpl
.write_to_string
407 # Returns `mclassdef::self`
408 redef fun cs_namespace
do
409 var tpl
= new FlatBuffer
410 tpl
.append mclassdef
.cs_namespace
413 return tpl
.write_to_string
416 redef fun cs_location
do return location
.to_s
418 # Returns the MPropdDef signature without static types.
419 fun cs_short_signature
: String is abstract
421 # Returns the MPropDef signature with static types.
422 fun cs_signature
: String is abstract
425 redef class MAttributeDef
427 redef fun cs_modifiers
do
433 redef fun cs_short_signature
do return ""
435 redef fun cs_signature
do
436 var tpl
= new FlatBuffer
437 if static_mtype
!= null then
439 tpl
.append static_mtype
.cs_signature
441 return tpl
.write_to_string
444 redef fun cs_icon
do return "A"
447 redef class MMethodDef
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
460 else if is_intern
then
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
(" ")
475 tpl
.append cs_signature
476 return tpl
.write_to_string
481 redef fun cs_short_signature
do
482 if mproperty
.is_root_init
then
483 return new_msignature
.cs_short_signature
485 return msignature
.cs_short_signature
488 redef fun cs_signature
do
489 if mproperty
.is_root_init
then
490 return new_msignature
.cs_signature
492 return msignature
.cs_signature
496 if mproperty
.is_init
then
503 redef class MVirtualTypeDef
505 redef fun cs_modifiers
do
511 # Short signature for `MVirtualType` is always empty.
512 redef fun cs_short_signature
do return ""
514 redef fun cs_signature
do
515 var tpl
= new FlatBuffer
516 if bound
== null then return tpl
.write_to_string
518 tpl
.append bound
.cs_signature
519 return tpl
.write_to_string
521 redef fun cs_icon
do return "V"
525 # Returns the signature of this type whithout bounds.
526 fun cs_short_signature
: String is abstract
528 # Returns the signature of this type.
529 fun cs_signature
: String is abstract
531 redef fun cs_icon
do return "T"
534 redef class MClassType
535 redef fun cs_short_signature
do return cs_name
536 redef fun cs_signature
do return cs_name
539 redef class MNullableType
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
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
556 redef class MGenericType
557 redef fun cs_short_signature
do
558 var tpl
= new FlatBuffer
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
", "
566 return tpl
.write_to_string
569 redef fun cs_signature
do
570 var tpl
= new FlatBuffer
571 tpl
.append mclass
.name
573 for i
in [0..arguments
.length
[ do
574 tpl
.append arguments
[i
].cs_signature
575 if i
< arguments
.length
- 1 then tpl
.append
", "
578 return tpl
.write_to_string
582 redef class MParameterType
583 redef fun cs_short_signature
do return cs_name
584 redef fun cs_signature
do return cs_name
587 redef class MVirtualType
588 redef fun cs_signature
do return cs_name
591 redef class MSignature
593 redef fun cs_short_signature
do
594 var tpl
= new FlatBuffer
595 if not mparameters
.is_empty
then
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
", "
603 return tpl
.write_to_string
606 redef fun cs_signature
do
607 var tpl
= new FlatBuffer
608 if not mparameters
.is_empty
then
610 for i
in [0..mparameters
.length
[ do
611 tpl
.append mparameters
[i
].cs_signature
612 if i
< mparameters
.length
- 1 then tpl
.append
", "
616 if return_mtype
!= null then
618 tpl
.append return_mtype
.cs_signature
620 return tpl
.write_to_string
624 redef class MParameter
626 # Returns `self` name and ellipsys if any.
627 fun cs_short_signature
: String do
628 var tpl
= new FlatBuffer
630 if is_vararg
then tpl
.append
"..."
631 return tpl
.write_to_string
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
644 ################################################################################
645 # Additions to `model_ext`.
648 redef fun cs_signature
do
649 var tpl
= new FlatBuffer
652 if part
.target
!= null then
653 tpl
.append part
.target
.as(not null).cs_name
658 return tpl
.write_to_string
662 redef class MInnerClass
663 redef fun cs_signature
do return inner
.cs_signature
666 redef class MInnerClassDef
667 redef fun cs_signature
do return inner
.cs_signature