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 mdoc_or_fallback
do return intro
.mdoc
212 redef fun cs_icon
do return intro
.cs_icon
215 redef var cs_name
is lazy
do
216 var tpl
= new FlatBuffer
220 var parameter_names
= new Array[String]
221 for p
in mparameters
do
222 parameter_names
.add
(p
.cs_name
)
224 tpl
.append parameter_names
.join
(", ")
227 return tpl
.write_to_string
230 redef fun cs_modifiers
do return intro
.cs_modifiers
231 redef fun cs_declaration
do return intro
.cs_declaration
233 # Returns `mpackage::self`.
234 redef fun cs_namespace
do
235 var tpl
= new FlatBuffer
236 tpl
.append intro_mmodule
.mgroup
.mpackage
.cs_namespace
239 return tpl
.write_to_string
242 # Returns `intro.cs_short_signature`.
243 fun cs_short_signature
: String do return intro
.cs_short_signature
245 # Returns `intro.cs_signature`.
246 fun cs_signature
: String do return intro
.cs_signature
248 redef fun cs_visibility_color
(string
) do
249 if visibility
== private_visibility
then
251 else if visibility
== protected_visibility
then
257 redef fun cs_location
do return intro
.location
.to_s
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"
264 # Depends if `self` is an intro or not.
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]
273 if mclass
.visibility
!= public_visibility
then
274 res
.add mclass
.visibility
.to_s
277 res
.add mclass
.kind
.to_s
281 # Depends if `self` is an intro or not.
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
(" ")
291 tpl
.append cs_signature
293 tpl
.append cs_short_signature
295 return tpl
.write_to_string
.write_to_string
.write_to_string
298 # Returns `mmodule::self`
299 redef fun cs_namespace
do
300 var tpl
= new FlatBuffer
301 tpl
.append mmodule
.cs_namespace
303 tpl
.append mclass
.cs_name
304 return tpl
.write_to_string
.write_to_string
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
313 for i
in [0..mparameters
.length
[ do
314 tpl
.append mparameters
[i
].cs_name
315 if i
< mparameters
.length
- 1 then tpl
.append
", "
319 return tpl
.write_to_string
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
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
", "
335 return tpl
.write_to_string
338 redef fun cs_location
do return location
.to_s
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
347 # Returns `mclass::self`.
348 redef fun cs_namespace
do
349 var tpl
= new FlatBuffer
350 tpl
.append intro_mclassdef
.mclass
.cs_namespace
352 tpl
.append intro
.cs_name
353 return tpl
.write_to_string
356 # Returns `intro.cs_short_signature`.
357 fun cs_short_signature
: String do return intro
.cs_short_signature
359 # Returns `intro.cs_signature`.
360 fun cs_signature
: String do return intro
.cs_signature
362 redef fun cs_visibility_color
(string
) do
363 if visibility
== private_visibility
then
365 else if visibility
== protected_visibility
then
371 # Returns `intro.cs_location`.
372 redef fun cs_location
do return intro
.location
.to_s
376 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
378 # Depends if `self` is an intro or not.
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]
387 if mproperty
.visibility
!= public_visibility
then
388 res
.add mproperty
.visibility
.to_s
394 # Depends if `self` is an intro or not.
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
(" ")
404 tpl
.append cs_signature
406 tpl
.append mproperty
.intro
.cs_name
407 tpl
.append cs_short_signature
409 return tpl
.write_to_string
412 # Returns `mclassdef::self`
413 redef fun cs_namespace
do
414 var tpl
= new FlatBuffer
415 tpl
.append mclassdef
.cs_namespace
418 return tpl
.write_to_string
421 redef fun cs_location
do return location
.to_s
423 # Returns the MPropdDef signature without static types.
424 fun cs_short_signature
: String is abstract
426 # Returns the MPropDef signature with static types.
427 fun cs_signature
: String is abstract
430 redef class MAttributeDef
432 redef fun cs_modifiers
do
438 redef fun cs_short_signature
do return ""
440 redef fun cs_signature
do
441 var tpl
= new FlatBuffer
442 if static_mtype
!= null then
444 tpl
.append static_mtype
.cs_signature
446 return tpl
.write_to_string
449 redef fun cs_icon
do return "A"
452 redef class MMethodDef
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
465 else if is_intern
then
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
(" ")
480 tpl
.append cs_signature
481 return tpl
.write_to_string
486 redef fun cs_short_signature
do
487 if mproperty
.is_root_init
then
488 return new_msignature
.cs_short_signature
490 return msignature
.cs_short_signature
493 redef fun cs_signature
do
494 if mproperty
.is_root_init
then
495 return new_msignature
.cs_signature
497 return msignature
.cs_signature
501 if mproperty
.is_init
then
508 redef class MVirtualTypeDef
510 redef fun cs_modifiers
do
516 # Short signature for `MVirtualType` is always empty.
517 redef fun cs_short_signature
do return ""
519 redef fun cs_signature
do
520 var tpl
= new FlatBuffer
521 if bound
== null then return tpl
.write_to_string
523 tpl
.append bound
.cs_signature
524 return tpl
.write_to_string
526 redef fun cs_icon
do return "V"
530 # Returns the signature of this type whithout bounds.
531 fun cs_short_signature
: String is abstract
533 # Returns the signature of this type.
534 fun cs_signature
: String is abstract
536 redef fun cs_icon
do return "T"
539 redef class MClassType
540 redef fun cs_short_signature
do return cs_name
541 redef fun cs_signature
do return cs_name
544 redef class MNullableType
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
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
561 redef class MGenericType
562 redef fun cs_short_signature
do
563 var tpl
= new FlatBuffer
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
", "
571 return tpl
.write_to_string
574 redef fun cs_signature
do
575 var tpl
= new FlatBuffer
576 tpl
.append mclass
.name
578 for i
in [0..arguments
.length
[ do
579 tpl
.append arguments
[i
].cs_signature
580 if i
< arguments
.length
- 1 then tpl
.append
", "
583 return tpl
.write_to_string
587 redef class MParameterType
588 redef fun cs_short_signature
do return cs_name
589 redef fun cs_signature
do return cs_name
592 redef class MVirtualType
593 redef fun cs_signature
do return cs_name
596 redef class MSignature
598 redef fun cs_short_signature
do
599 var tpl
= new FlatBuffer
600 if not mparameters
.is_empty
then
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
", "
608 return tpl
.write_to_string
611 redef fun cs_signature
do
612 var tpl
= new FlatBuffer
613 if not mparameters
.is_empty
then
615 for i
in [0..mparameters
.length
[ do
616 tpl
.append mparameters
[i
].cs_signature
617 if i
< mparameters
.length
- 1 then tpl
.append
", "
621 if return_mtype
!= null then
623 tpl
.append return_mtype
.cs_signature
625 return tpl
.write_to_string
629 redef class MParameter
631 # Returns `self` name and ellipsys if any.
632 fun cs_short_signature
: String do
633 var tpl
= new FlatBuffer
635 if is_vararg
then tpl
.append
"..."
636 return tpl
.write_to_string
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
649 ################################################################################
650 # Additions to `model_ext`.
653 redef fun cs_signature
do
654 var tpl
= new FlatBuffer
657 if part
.target
!= null then
658 tpl
.append part
.target
.as(not null).cs_name
663 return tpl
.write_to_string
667 redef class MInnerClass
668 redef fun cs_signature
do return inner
.cs_signature
671 redef class MInnerClassDef
672 redef fun cs_signature
do return inner
.cs_signature