-
Notifications
You must be signed in to change notification settings - Fork 14
/
ycm_helpers.py
254 lines (201 loc) · 8.38 KB
/
ycm_helpers.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
#!/usr/bin/env python
import gevent.monkey; gevent.monkey.patch_all(subprocess=True)
import uuid
import tempfile
import os
import os.path
import errno
import gevent
import collections
import traceback
import atexit
import settings
from ycm import YCM
from filesync import FileSync
from projectinfo import ProjectInfo, RESOURCE_HEADER_NAME, MESSAGEKEY_HEADER_NAME
from npm_helpers import try_setup_dependencies, setup_dependencies
mapping = {}
YCMHolder = collections.namedtuple('YCMHolder', ('filesync', 'projectinfo', 'ycms'))
CodeCompletion = collections.namedtuple('CodeCompletion', ('kind', 'insertion_text', 'extra_menu_info', 'detailed_info'))
class YCMProxyException(Exception):
pass
def spinup(content):
root_dir = tempfile.mkdtemp()
platforms = set(content.get('platforms', ['aplite']))
sdk_version = content.get('sdk', '2')
dependencies = content.get('dependencies', {})
messagekeys = content.get('messagekeys', [])
resources = content.get('resources', [])
print "spinup in %s" % root_dir
# Dump all the files we should need.
for path, file_content in content['files'].iteritems():
abs_path = os.path.normpath(os.path.join(root_dir, path))
if not abs_path.startswith(root_dir):
raise Exception("Failed: escaped root directory.")
dir_name = os.path.dirname(abs_path)
try:
os.makedirs(dir_name)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(dir_name):
pass
else:
raise
with open(abs_path, 'w') as f:
f.write(file_content.encode('utf-8'))
filesync = FileSync(root_dir)
# Just ignore NPM failures on spinup since we don't wait them to kill YCM completely.
# The user will notice that something is wrong when their includes all show as errors.
(lib_info, lib_messagekeys, lib_resources), npm_error = try_setup_dependencies(dependencies, root_dir)
info = ProjectInfo(
messagekeys=messagekeys,
resources=resources,
lib_resources=lib_resources,
lib_messagekeys=lib_messagekeys
)
filesync.create_file(RESOURCE_HEADER_NAME, info.make_resource_ids_header())
filesync.create_file(MESSAGEKEY_HEADER_NAME, info.make_messagekey_header())
ycms = YCMHolder(filesync=filesync, projectinfo=info, ycms={})
print "created files"
settings_path = os.path.join(root_dir, ".ycm_extra_conf.py")
conf_mapping = {
'2': 'ycm_extra_conf_sdk2.py',
'3': 'ycm_extra_conf_sdk3.py',
}
sdk_mapping = {
'2': settings.PEBBLE_SDK2,
'3': settings.PEBBLE_SDK3,
}
with open(settings_path, "w") as f:
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ycm_conf', conf_mapping[sdk_version])) as template:
f.write(template.read().format(sdk=sdk_mapping[sdk_version], here=root_dir, stdlib=settings.STDLIB_INCLUDE_PATH))
try:
for platform in ('aplite', 'basalt', 'chalk', 'diorite'):
if platform in platforms:
ycm = YCM(filesync, platform)
ycm.wait()
ycm.apply_settings(settings_path)
ycms.ycms[platform] = ycm
except Exception as e:
print "Failed to spawn ycm with root_dir %s" % root_dir
print traceback.format_exc()
return dict(success=False, error=str(e))
# Keep track of it
this_uuid = str(uuid.uuid4())
mapping[this_uuid] = ycms
# print mapping
print "spinup complete (%s); %s -> %s" % (platforms, this_uuid, root_dir)
# victory!
return dict(success=True, uuid=this_uuid, libraries=lib_info, npm_error=npm_error)
def get_completions(ycms, data):
if 'patches' in data:
ycms.filesync.apply_patches(data['patches'])
completions = collections.OrderedDict()
completion_start_column = None
for platform, ycm in sorted(ycms.ycms.iteritems(), reverse=True):
ycm.parse(data['file'], data['line'], data['ch']) # TODO: Should we do this here?
platform_completions = ycm.get_completions(data['file'], data['line'], data['ch'])
if platform_completions is not None:
completion_start_column = platform_completions['completion_start_column']
for completion in platform_completions['completions']:
if completion['insertion_text'] in completions:
continue
completions[completion['insertion_text']] = completion
return dict(
completions=completions.values(),
start_column=completion_start_column,
)
def get_ycms(process_uuid):
if process_uuid not in mapping:
raise YCMProxyException("UUID not found")
return mapping[process_uuid]
def get_errors(ycms, data):
if 'patches' in data:
ycms.filesync.apply_patches(data['patches'])
errors = {}
for platform, ycm in sorted(ycms.ycms.iteritems(), reverse=True):
result = ycm.parse(data['file'], data['line'], data['ch'])
if result is None:
continue
if 'exception' in result:
continue
for error in result:
error_key = (error['kind'], error['location']['line_num'], error['text'])
if error_key in errors:
errors[error_key]['platforms'].append(platform)
else:
error['platforms'] = [platform]
errors[error_key] = error
return dict(
errors=errors.values()
)
def go_to(ycms, data):
if 'patches' in data:
ycms.filesync.apply_patches(data['patches'])
for platform, ycm in sorted(ycms.ycms.iteritems(), reverse=True):
ycm.parse(data['file'], data['line'], data['ch'])
result = ycm.go_to(data['file'], data['line'], data['ch'])
if result is not None:
return dict(location=result)
return dict(location=None)
def update_dependencies(ycms, data):
info = ycms.projectinfo
filesync = ycms.filesync
lib_info, new_messagekeys, new_resources = setup_dependencies(data['dependencies'], filesync.root_dir)
info.lib_resources = new_resources
info.lib_messagekeys = new_messagekeys
filesync.create_file(RESOURCE_HEADER_NAME, info.make_resource_ids_header())
filesync.create_file(MESSAGEKEY_HEADER_NAME, info.make_messagekey_header())
return {'libraries': lib_info}
def update_resources(ycms, data):
info = ycms.projectinfo
filesync = ycms.filesync
info.resources = data['resources']
filesync.create_file(RESOURCE_HEADER_NAME, info.make_resource_ids_header())
def update_messagekeys(ycms, data):
info = ycms.projectinfo
filesync = ycms.filesync
info.messagekeys = data['messagekeys']
filesync.create_file(MESSAGEKEY_HEADER_NAME, info.make_messagekey_header())
def create_file(ycms, data):
ycms.filesync.create_file(data['filename'], data['content'])
def delete_file(ycms, data):
ycms.filesync.delete_file(data['filename'])
def rename_file(ycms, data):
ycms.filesync.rename_file(data['filename'], data['new_filename'])
def ping(ycms, data=None):
for ycm in ycms.ycms.itervalues():
if not ycm.ping():
raise YCMProxyException("Failed to ping YCM")
def kill_completer(process_uuid):
global mapping
if process_uuid in mapping:
for platform, ycm in mapping[process_uuid].ycms.iteritems():
print "killing %s:%s (alive: %s)" % (process_uuid, platform, ycm.alive)
ycm.close()
del mapping[process_uuid]
else:
print "no uuid %s to kill" % process_uuid
def kill_completers():
global mapping
for ycms in mapping.itervalues():
for ycm in ycms.ycms.itervalues():
ycm.close()
mapping = {}
def monitor_processes():
global mapping
def monitor(process_mapping):
while True:
print "process sweep running"
gevent.sleep(20)
to_kill = set()
for process_uuid, ycms in process_mapping.iteritems():
for platform, ycm in ycms.ycms.iteritems():
if not ycm.alive:
print "killing %s:%s (alive: %s)" % (process_uuid, platform, ycm.alive)
ycm.close()
to_kill.add(process_uuid)
for process_uuid in to_kill:
del process_mapping[process_uuid]
print "process sweep collected %d instances" % len(to_kill)
g = gevent.spawn(monitor, mapping)
atexit.register(lambda: g.kill())