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 # Generate files used by the Vim plugin to autocomplete with doc
17 # There is 3 files generated, each with a different target: modules, types,
18 # properties and constructors. Each line describe a different entity,
21 # 1. Short name to use in autocompletion
24 # 4. Full doc with extra
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
29 module vim_autocomplete
33 import modelize
::modelize_class
34 import model
::model_collect
36 redef class ToolContext
37 # Phase generating the files for the Vim plugin
38 var autocomplete_phase
: Phase = new AutocompletePhase(self, [modelize_class_phase
])
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")
47 option_context
.add_option opt_vim_autocomplete
48 opt_vim_autocomplete
.hidden
= true
53 private fun field_separator
: String do return "#====#"
54 private fun line_separator
: String do return "#nnnn#"
56 private fun write_doc
(view
: ModelView, stream
: Writer)
58 # 1. Short name for autocompletion
59 stream
.write complete_name
60 stream
.write field_separator
63 stream
.write complete_name
64 write_signature_to_stream
(stream
)
65 stream
.write field_separator
68 var mdoc
= complete_mdoc
70 stream
.write mdoc
.content
.first
73 # 4. Full doc with extra
74 stream
.write field_separator
76 stream
.write full_name
77 write_signature_to_stream
(stream
)
79 for i
in 2.times
do stream
.write line_separator
80 stream
.write mdoc
.content
.join
(line_separator
)
83 write_location
(view
.mainmodule
, stream
)
85 write_extra_doc
(view
, stream
)
90 private fun write_signature_to_stream
(stream
: Writer) do end
92 # Actual name used in completion
93 private fun complete_name
: String do return name
95 # Doc to use in completion
96 private fun complete_mdoc
: nullable MDoc do return mdoc
98 # Extra auto documentation to append to the `stream`
99 private fun write_extra_doc
(view
: ModelView, stream
: Writer) do end
101 # Location (file and line when available) of related declarations
102 private fun write_location
(mainmodule
: MModule, stream
: Writer)
104 for i
in 2.times
do stream
.write line_separator
105 stream
.write
"## Location"
106 stream
.write line_separator
107 stream
.write
"* {location}"
111 redef class MMethodDef
112 redef fun write_signature_to_stream
(stream
)
114 var msignature
= msignature
115 if msignature
!= null then
116 stream
.write msignature
.to_s
120 redef fun write_location
(mainmodule
, stream
)
122 for i
in 2.times
do stream
.write line_separator
123 stream
.write
"## Location of introduction and refinements"
125 # Group locations in the same file
126 var file_to_location
= new MultiHashMap[nullable SourceFile, Location]
127 for c
in mproperty
.mpropdefs
do
128 file_to_location
[c
.location
.file
].add c
.location
131 # Write one file per location
132 for file
, locations
in file_to_location
do
133 var l
= locations
.first
134 stream
.write line_separator
136 if locations
.length
> 1 then stream
.write
" ({locations.length-1} more)"
141 redef class MAttributeDef
142 redef fun write_signature_to_stream
(stream
)
144 var static_mtype
= static_mtype
145 if static_mtype
!= null then
146 stream
.write stream
.to_s
151 # Use `MClassDef` as anchor for its constructors only
152 redef class MClassDef
153 private var target_constructor
: nullable MMethodDef = null
155 redef fun complete_name
157 var target_constructor
= target_constructor
158 assert target_constructor
!= null
161 var mparameters
= mclass
.mparameters
162 if not mparameters
.is_empty
then
163 params
= "[{mparameters.join(", ")}]"
168 if target_constructor
.name
!= "init" and target_constructor
.name
!= "new" then
169 return name
+ params
+ "." + target_constructor
.name
175 redef fun complete_mdoc
177 var target_constructor
= target_constructor
178 assert target_constructor
!= null
180 if target_constructor
.name
!= "init" and target_constructor
.name
!= "new" then
181 return target_constructor
.mdoc
188 redef class MClassType
189 redef fun write_extra_doc
(view
, stream
)
192 stream
.write line_separator
*2
193 stream
.write
"## Class hierarchy"
195 var direct_supers
= [for s
in mclass
.in_hierarchy
(view
.mainmodule
).direct_greaters
do s
.name
]
196 if not direct_supers
.is_empty
then
197 alpha_comparator
.sort direct_supers
198 stream
.write line_separator
199 stream
.write
"* Direct super classes: "
200 stream
.write direct_supers
.join
(", ")
203 var supers
= [for s
in mclass
.in_hierarchy
(view
.mainmodule
).greaters
do s
.name
]
204 supers
.remove mclass
.name
205 if not supers
.is_empty
then
206 alpha_comparator
.sort supers
207 stream
.write line_separator
208 stream
.write
"* All super classes: "
209 stream
.write supers
.join
(", ")
212 var direct_subs
= [for s
in mclass
.in_hierarchy
(view
.mainmodule
).direct_smallers
do s
.name
]
213 if not direct_subs
.is_empty
then
214 alpha_comparator
.sort direct_subs
215 stream
.write line_separator
216 stream
.write
"* Direct sub classes: "
217 stream
.write direct_subs
.join
(", ")
220 var subs
= [for s
in mclass
.in_hierarchy
(view
.mainmodule
).smallers
do s
.name
]
221 subs
.remove mclass
.name
222 if not subs
.is_empty
then
223 alpha_comparator
.sort subs
224 stream
.write line_separator
225 stream
.write
"* All sub classes: "
226 stream
.write subs
.join
(", ")
229 # List other properties
230 stream
.write line_separator
*2
231 stream
.write
"## Properties"
232 stream
.write line_separator
233 var props
= mclass
.collect_accessible_mproperties
(view
).to_a
234 alpha_comparator
.sort props
236 if mclass
.name
== "Object" or prop
.intro
.mclassdef
.mclass
.name
!= "Object" then
237 prop
.write_synopsis
(view
.mainmodule
, stream
)
242 redef fun complete_mdoc
do return mclass
.intro
.mdoc
244 redef fun write_location
(mainmodule
, stream
)
246 for i
in 2.times
do stream
.write line_separator
247 stream
.write
"## Location of introduction and refinements"
248 for c
in mclass
.mclassdefs
do
249 stream
.write line_separator
250 stream
.write
"* {c.location}"
255 private class AutocompletePhase
258 redef fun process_mainmodule
(mainmodule
, given_mmodules
)
260 if not toolcontext
.opt_vim_autocomplete
.value
then return
262 var compile_dir
= "NIT_VIM_DIR".environ
263 if compile_dir
.is_empty
then compile_dir
= "HOME".environ
/ ".vim/nit"
266 var modules_stream
= new FileWriter.open
(compile_dir
/ "modules.txt")
267 var classes_stream
= new FileWriter.open
(compile_dir
/ "classes.txt")
268 var constructors_stream
= new FileWriter.open
(compile_dir
/ "constructors.txt")
269 var types_stream
= new FileWriter.open
(compile_dir
/ "types.txt")
270 var properties_stream
= new FileWriter.open
(compile_dir
/ "properties.txt")
272 # Got all known modules
273 var model
= mainmodule
.model
274 var view
= new ModelView(model
, mainmodule
)
275 for mmodule
in model
.mmodules
do
276 mmodule
.write_doc
(view
, modules_stream
)
279 # TODO list other modules from the Nit lib
281 # Get all known classes
282 for mclass
in model
.mclasses
do
283 if not mainmodule
.is_visible
(mclass
.intro_mmodule
, public_visibility
) then continue
284 var mclass_intro
= mclass
.intro
286 # Can it be instantiated?
287 if mclass
.kind
!= interface_kind
and mclass
.kind
!= abstract_kind
then
289 for prop
in mclass
.collect_accessible_mproperties
(view
) do
290 if prop
isa MMethod and prop
.is_init
then
291 mclass_intro
.target_constructor
= prop
.intro
292 mclass_intro
.write_doc
(view
, constructors_stream
)
295 mclass_intro
.target_constructor
= null
298 # Always add to types and classes
299 mclass
.mclass_type
.write_doc
(view
, classes_stream
)
300 mclass
.mclass_type
.write_doc
(view
, types_stream
)
303 # Get all known properties
304 for mproperty
in model
.mproperties
do
305 var intro_mmodule
= mproperty
.intro_mclassdef
.mmodule
306 if not mainmodule
.is_visible
(intro_mmodule
, public_visibility
) then continue
308 # Is it a virtual type?
309 if mproperty
isa MVirtualTypeProp then
310 mproperty
.intro
.write_doc
(view
, types_stream
)
314 # Skip properties beginning with @ or _
315 var first_letter
= mproperty
.name
.chars
.first
316 if first_letter
== '@' or first_letter
== '_' then continue
318 mproperty
.intro
.write_doc
(view
, properties_stream
)
322 for stream
in [modules_stream
, classes_stream
, properties_stream
,
323 types_stream
, constructors_stream
] do
326 var error
= stream
.last_error
327 if error
!= null then
328 toolcontext
.error
(null, "Error: failed to write Vim autocomplete file: {error}.")
335 redef fun write_extra_doc
(view
, stream
)
338 var class_intros
= collect_intro_mclasses
(view
).to_a
339 if class_intros
.not_empty
then
340 alpha_comparator
.sort class_intros
341 stream
.write line_separator
*2
342 stream
.write
"## Introduced classes"
344 for c
in class_intros
do
345 stream
.write line_separator
346 stream
.write
"* {c.name}"
347 var doc
= c
.intro
.mdoc
348 if doc
!= null then stream
.write
": {doc.content.first}"
352 # Introduced properties
353 var prop_intros
= new Array[MPropDef]
354 for c
in mclassdefs
do
355 prop_intros
.add_all c
.collect_intro_mpropdefs
(view
)
358 if prop_intros
.not_empty
then
359 alpha_comparator
.sort prop_intros
360 stream
.write line_separator
*2
361 stream
.write
"## Introduced properties"
362 stream
.write line_separator
364 for p
in prop_intros
do
365 p
.mproperty
.write_synopsis
(view
.mainmodule
, stream
)
371 redef class MProperty
372 private fun write_synopsis
(mainmodule
: MModule, stream
: Writer)
374 if visibility
== public_visibility
then
376 else stream
.write
"~ " # protected_visibility
378 if self isa MMethod then
379 if is_new
and name
!= "new" then
381 else if is_init
and name
!= "init" then
388 if self isa MMethod then
390 assert intro
isa MMethodDef
391 var msignature
= intro
.msignature
392 if msignature
!= null then
393 stream
.write msignature
.to_s
397 var mdoc
= intro
.mdoc
400 stream
.write mdoc
.content
.first
402 stream
.write line_separator