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, classes,
18 # and properties. Each line describe a different entity, with 4 values:
20 # 1. Short name to use in autocompletion
23 # 4. Full doc with extra
25 # The priority with those files is for them to be analyzed efficiently, for
26 # this reason, the data is prepared in advant and some information may be
28 module vim_autocomplete
32 import modelize
::modelize_class
34 redef class ToolContext
35 # Phase generating the files for the Vim plugin
36 var autocomplete_phase
: Phase = new AutocompletePhase(self, [modelize_class_phase
])
38 # Shall we generate the files for the Vim plugin?
39 var opt_vim_autocomplete
= new OptionBool(
40 "Generate metadata files used by the Vim plugin for autocompletion", "--vim-autocomplete")
45 option_context
.add_option opt_vim_autocomplete
50 private fun field_separator
: String do return "#====#"
51 private fun line_separator
: String do return "#nnnn#"
53 private fun write_to_stream
(stream
: OStream)
55 # 1. Short name for autocompletion
57 stream
.write field_separator
61 write_signature_to_stream
(stream
)
62 stream
.write field_separator
67 stream
.write mdoc
.content
.first
70 # 4. Full doc with extra
71 stream
.write field_separator
74 stream
.write full_name
75 write_signature_to_stream
(stream
)
76 for i
in 2.times
do stream
.write line_separator
77 stream
.write mdoc
.content
.join
(line_separator
)
83 private fun write_signature_to_stream
(stream
: OStream) do end
86 redef class MMethodDef
87 redef fun write_signature_to_stream
(stream
)
89 var msignature
= msignature
90 if msignature
!= null then
91 stream
.write msignature
.to_s
96 redef class MAttributeDef
97 redef fun write_signature_to_stream
(stream
)
99 var static_mtype
= static_mtype
100 if static_mtype
!= null then
101 stream
.write stream
.to_s
106 private class AutocompletePhase
109 redef fun process_mainmodule
(mainmodule
, given_mmodules
)
111 if not toolcontext
.opt_vim_autocomplete
.value
then return
113 var compile_dir
= "NIT_VIM_DIR".environ
114 if compile_dir
.is_empty
then compile_dir
= "HOME".environ
/ ".vim/nit"
116 var modules_stream
= new OFStream.open
(compile_dir
/ "modules.txt")
117 var classes_stream
= new OFStream.open
(compile_dir
/ "classes.txt")
118 var properties_stream
= new OFStream.open
(compile_dir
/ "properties.txt")
120 # Got all known modules
121 var model
= mainmodule
.model
122 for mmodule
in model
.mmodules
do
123 mmodule
.write_to_stream modules_stream
126 # TODO list other modules from the Nit lib
127 # TODO list submodules
129 # Get all known classes
130 for mclass
in model
.mclasses
do
131 if not mainmodule
.is_visible
(mclass
.intro_mmodule
, public_visibility
) then continue
133 mclass
.intro
.write_to_stream classes_stream
136 # Get all known properties
137 for mproperty
in model
.mproperties
do
138 var intro_mmodule
= mproperty
.intro_mclassdef
.mmodule
139 if not mainmodule
.is_visible
(intro_mmodule
, public_visibility
) then continue
141 mproperty
.intro
.write_to_stream properties_stream
145 for stream
in [modules_stream
, classes_stream
, properties_stream
] do
148 var error
= stream
.last_error
149 if error
!= null then
150 toolcontext
.error
(null, "Failed to write Vim autocomplete file: {error}")