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
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
52 private fun field_separator
: String do return "#====#"
53 private fun line_separator
: String do return "#nnnn#"
55 private fun write_doc
(mainmodule
: MModule, stream
: Writer)
57 # 1. Short name for autocompletion
58 stream
.write complete_name
59 stream
.write field_separator
62 stream
.write complete_name
63 write_signature_to_stream
(stream
)
64 stream
.write field_separator
67 var mdoc
= complete_mdoc
69 stream
.write mdoc
.content
.first
72 # 4. Full doc with extra
73 stream
.write field_separator
75 stream
.write full_name
76 write_signature_to_stream
(stream
)
78 for i
in 2.times
do stream
.write line_separator
79 stream
.write mdoc
.content
.join
(line_separator
)
81 write_extra_doc
(mainmodule
, stream
)
86 private fun write_signature_to_stream
(stream
: Writer) do end
88 # Actual name used in completion
89 private fun complete_name
: String do return name
91 # Doc to use in completion
92 private fun complete_mdoc
: nullable MDoc do return mdoc
94 # Extra auto documentation to append to the `stream`
95 private fun write_extra_doc
(mainmodule
: MModule, stream
: Writer) do end
98 redef class MMethodDef
99 redef fun write_signature_to_stream
(stream
)
101 var msignature
= msignature
102 if msignature
!= null then
103 stream
.write msignature
.to_s
108 redef class MAttributeDef
109 redef fun write_signature_to_stream
(stream
)
111 var static_mtype
= static_mtype
112 if static_mtype
!= null then
113 stream
.write stream
.to_s
118 # Use `MClassDef` as anchor for its constructors only
119 redef class MClassDef
120 private var target_constructor
: nullable MMethodDef = null
122 redef fun complete_name
124 var target_constructor
= target_constructor
125 assert target_constructor
!= null
128 var mparameters
= mclass
.mparameters
129 if not mparameters
.is_empty
then
130 params
= "[{mparameters.join(", ")}]"
135 if target_constructor
.name
!= "init" and target_constructor
.name
!= "new" then
136 return name
+ params
+ "." + target_constructor
.name
142 redef fun complete_mdoc
144 var target_constructor
= target_constructor
145 assert target_constructor
!= null
147 if target_constructor
.name
!= "init" and target_constructor
.name
!= "new" then
148 return target_constructor
.mdoc
155 redef class MClassType
156 redef fun write_extra_doc
(mainmodule
, stream
)
159 stream
.write line_separator
*2
160 stream
.write
"## Class hierarchy"
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
(", ")
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
(", ")
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
(", ")
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
(", ")
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
203 if mclass
.name
== "Object" or prop
.intro
.mclassdef
.mclass
.name
!= "Object" then
205 if prop
.visibility
== public_visibility
then
207 else stream
.write
"~ " # protected_visibility
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 "
214 stream
.write prop
.name
216 if prop
isa MMethod then
217 stream
.write prop
.intro
.msignature
.to_s
220 var mdoc
= prop
.intro
.mdoc
223 stream
.write mdoc
.content
.first
225 stream
.write line_separator
230 redef fun complete_mdoc
do return mclass
.intro
.mdoc
233 private class AutocompletePhase
236 redef fun process_mainmodule
(mainmodule
, given_mmodules
)
238 if not toolcontext
.opt_vim_autocomplete
.value
then return
240 var compile_dir
= "NIT_VIM_DIR".environ
241 if compile_dir
.is_empty
then compile_dir
= "HOME".environ
/ ".vim/nit"
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")
250 # Got all known modules
251 var model
= mainmodule
.model
252 for mmodule
in model
.mmodules
do
253 mmodule
.write_doc
(mainmodule
, modules_stream
)
256 # TODO list other modules from the Nit lib
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
263 # Can it be instantiated?
264 if mclass
.kind
!= interface_kind
and mclass
.kind
!= abstract_kind
then
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
)
272 mclass_intro
.target_constructor
= null
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
)
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
285 # Is it a virtual type?
286 if mproperty
isa MVirtualTypeProp then
287 mproperty
.intro
.write_doc
(mainmodule
, types_stream
)
291 # Skip properties beginning with @ or _
292 var first_letter
= mproperty
.name
.chars
.first
293 if first_letter
== '@' or first_letter
== '_' then continue
295 mproperty
.intro
.write_doc
(mainmodule
, properties_stream
)
299 for stream
in [modules_stream
, classes_stream
, properties_stream
,
300 types_stream
, constructors_stream
] do
303 var error
= stream
.last_error
304 if error
!= null then
305 toolcontext
.error
(null, "Failed to write Vim autocomplete file: {error}")