forked from waj/SublimErl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
erl_completion.py
271 lines (238 loc) · 9.69 KB
/
erl_completion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# ==========================================================================================================
# Erl - A Sublime Text 3 Plugin for Erlang Integrated Testing & Code Completion
#
# Copyright (C) 2013, Roberto Ostinelli <roberto@ostinelli.net>.
# All rights reserved.
#
# BSD License
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
# the following disclaimer in the documentation and/or other materials provided with the distribution.
# * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ==========================================================================================================
# imports
import sublime, sublime_plugin, fnmatch
import os, threading, pickle, json, re
import Erl.erl_core as GLOBALS
from .erl_core import ErlProjectLoader
ERL_COMPLETIONS = {
'erlang_libs': {
'completions': {},
'load_in_progress': False,
'rebuilt': False
},
'current_project': {
'completions': {},
'load_in_progress': False,
'rebuild_in_progress': False
}
}
# erlang module name completions
class ErlModuleNameCompletions():
def set_completions(self):
# if errors occurred
if GLOBALS.ERL.plugin_path == None: return
# load json
completions_full_path = os.path.join(GLOBALS.ERL.plugin_path, 'completion', 'Erlang-Libs.sublime-completions.full')
if os.path.exists(completions_full_path):
f = open(completions_full_path)
file_json = json.load(f)
f.close()
# filter
completions = []
for m in file_json['completions']:
valid = True
for regex in GLOBALS.ERL.completion_skip_erlang_libs:
if re.search(regex, m['trigger']):
valid = False
break
if valid == True: completions.append(m)
# generate completion file
file_json['completions'] = completions
f = open(os.path.join(GLOBALS.ERL.plugin_path, 'completion', 'Erlang-Libs.sublime-completions'), 'w')
f.write(json.dumps(file_json))
f.close()
def set_completions_threaded(self):
this = self
class ErlThread(threading.Thread):
def run(self):
this.set_completions()
ErlThread().start()
def plugin_loaded():
sublime.set_timeout(lambda: ErlModuleNameCompletions().set_completions_threaded(), 5000)
# completions
class ErlCompletions(ErlProjectLoader):
def get_available_completions(self):
# load current erlang libs
if ERL_COMPLETIONS['erlang_libs']['completions'] == {}: self.load_erlang_lib_completions()
# start rebuilding: only done once per erl session
# [i.e. needs sublime text restart to regenerate erlang completions]
self.generate_erlang_lib_completions()
# generate & load project files
self.generate_project_completions()
def get_completion_filename(self, code_type):
if code_type == 'erlang_libs': return 'Erlang-Libs'
elif code_type == 'current_project': return 'Current-Project'
def load_erlang_lib_completions(self):
self.load_completions('erlang_libs')
def load_current_project_completions(self):
self.load_completions('current_project')
def load_completions(self, code_type):
# check lock
global ERL_COMPLETIONS
if ERL_COMPLETIONS[code_type]['load_in_progress'] == True: return
# set lock
ERL_COMPLETIONS[code_type]['load_in_progress'] = True
# load
this = self
class ErlThread(threading.Thread):
def run(self):
global ERL_COMPLETIONS
# load completetions from file
disasm_filepath = os.path.join(GLOBALS.ERL.plugin_path, "completion", "%s.disasm" % this.get_completion_filename(code_type))
if os.path.exists(disasm_filepath):
# load file
f = open(disasm_filepath, 'rb')
completions = pickle.load(f)
f.close()
# set
ERL_COMPLETIONS[code_type]['completions'] = completions
# release lock
ERL_COMPLETIONS[code_type]['load_in_progress'] = False
ErlThread().start()
def generate_erlang_lib_completions(self):
# check lock
global ERL_COMPLETIONS
if ERL_COMPLETIONS['erlang_libs']['rebuilt'] == True: return
# set lock
ERL_COMPLETIONS['erlang_libs']['rebuilt'] = True
# rebuild
this = self
class ErlThread(threading.Thread):
def run(self):
# get dirs
dest_file_base = os.path.join(GLOBALS.ERL.completions_path, "Erlang-Libs")
# get erlang libs info
current_erlang_libs = [name for name in os.listdir(GLOBALS.ERL.erlang_libs_path) if os.path.isdir(os.path.join(GLOBALS.ERL.erlang_libs_path, name))]
# read file of previous erlang libs
dirinfo_path = os.path.join(GLOBALS.ERL.completions_path, "Erlang-Libs.dirinfo")
if os.path.exists(dirinfo_path):
f = open(dirinfo_path, 'rb')
erlang_libs = pickle.load(f)
f.close()
if current_erlang_libs == erlang_libs:
# same erlang libs, do not regenerate
return
# different erlang libs -> regenerate
this.status("Regenerating Erlang lib completions...")
# set cwd
os.chdir(GLOBALS.ERL.support_path)
# start gen
this.execute_os_command("python erl_libparser.py %s %s" % (this.shellquote(this.project_root), this.shellquote(dest_file_base)))
# rename file to .full
os.remove("%s.sublime-completions.full" % dest_file_base)
os.rename("%s.sublime-completions" % dest_file_base, "%s.sublime-completions.full" % dest_file_base)
# save dir information
f = open(dirinfo_path, 'wb')
pickle.dump(current_erlang_libs, f)
f.close()
# regenerate completions based on options
ErlModuleNameCompletions().set_completions()
# trigger event to reload completions
this.load_erlang_lib_completions()
this.status("Finished regenerating Erlang lib completions.")
ErlThread().start()
def generate_project_completions(self):
# check lock
global ERL_COMPLETIONS
if ERL_COMPLETIONS['current_project']['rebuild_in_progress'] == True: return
# set lock
ERL_COMPLETIONS['current_project']['rebuild_in_progress'] = True
# rebuild
this = self
class ErlThread(threading.Thread):
def run(self):
global ERL_COMPLETIONS
this.status("Regenerating Project completions...")
# get dir
dest_file_base = os.path.join(GLOBALS.ERL.completions_path, "Current-Project")
# set cwd
os.chdir(GLOBALS.ERL.support_path)
# start gen
this.execute_os_command("python erl_libparser.py %s %s" % (this.shellquote(this.project_root), this.shellquote(dest_file_base)))
# release lock
ERL_COMPLETIONS['current_project']['rebuild_in_progress'] = False
# trigger event to reload completions
this.load_current_project_completions()
this.status("Finished regenerating Project completions.")
ErlThread().start()
# listener
class ErlCompletionsListener(sublime_plugin.EventListener):
# CALLBACK ON VIEW SAVE
def on_post_save(self, view):
# check init successful
if GLOBALS.ERL.initialized == False: return
# ensure context matches
caret = view.sel()[0].a
if not ('source.erlang' in view.scope_name(caret)): return
# init
completions = ErlCompletions(view)
# compile saved file & reload completions
class ErlThread(threading.Thread):
def run(self):
# trigger event to reload completions
completions.generate_project_completions()
ErlThread().start()
# CALLBACK ON VIEW LOADED
def on_load(self, view):
# check init successful
if GLOBALS.ERL.initialized == False: return
# only trigger within erlang
caret = view.sel()[0].a
if not ('source.erlang' in view.scope_name(caret)): return
# init
completions = ErlCompletions(view)
# get completions
class ErlThread(threading.Thread):
def run(self):
# trigger event to reload completions
completions.get_available_completions()
ErlThread().start()
# CALLBACK ON QUERY COMPLETIONS
def on_query_completions(self, view, prefix, locations):
# check init successful
if GLOBALS.ERL.initialized == False: return
# only trigger within erlang
if not view.match_selector(locations[0], "source.erlang"): return []
# only trigger if : was hit
pt = locations[0] - len(prefix) - 1
ch = view.substr(sublime.Region(pt, pt + 1))
if ch != ':': return []
# get function name that triggered the autocomplete
function_name = view.substr(view.word(pt))
if function_name.strip() == ':': return
# check for existance
global ERL_COMPLETIONS
if function_name in ERL_COMPLETIONS['erlang_libs']['completions']:
available_completions = ERL_COMPLETIONS['erlang_libs']['completions'][function_name]
elif function_name in ERL_COMPLETIONS['current_project']['completions']:
available_completions = ERL_COMPLETIONS['current_project']['completions'][function_name]
else:
return
# return snippets
return (available_completions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)