html_model: Remove new_msignature
[nit.git] / src / doc / templates / term_model.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Markdown templates for Nit model MEntities.
16 #
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.
19 module term_model
20
21 import model_collect
22 import console
23
24 redef class MDoc
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"
31 end
32 if no_color == null or not no_color then
33 return res.write_to_string.green
34 end
35 return res.write_to_string
36 end
37
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
42 end
43 return content.first
44 end
45 end
46
47 redef class MEntity
48
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)
52 end
53
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
58 end
59 return location.to_s
60 end
61
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
66 end
67 return full_name
68 end
69
70 # Returns `self` signature formatted for console.
71 fun cs_signature(no_color: nullable Bool): String do return ""
72
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)
79 end
80
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)
86 end
87
88 # Returns the complete MEntity declaration (modifiers + name + signature).
89 #
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(" ")
100 tpl.append " "
101 tpl.append name
102 tpl.append cs_signature(no_color)
103 if no_color == null or not no_color then
104 return tpl.write_to_string.bold
105 end
106 return tpl.write_to_string
107 end
108
109 # Returns `self` as a list element that can be displayed in console.
110 #
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
115 tpl.append " "
116 tpl.append cs_icon(no_color)
117 tpl.append " "
118 tpl.append cs_full_name(no_color)
119 var comment = cs_short_comment(no_color)
120 if comment != null then
121 tpl.append "\n "
122 if no_color == null or not no_color then
123 tpl.append "# ".green
124 else
125 tpl.append "# "
126 end
127 tpl.append comment
128 end
129 tpl.append "\n "
130 tpl.append cs_declaration(no_color)
131 tpl.append "\n "
132 tpl.append cs_location(no_color)
133 return tpl.write_to_string
134 end
135
136 # Source code associated to this MEntity.
137 #
138 # Uses `cs_location` to locate the source code.
139 fun cs_source_code: String do
140 var tpl = new Buffer
141 tpl.append "\n~~~\n"
142 tpl.append location.text
143 tpl.append "\n~~~\n"
144 return tpl.write_to_string
145 end
146 end
147
148 redef class MClass
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)
151 end
152
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("*")
157 end
158
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
164 tpl.append "["
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 ", "
169 end
170 tpl.append "]"
171 end
172 return tpl.write_to_string
173 end
174 end
175
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)
179 end
180
181 redef class MPropDef
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)
185 end
186 end
187
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
193 tpl.append ": "
194 tpl.append static_mtype.cs_signature(no_color)
195 end
196 return tpl.write_to_string
197 end
198 end
199
200 redef class MMethodDef
201 redef fun cs_signature(no_color) do
202 return msignature.as(not null).cs_signature(no_color)
203 end
204 end
205
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
211 tpl.append ": "
212 tpl.append bound.cs_signature(no_color)
213 return tpl.write_to_string
214 end
215 end
216
217 redef class MClassType
218 redef fun cs_signature(no_color) do return name
219 end
220
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
227 end
228 end
229
230 redef class MGenericType
231 redef fun cs_signature(no_color) do
232 var tpl = new FlatBuffer
233 tpl.append mclass.name
234 tpl.append "["
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 ", "
238 end
239 tpl.append "]"
240 return tpl.write_to_string
241 end
242 end
243
244 redef class MParameterType
245 redef fun cs_signature(no_color) do return name
246 end
247
248 redef class MVirtualType
249 redef fun cs_signature(no_color) do return name
250 end
251
252 redef class MSignature
253 redef fun cs_signature(no_color) do
254 var tpl = new FlatBuffer
255 if not mparameters.is_empty then
256 tpl.append "("
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 ", "
260 end
261 tpl.append ")"
262 end
263 var return_mtype = self.return_mtype
264 if return_mtype != null then
265 tpl.append ": "
266 tpl.append return_mtype.cs_signature(no_color)
267 end
268 return tpl.write_to_string
269 end
270 end
271
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
279 end
280 end
281
282 redef class MVisibility
283 # Visibility icon
284 fun cs_icon(no_color: nullable Bool): String do
285 var icon
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)
290 else
291 icon = cs_visibility_color("+", no_color)
292 end
293 if no_color != null and no_color then return icon
294 return icon.bold
295 end
296
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
301 return string.red
302 else if self == protected_visibility then
303 return string.yellow
304 else
305 return string.green
306 end
307 end
308 end