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 # Markdown templates for Nit model MEntities.
17 # This module introduces Markdown rendering services in model entities.
18 # With the `no_color` option set to `false`, the output can be highlighted for console.
25 # Returns the full comment formatted for the console
26 fun cs_comment
(no_color
: nullable Bool, indent
: nullable Int): String do
27 var res
= new FlatBuffer
28 indent
= indent
or else 0
29 for line
in content
do
30 res
.append
"{" " * indent}{line}\n"
32 if no_color
== null or not no_color
then
33 return res
.write_to_string
.green
35 return res
.write_to_string
38 # Returns the synopsis formatted for the console
39 fun cs_short_comment
(no_color
: nullable Bool): String do
40 if no_color
== null or not no_color
then
41 return content
.first
.green
49 # Text icon to be displayed in front of the entity
50 fun cs_icon
(no_color
: nullable Bool): String do
51 return public_visibility
.cs_icon
(no_color
)
54 # Source code location of this MEntity formatted for console
55 fun cs_location
(no_color
: nullable Bool): String do
56 if no_color
== null or not no_color
then
57 return location
.to_s
.bold
.gray
62 # Returns `self.full_name` formatted for console
63 fun cs_full_name
(no_color
: nullable Bool): String do
64 if no_color
== null or not no_color
then
65 return full_name
.bold
.blue
70 # Returns `self` signature formatted for console.
71 fun cs_signature
(no_color
: nullable Bool): String do return ""
73 # Returns the comment of this MEntity formatted for console.
74 fun cs_comment
(no_color
: nullable Bool): nullable String do
75 var mdoc
= mdoc_or_fallback
76 if mdoc
== null then return null
77 # FIXME add markdown for console
78 return mdoc
.cs_comment
(no_color
)
81 # Returns the comment of this MEntity formatted for console.
82 fun cs_short_comment
(no_color
: nullable Bool): nullable String do
83 var mdoc
= mdoc_or_fallback
84 if mdoc
== null then return null
85 return mdoc
.cs_short_comment
(no_color
)
88 # Returns the complete MEntity declaration (modifiers + name + signature).
90 # * MPackage: `package foo`
91 # * MGroup: `group foo`
92 # * MModule: `module foo`
93 # * MClass: `private abstract class Foo[E: Object]`
94 # * MClassDef: `redef class Foo[E]`
95 # * MProperty: `private fun foo(e: Object): Int`
96 # * MPropdef: `redef fun foo(e)`
97 fun cs_declaration
(no_color
: nullable Bool): String do
98 var tpl
= new FlatBuffer
99 tpl
.append collect_modifiers
.join
(" ")
102 tpl
.append cs_signature
(no_color
)
103 if no_color
== null or not no_color
then
104 return tpl
.write_to_string
.bold
106 return tpl
.write_to_string
109 # Returns `self` as a list element that can be displayed in console.
111 # Displays `cs_icon`, `cs_name`, `cs_short_comment, `cs_namespace`,
112 # `cs_declaration` and `cs_location`.
113 fun cs_list_item
(no_color
: nullable Bool): String do
114 var tpl
= new FlatBuffer
116 tpl
.append cs_icon
(no_color
)
118 tpl
.append cs_full_name
(no_color
)
119 var comment
= cs_short_comment
(no_color
)
120 if comment
!= null then
122 if no_color
== null or not no_color
then
123 tpl
.append
"# ".green
130 tpl
.append cs_declaration
(no_color
)
132 tpl
.append cs_location
(no_color
)
133 return tpl
.write_to_string
136 # Source code associated to this MEntity.
138 # Uses `cs_location` to locate the source code.
139 fun cs_source_code
: String do
142 tpl
.append location
.text
144 return tpl
.write_to_string
149 redef fun cs_icon
(no_color
) do return intro
.cs_icon
(no_color
)
150 redef fun cs_signature
(no_color
) do return intro
.cs_signature
(no_color
)
153 redef class MClassDef
154 redef fun cs_icon
(no_color
) do
155 if is_intro
then return visibility
.cs_icon
(no_color
)
156 return visibility
.cs_visibility_color
("*")
159 # Returns the MClassDef generic signature with static bounds.
160 redef fun cs_signature
(no_color
) do
161 var tpl
= new FlatBuffer
162 var mparameters
= mclass
.mparameters
163 if not mparameters
.is_empty
then
165 for i
in [0..mparameters
.length
[ do
166 tpl
.append
"{mparameters[i].name}: "
167 tpl
.append bound_mtype
.arguments
[i
].cs_signature
(no_color
)
168 if i
< mparameters
.length
- 1 then tpl
.append
", "
172 return tpl
.write_to_string
176 redef class MProperty
177 redef fun cs_icon
(no_color
) do return intro
.cs_icon
(no_color
)
178 redef fun cs_signature
(no_color
) do return intro
.cs_signature
(no_color
)
182 redef fun cs_icon
(no_color
) do
183 if is_intro
then return visibility
.cs_icon
(no_color
)
184 return visibility
.cs_visibility_color
("*", no_color
)
188 redef class MAttributeDef
189 redef fun cs_signature
(no_color
) do
190 var tpl
= new FlatBuffer
191 var static_mtype
= self.static_mtype
192 if static_mtype
!= null then
194 tpl
.append static_mtype
.cs_signature
(no_color
)
196 return tpl
.write_to_string
200 redef class MMethodDef
201 redef fun cs_signature
(no_color
) do
202 return msignature
.as(not null).cs_signature
(no_color
)
206 redef class MVirtualTypeDef
207 redef fun cs_signature
(no_color
) do
208 var tpl
= new FlatBuffer
209 var bound
= self.bound
210 if bound
== null then return tpl
.write_to_string
212 tpl
.append bound
.cs_signature
(no_color
)
213 return tpl
.write_to_string
217 redef class MClassType
218 redef fun cs_signature
(no_color
) do return name
221 redef class MNullableType
222 redef fun cs_signature
(no_color
) do
223 var tpl
= new FlatBuffer
224 tpl
.append
"nullable "
225 tpl
.append mtype
.cs_signature
(no_color
)
226 return tpl
.write_to_string
230 redef class MGenericType
231 redef fun cs_signature
(no_color
) do
232 var tpl
= new FlatBuffer
233 tpl
.append mclass
.name
235 for i
in [0..arguments
.length
[ do
236 tpl
.append arguments
[i
].cs_signature
(no_color
)
237 if i
< arguments
.length
- 1 then tpl
.append
", "
240 return tpl
.write_to_string
244 redef class MParameterType
245 redef fun cs_signature
(no_color
) do return name
248 redef class MVirtualType
249 redef fun cs_signature
(no_color
) do return name
252 redef class MSignature
253 redef fun cs_signature
(no_color
) do
254 var tpl
= new FlatBuffer
255 if not mparameters
.is_empty
then
257 for i
in [0..mparameters
.length
[ do
258 tpl
.append mparameters
[i
].cs_signature
(no_color
)
259 if i
< mparameters
.length
- 1 then tpl
.append
", "
263 var return_mtype
= self.return_mtype
264 if return_mtype
!= null then
266 tpl
.append return_mtype
.cs_signature
(no_color
)
268 return tpl
.write_to_string
272 redef class MParameter
273 redef fun cs_signature
(no_color
) do
274 var tpl
= new FlatBuffer
275 tpl
.append
"{name}: "
276 tpl
.append mtype
.cs_signature
(no_color
)
277 if is_vararg
then tpl
.append
"..."
278 return tpl
.write_to_string
282 redef class MVisibility
284 fun cs_icon
(no_color
: nullable Bool): String do
286 if self == private_visibility
then
287 icon
= cs_visibility_color
("-", no_color
)
288 else if self == protected_visibility
then
289 icon
= cs_visibility_color
("#", no_color
)
291 icon
= cs_visibility_color
("+", no_color
)
293 if no_color
!= null and no_color
then return icon
297 # Colorize `string` depending on the visibility
298 fun cs_visibility_color
(string
: String, no_color
: nullable Bool): String do
299 if no_color
!= null and no_color
then return string
300 if self == private_visibility
then
302 else if self == protected_visibility
then