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
125 # Source code associated to this MEntity.
127 # Uses `cs_location` to locate the source code.
128 fun cs_source_code
: String do
129 # FIXME up location to mentity
130 var loc
= new Location.from_string
(cs_location
)
131 var fr
= new FileReader.open
(loc
.file
.filename
)
132 var content
= new FlatBuffer
136 var line
= fr
.read_line
137 if i
< loc
.line_start
or i
> loc
.line_end
then continue
138 # FIXME add nitlight for console
139 content
.append
"{line}\n"
142 return content
.write_to_string
147 redef var cs_modifiers
= ["project"]
148 redef fun cs_namespace
do return cs_name
149 redef fun cs_icon
do return "P"
150 redef fun cs_location
do return root
.mmodules
.first
.location
.to_s
154 redef var cs_modifiers
= ["group"]
155 redef fun cs_icon
do return "G"
157 # Depends if `self` is root or not.
159 # * If root `mproject`.
160 # * Else `mproject::self`.
161 redef fun cs_namespace
do
162 var tpl
= new FlatBuffer
163 tpl
.append mproject
.cs_namespace
164 if mproject
.root
!= self then
168 return tpl
.write_to_string
171 redef fun cs_location
do return mmodules
.first
.location
.to_s
175 redef var cs_modifiers
= ["module"]
176 redef fun cs_icon
do return "M"
178 # Depends if `self` belongs to a MGroup.
180 # * If mgroup `mgroup::self`.
182 redef fun cs_namespace
do
183 var tpl
= new FlatBuffer
184 if mgroup
!= null then
185 tpl
.append mgroup
.cs_namespace
189 return tpl
.write_to_string
192 redef fun cs_location
do return location
.to_s
196 redef fun mdoc_or_fallback
do return intro
.mdoc
197 redef fun cs_icon
do return intro
.cs_icon
200 redef var cs_name
is lazy
do
201 var tpl
= new FlatBuffer
205 var parameter_names
= new Array[String]
206 for p
in mparameters
do
207 parameter_names
.add
(p
.cs_name
)
209 tpl
.append parameter_names
.join
(", ")
212 return tpl
.write_to_string
215 redef fun cs_modifiers
do return intro
.cs_modifiers
216 redef fun cs_declaration
do return intro
.cs_declaration
218 # Returns `mproject::self`.
219 redef fun cs_namespace
do
220 var tpl
= new FlatBuffer
221 tpl
.append intro_mmodule
.mgroup
.mproject
.cs_namespace
224 return tpl
.write_to_string
227 # Returns `intro.cs_short_signature`.
228 fun cs_short_signature
: String do return intro
.cs_short_signature
230 # Returns `intro.cs_signature`.
231 fun cs_signature
: String do return intro
.cs_signature
233 redef fun cs_visibility_color
(string
) do
234 if visibility
== private_visibility
then
236 else if visibility
== protected_visibility
then
242 redef fun cs_location
do return intro
.location
.to_s
245 redef class MClassDef
246 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
247 redef fun cs_icon
do return "C"
249 # Depends if `self` is an intro or not.
251 # * If intro contains the visibility and kind.
252 # * If redef contains the `redef` keyword and kind.
253 redef fun cs_modifiers
do
254 var res
= new Array[String]
258 if mclass
.visibility
!= public_visibility
then
259 res
.add mclass
.visibility
.to_s
262 res
.add mclass
.kind
.to_s
266 # Depends if `self` is an intro or not.
268 # For intro: `private abstract class Foo[E: Object]`
269 # For redef: `redef class Foo[E]`
270 redef fun cs_declaration
do
271 var tpl
= new FlatBuffer
272 tpl
.append cs_modifiers
.join
(" ")
276 tpl
.append cs_signature
278 tpl
.append cs_short_signature
280 return tpl
.write_to_string
.write_to_string
.write_to_string
283 # Returns `mmodule::self`
284 redef fun cs_namespace
do
285 var tpl
= new FlatBuffer
286 tpl
.append mmodule
.cs_namespace
288 tpl
.append mclass
.cs_name
289 return tpl
.write_to_string
.write_to_string
292 # Returns the MClassDef generic signature without static bounds.
293 fun cs_short_signature
: String do
294 var tpl
= new FlatBuffer
295 var mparameters
= mclass
.mparameters
296 if not mparameters
.is_empty
then
298 for i
in [0..mparameters
.length
[ do
299 tpl
.append mparameters
[i
].cs_name
300 if i
< mparameters
.length
- 1 then tpl
.append
", "
304 return tpl
.write_to_string
307 # Returns the MClassDef generic signature with static bounds.
308 fun cs_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 tpl
.append bound_mtype
.arguments
[i
].cs_signature
316 if i
< mparameters
.length
- 1 then tpl
.append
", "
320 return tpl
.write_to_string
323 redef fun cs_location
do return location
.to_s
326 redef class MProperty
327 redef fun mdoc_or_fallback
do return intro
.mdoc
328 redef fun cs_modifiers
do return intro
.cs_modifiers
329 redef fun cs_declaration
do return intro
.cs_declaration
330 redef fun cs_icon
do return intro
.cs_icon
332 # Returns `mclass::self`.
333 redef fun cs_namespace
do
334 var tpl
= new FlatBuffer
335 tpl
.append intro_mclassdef
.mclass
.cs_namespace
337 tpl
.append intro
.cs_name
338 return tpl
.write_to_string
341 # Returns `intro.cs_short_signature`.
342 fun cs_short_signature
: String do return intro
.cs_short_signature
344 # Returns `intro.cs_signature`.
345 fun cs_signature
: String do return intro
.cs_signature
347 redef fun cs_visibility_color
(string
) do
348 if visibility
== private_visibility
then
350 else if visibility
== protected_visibility
then
356 # Returns `intro.cs_location`.
357 redef fun cs_location
do return intro
.location
.to_s
361 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
363 # Depends if `self` is an intro or not.
365 # * If intro contains the visibility and kind.
366 # * If redef contains the `redef` keyword and kind.
367 redef fun cs_modifiers
do
368 var res
= new Array[String]
372 if mproperty
.visibility
!= public_visibility
then
373 res
.add mproperty
.visibility
.to_s
379 # Depends if `self` is an intro or not.
381 # For intro: `private fun foo(e: Object): Bar is abstract`
382 # For redef: `redef fun foo(e) is cached`
383 redef fun cs_declaration
do
384 var tpl
= new FlatBuffer
385 tpl
.append cs_modifiers
.join
(" ")
389 tpl
.append cs_signature
391 tpl
.append mproperty
.intro
.cs_name
392 tpl
.append cs_short_signature
394 return tpl
.write_to_string
397 # Returns `mclassdef::self`
398 redef fun cs_namespace
do
399 var tpl
= new FlatBuffer
400 tpl
.append mclassdef
.cs_namespace
403 return tpl
.write_to_string
406 redef fun cs_location
do return location
.to_s
408 # Returns the MPropdDef signature without static types.
409 fun cs_short_signature
: String is abstract
411 # Returns the MPropDef signature with static types.
412 fun cs_signature
: String is abstract
415 redef class MAttributeDef
417 redef fun cs_modifiers
do
423 redef fun cs_short_signature
do return ""
425 redef fun cs_signature
do
426 var tpl
= new FlatBuffer
427 if static_mtype
!= null then
429 tpl
.append static_mtype
.cs_signature
431 return tpl
.write_to_string
434 redef fun cs_icon
do return "A"
437 redef class MMethodDef
439 redef fun cs_modifiers
do
440 if mproperty
.is_init
then
441 var res
= new Array[String]
442 if mproperty
.visibility
!= public_visibility
then
443 res
.add mproperty
.visibility
.to_s
450 else if is_intern
then
457 redef fun cs_declaration
do
458 if mproperty
.is_init
then
459 var tpl
= new FlatBuffer
460 if not cs_modifiers
.is_empty
then
461 tpl
.append cs_modifiers
.join
(" ")
465 tpl
.append cs_signature
466 return tpl
.write_to_string
471 redef fun cs_short_signature
do
472 if mproperty
.is_root_init
then
473 return new_msignature
.cs_short_signature
475 return msignature
.cs_short_signature
478 redef fun cs_signature
do
479 if mproperty
.is_root_init
then
480 return new_msignature
.cs_signature
482 return msignature
.cs_signature
486 if mproperty
.is_init
then
493 redef class MVirtualTypeDef
495 redef fun cs_modifiers
do
501 # Short signature for `MVirtualType` is always empty.
502 redef fun cs_short_signature
do return ""
504 redef fun cs_signature
do
505 var tpl
= new FlatBuffer
506 if bound
== null then return tpl
.write_to_string
508 tpl
.append bound
.cs_signature
509 return tpl
.write_to_string
511 redef fun cs_icon
do return "V"
515 # Returns the signature of this type whithout bounds.
516 fun cs_short_signature
: String is abstract
518 # Returns the signature of this type.
519 fun cs_signature
: String is abstract
521 redef fun cs_icon
do return "T"
524 redef class MClassType
525 redef fun cs_short_signature
do return cs_name
526 redef fun cs_signature
do return cs_name
529 redef class MNullableType
531 redef fun cs_short_signature
do
532 var tpl
= new FlatBuffer
533 tpl
.append
"nullable "
534 tpl
.append mtype
.cs_short_signature
535 return tpl
.write_to_string
538 redef fun cs_signature
do
539 var tpl
= new FlatBuffer
540 tpl
.append
"nullable "
541 tpl
.append mtype
.cs_signature
542 return tpl
.write_to_string
546 redef class MGenericType
547 redef fun cs_short_signature
do
548 var tpl
= new FlatBuffer
551 for i
in [0..arguments
.length
[ do
552 tpl
.append arguments
[i
].cs_short_signature
553 if i
< arguments
.length
- 1 then tpl
.append
", "
556 return tpl
.write_to_string
559 redef fun cs_signature
do
560 var tpl
= new FlatBuffer
561 tpl
.append mclass
.name
563 for i
in [0..arguments
.length
[ do
564 tpl
.append arguments
[i
].cs_signature
565 if i
< arguments
.length
- 1 then tpl
.append
", "
568 return tpl
.write_to_string
572 redef class MParameterType
573 redef fun cs_short_signature
do return cs_name
574 redef fun cs_signature
do return cs_name
577 redef class MVirtualType
578 redef fun cs_signature
do return cs_name
581 redef class MSignature
583 redef fun cs_short_signature
do
584 var tpl
= new FlatBuffer
585 if not mparameters
.is_empty
then
587 for i
in [0..mparameters
.length
[ do
588 tpl
.append mparameters
[i
].cs_short_signature
589 if i
< mparameters
.length
- 1 then tpl
.append
", "
593 return tpl
.write_to_string
596 redef fun cs_signature
do
597 var tpl
= new FlatBuffer
598 if not mparameters
.is_empty
then
600 for i
in [0..mparameters
.length
[ do
601 tpl
.append mparameters
[i
].cs_signature
602 if i
< mparameters
.length
- 1 then tpl
.append
", "
606 if return_mtype
!= null then
608 tpl
.append return_mtype
.cs_signature
610 return tpl
.write_to_string
614 redef class MParameter
616 # Returns `self` name and ellipsys if any.
617 fun cs_short_signature
: String do
618 var tpl
= new FlatBuffer
620 if is_vararg
then tpl
.append
"..."
621 return tpl
.write_to_string
624 # Returns `self` name with it's static type and ellipsys if any.
625 fun cs_signature
: String do
626 var tpl
= new FlatBuffer
627 tpl
.append
"{name}: "
628 tpl
.append mtype
.cs_signature
629 if is_vararg
then tpl
.append
"..."
630 return tpl
.write_to_string
634 ################################################################################
635 # Additions to `model_ext`.
638 redef fun cs_signature
do
639 var tpl
= new FlatBuffer
642 if part
.target
!= null then
643 tpl
.append part
.target
.as(not null).cs_name
648 return tpl
.write_to_string
652 redef class MInnerClass
653 redef fun cs_signature
do return inner
.cs_signature
656 redef class MInnerClassDef
657 redef fun cs_signature
do return inner
.cs_signature