vim autocomplete: fix `MDoc` used for classes
[nit.git] / src / doc / vim_autocomplete.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 # Generate files used by the Vim plugin to autocomplete with doc
16 #
17 # There is 3 files generated, each with a different target: modules, types,
18 # properties and constructors. Each line describe a different entity,
19 # with 4 values:
20 #
21 # 1. Short name to use in autocompletion
22 # 2. Full signature
23 # 3. Doc synopsis
24 # 4. Full doc with extra
25 #
26 # The priority with those files is for them to be analyzed efficiently, for
27 # this reason, the data is prepared in advance and some information may be
28 # duplicated.
29 module vim_autocomplete
30
31 import modelbuilder
32 import phase
33 import modelize::modelize_class
34 import model_utils
35
36 redef class ToolContext
37 # Phase generating the files for the Vim plugin
38 var autocomplete_phase: Phase = new AutocompletePhase(self, [modelize_class_phase])
39
40 # Shall we generate the files for the Vim plugin?
41 var opt_vim_autocomplete = new OptionBool(
42 "Generate metadata files used by the Vim plugin for autocompletion", "--vim-autocomplete")
43
44 init
45 do
46 super
47 option_context.add_option opt_vim_autocomplete
48 end
49 end
50
51 redef class MEntity
52 private fun field_separator: String do return "#====#"
53 private fun line_separator: String do return "#nnnn#"
54
55 private fun write_doc(mainmodule: MModule, stream: Writer)
56 do
57 # 1. Short name for autocompletion
58 stream.write complete_name
59 stream.write field_separator
60
61 # 2. Full signature
62 stream.write complete_name
63 write_signature_to_stream(stream)
64 stream.write field_separator
65
66 # 3. Doc synopsis
67 var mdoc = complete_mdoc
68 if mdoc != null then
69 stream.write mdoc.content.first
70 end
71
72 # 4. Full doc with extra
73 stream.write field_separator
74 stream.write "# "
75 stream.write full_name
76 write_signature_to_stream(stream)
77 if mdoc != null then
78 for i in 2.times do stream.write line_separator
79 stream.write mdoc.content.join(line_separator)
80 end
81 write_extra_doc(mainmodule, stream)
82
83 stream.write "\n"
84 end
85
86 private fun write_signature_to_stream(stream: Writer) do end
87
88 # Actual name used in completion
89 private fun complete_name: String do return name
90
91 # Doc to use in completion
92 private fun complete_mdoc: nullable MDoc do return mdoc
93
94 # Extra auto documentation to append to the `stream`
95 private fun write_extra_doc(mainmodule: MModule, stream: Writer) do end
96 end
97
98 redef class MMethodDef
99 redef fun write_signature_to_stream(stream)
100 do
101 var msignature = msignature
102 if msignature != null then
103 stream.write msignature.to_s
104 end
105 end
106 end
107
108 redef class MAttributeDef
109 redef fun write_signature_to_stream(stream)
110 do
111 var static_mtype = static_mtype
112 if static_mtype != null then
113 stream.write stream.to_s
114 end
115 end
116 end
117
118 # Use `MClassDef` as anchor for its constructors only
119 redef class MClassDef
120 private var target_constructor: nullable MMethodDef = null
121
122 redef fun complete_name
123 do
124 var target_constructor = target_constructor
125 assert target_constructor != null
126
127 var params
128 var mparameters = mclass.mparameters
129 if not mparameters.is_empty then
130 params = "[{mparameters.join(", ")}]"
131 else
132 params = ""
133 end
134
135 if target_constructor.name != "init" and target_constructor.name != "new" then
136 return name + params + "." + target_constructor.name
137 end
138
139 return name + params
140 end
141
142 redef fun complete_mdoc
143 do
144 var target_constructor = target_constructor
145 assert target_constructor != null
146
147 if target_constructor.name != "init" and target_constructor.name != "new" then
148 return target_constructor.mdoc
149 end
150
151 return mdoc
152 end
153 end
154
155 redef class MClassType
156 redef fun write_extra_doc(mainmodule, stream)
157 do
158 # Super classes
159 stream.write line_separator*2
160 stream.write "## Class hierarchy"
161
162 var direct_supers = [for s in mclass.in_hierarchy(mainmodule).direct_greaters do s.name]
163 if not direct_supers.is_empty then
164 alpha_comparator.sort direct_supers
165 stream.write line_separator
166 stream.write "* Direct super classes: "
167 stream.write direct_supers.join(", ")
168 end
169
170 var supers = [for s in mclass.in_hierarchy(mainmodule).greaters do s.name]
171 supers.remove mclass.name
172 if not supers.is_empty then
173 alpha_comparator.sort supers
174 stream.write line_separator
175 stream.write "* All super classes: "
176 stream.write supers.join(", ")
177 end
178
179 var direct_subs = [for s in mclass.in_hierarchy(mainmodule).direct_smallers do s.name]
180 if not direct_subs.is_empty then
181 alpha_comparator.sort direct_subs
182 stream.write line_separator
183 stream.write "* Direct sub classes: "
184 stream.write direct_subs.join(", ")
185 end
186
187 var subs = [for s in mclass.in_hierarchy(mainmodule).smallers do s.name]
188 subs.remove mclass.name
189 if not subs.is_empty then
190 alpha_comparator.sort subs
191 stream.write line_separator
192 stream.write "* All sub classes: "
193 stream.write subs.join(", ")
194 end
195
196 # List other properties
197 stream.write line_separator*2
198 stream.write "## Properties"
199 stream.write line_separator
200 var props = mclass.all_mproperties(mainmodule, protected_visibility).to_a
201 alpha_comparator.sort props
202 for prop in props do
203 if mclass.name == "Object" or prop.intro.mclassdef.mclass.name != "Object" then
204
205 if prop.visibility == public_visibility then
206 stream.write "+ "
207 else stream.write "~ " # protected_visibility
208
209 if prop isa MMethod then
210 if prop.is_init and prop.name != "init" then stream.write "init "
211 if prop.is_new and prop.name != "new" then stream.write "new "
212 end
213
214 stream.write prop.name
215
216 if prop isa MMethod then
217 stream.write prop.intro.msignature.to_s
218 end
219
220 var mdoc = prop.intro.mdoc
221 if mdoc != null then
222 stream.write " # "
223 stream.write mdoc.content.first
224 end
225 stream.write line_separator
226 end
227 end
228 end
229
230 redef fun complete_mdoc do return mclass.intro.mdoc
231 end
232
233 private class AutocompletePhase
234 super Phase
235
236 redef fun process_mainmodule(mainmodule, given_mmodules)
237 do
238 if not toolcontext.opt_vim_autocomplete.value then return
239
240 var compile_dir = "NIT_VIM_DIR".environ
241 if compile_dir.is_empty then compile_dir = "HOME".environ / ".vim/nit"
242 compile_dir.mkdir
243
244 var modules_stream = new FileWriter.open(compile_dir / "modules.txt")
245 var classes_stream = new FileWriter.open(compile_dir / "classes.txt")
246 var constructors_stream = new FileWriter.open(compile_dir / "constructors.txt")
247 var types_stream = new FileWriter.open(compile_dir / "types.txt")
248 var properties_stream = new FileWriter.open(compile_dir / "properties.txt")
249
250 # Got all known modules
251 var model = mainmodule.model
252 for mmodule in model.mmodules do
253 mmodule.write_doc(mainmodule, modules_stream)
254 end
255
256 # TODO list other modules from the Nit lib
257
258 # Get all known classes
259 for mclass in model.mclasses do
260 if not mainmodule.is_visible(mclass.intro_mmodule, public_visibility) then continue
261 var mclass_intro = mclass.intro
262
263 # Can it be instantiated?
264 if mclass.kind != interface_kind and mclass.kind != abstract_kind then
265
266 for prop in mclass.all_mproperties(mainmodule, public_visibility) do
267 if prop isa MMethod and prop.is_init then
268 mclass_intro.target_constructor = prop.intro
269 mclass_intro.write_doc(mainmodule, constructors_stream)
270 end
271 end
272 mclass_intro.target_constructor = null
273 end
274
275 # Always add to types and classes
276 mclass.mclass_type.write_doc(mainmodule, classes_stream)
277 mclass.mclass_type.write_doc(mainmodule, types_stream)
278 end
279
280 # Get all known properties
281 for mproperty in model.mproperties do
282 var intro_mmodule = mproperty.intro_mclassdef.mmodule
283 if not mainmodule.is_visible(intro_mmodule, public_visibility) then continue
284
285 # Is it a virtual type?
286 if mproperty isa MVirtualTypeProp then
287 mproperty.intro.write_doc(mainmodule, types_stream)
288 continue
289 end
290
291 # Skip properties beginning with @ or _
292 var first_letter = mproperty.name.chars.first
293 if first_letter == '@' or first_letter == '_' then continue
294
295 mproperty.intro.write_doc(mainmodule, properties_stream)
296 end
297
298 # Close streams
299 for stream in [modules_stream, classes_stream, properties_stream,
300 types_stream, constructors_stream] do
301
302 stream.close
303 var error = stream.last_error
304 if error != null then
305 toolcontext.error(null, "Failed to write Vim autocomplete file: {error}")
306 end
307 end
308 end
309 end