forked from JulianEberius/SublimePythonIDE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublime_python.py
More file actions
443 lines (381 loc) · 15 KB
/
sublime_python.py
File metadata and controls
443 lines (381 loc) · 15 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import sys
import os
import socket
import time
import itertools
import subprocess
import pipes
import threading
import xmlrpc.client
import sublime
import sublime_plugin
# contains root paths for each view, see root_folder_for()
ROOT_PATHS = {}
# contains proxy objects for external Python processes, by interpreter used
PROXIES = {}
# lock for aquiring proxy instances
PROXY_LOCK = threading.RLock()
# contains errors found by PyFlask
ERRORS_BY_LINE = {}
# saves positions on goto_definition
GOTO_STACK = []
# Constants
SERVER_SCRIPT = pipes.quote(os.path.join(
os.path.dirname(__file__), "server/server.py"))
RETRY_CONNECTION_LIMIT = 5
HEARTBEAT_FREQUENCY = 9
DRAW_TYPE = 4 | 32
NO_ROOT_PATH = -1
def get_setting(key, view=None, default_value=None):
if view is None:
view = sublime.active_window().active_view()
try:
settings = view.settings()
if settings.has(key):
return settings.get(key)
except:
pass
s = sublime.load_settings('SublimePython.sublime-settings')
return s.get(key, default_value)
class Proxy(object):
'''Abstracts the external Python processes that do the actual
work. SublimePython just calls local methods on Proxy objects.
The Proxy objects start external Python processes, send them heartbeat
messages, communicate with them and restart them if necessary.'''
def __init__(self, python):
self.python = python
self.proc = None
self.proxy = None
self.port = None
self.restart()
def get_free_port(self):
s = socket.socket()
s.bind(('', 0))
port = s.getsockname()[1]
s.close()
return port
def restart(self):
self.port = self.get_free_port()
self.proc = subprocess.Popen(
"%s %s %i" % (self.python, SERVER_SCRIPT, self.port),
shell=True
)
print("starting server on port %i with %s" % (self.port, self.python))
self.proxy = xmlrpc.client.ServerProxy(
'http://localhost:%i' % self.port, allow_none=True)
self.set_heartbeat_timer()
def set_heartbeat_timer(self):
sublime.set_timeout_async(
self.send_heartbeat, HEARTBEAT_FREQUENCY * 1000)
def stop(self):
self.proxy = None
self.proc.terminate()
def send_heartbeat(self):
if self.proxy:
self.proxy.heartbeat()
self.set_heartbeat_timer()
def __getattr__(self, attr):
'''deletegate all other calls to the xmlrpc client.
wait if the server process is still runnning, but not responding
if the server process has died, restart it'''
def wrapper(*args, **kwargs):
if not self.proxy:
self.restart()
time.sleep(0.2)
method = getattr(self.proxy, attr)
result = None
tries = 0
while tries < RETRY_CONNECTION_LIMIT:
try:
result = method(*args, **kwargs)
break
except Exception:
tries += 1
if self.proc.poll() is None:
# just retry
time.sleep(0.2)
else:
# died, restart and retry
self.restart()
time.sleep(0.2)
return result
return wrapper
def proxy_for(view):
'''retrieve an existing proxy for an external Python process.
will automatically create a new proxy if non exists for the
requested interpreter'''
proxy = None
with PROXY_LOCK:
python = get_setting("python_interpreter", view, "")
if python == "":
python = "python"
if python in PROXIES:
proxy = PROXIES[python]
else:
proxy = Proxy(python)
PROXIES[python] = proxy
return proxy
def root_folder_for(view):
'''returns the folder open in ST which contains
the file open in this view. Used to determine the
rope project directory (assumes directories open in
ST == project directory)'''
def in_directory(file_path, directory):
directory = os.path.realpath(directory)
file_path = os.path.realpath(file_path)
return os.path.commonprefix([file_path, directory]) == directory
file_name = view.file_name()
root_path = None
if file_name in ROOT_PATHS:
root_path = ROOT_PATHS[file_name]
else:
for folder in view.window().folders():
if in_directory(file_name, folder):
root_path = folder
ROOT_PATHS[file_name] = root_path
# no folders found -> single file project
if root_path is None:
root_path = NO_ROOT_PATH
return root_path
class PythonStopServerCommand(sublime_plugin.WindowCommand):
def run(self, *args):
with PROXY_LOCK:
python = get_setting("python_interpreter", "")
if python == "":
python = "python"
proxy = PROXIES.get(python, None)
if proxy:
proxy.stop()
del PROXIES[python]
class PythonTestCommand(sublime_plugin.WindowCommand):
def run(self, *args):
view = self.window.active_view()
proxy = proxy_for(view)
print("projects:", proxy.list_projects())
class PythonCheckSyntaxListener(sublime_plugin.EventListener):
def is_python_syntax(self, view):
syntax = view.settings().get('syntax')
return bool(syntax and ("Python" in syntax))
def on_load_async(self, view):
'''Check the file syntax on load'''
if not self.is_python_syntax(view) or view.is_scratch():
return
self._check(view)
def on_activated_async(self, view):
'''Check the file syntax on activated'''
if not self.is_python_syntax(view) or view.is_scratch():
return
self._check(view)
def on_post_save_async(self, view):
'''Check the file syntax on save'''
if not self.is_python_syntax(view) or view.is_scratch():
return
self._check(view)
def on_selection_modified_async(self, view):
if (not self.is_python_syntax(view)
or not get_setting('pyflakes_linting', view, True)):
return
vid = view.id()
errors_by_line = ERRORS_BY_LINE.get(vid, None)
if not errors_by_line:
view.erase_status('sublimepython-errors')
return
lineno = view.rowcol(view.sel()[0].end())[0] + 1
if lineno in errors_by_line:
view.set_status('sublimepython-errors', '; '.join(
[m['message'] % tuple(m['message_args'])
for m in errors_by_line[lineno]]
))
else:
view.erase_status('sublimepython-errors')
def _check(self, view):
if not get_setting('pyflakes_linting', view, True):
return
proxy = proxy_for(view)
check_result = proxy.check_syntax(
view.substr(sublime.Region(0, view.size())))
# the result can be a list of errors, or single syntax exception
if isinstance(check_result, list):
by_line = lambda e: e['lineno']
errors = sorted(check_result, key=by_line)
errors_by_line = {}
for k, g in itertools.groupby(errors, by_line):
errors_by_line[k] = list(g)
ERRORS_BY_LINE[view.id()] = errors_by_line
self.visualize_errors(view, errors)
else:
self.handle_syntax_exception(view, check_result)
self.on_selection_modified_async(view)
def visualize_errors(self, view, errors):
view.erase_regions('sublimepython-errors')
errors_by_line = ERRORS_BY_LINE[view.id()]
outlines = [view.line(view.text_point(lineno - 1, 0))
for lineno in errors_by_line.keys()]
if outlines:
view.add_regions(
'sublimepython-errors', outlines, 'keyword', 'dot',
DRAW_TYPE)
else:
view.erase_regions("sublimepython-errors")
def handle_syntax_exception(self, view, e):
if not get_setting('pyflakes_linting', view, True):
return
if e is None:
return
(lineno, offset, text) = e["lineno"], e["offset"], e["text"]
if text is None:
print >> sys.stderr, "SublimePython error decoding src file %s" % (
self.filename)
else:
ERRORS_BY_LINE[view.id()] = {
lineno: [{"message": "Syntax error", "message_args": ()}]}
line = text.splitlines()[-1]
if offset is not None:
offset = offset - (len(text) - len(line))
view.erase_regions('sublimepython-errors')
if offset is not None:
text_point = view.text_point(lineno - 1, 0) + offset
view.add_regions(
'sublimepython-errors',
[sublime.Region(text_point, text_point + 1)],
'keyword', 'dot', DRAW_TYPE)
else:
view.add_regions(
'sublimepython-errors',
[view.line(view.text_point(lineno - 1, 0))],
'keyword', 'dot', DRAW_TYPE)
class PythonCompletionsListener(sublime_plugin.EventListener):
'''Retrieves completion proposals from external Python
processes running Rope'''
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0], 'source.python'):
return []
path = view.file_name()
source = view.substr(sublime.Region(0, view.size()))
loc = locations[0]
# t0 = time.time()
proxy = proxy_for(view)
proposals = proxy.completions(source, root_folder_for(view), path, loc)
# proposals = (
# proxy.profile_completions(source, root_folder_for(view), path, loc)
# )
# print("+++", time.time() - t0)
if proposals:
completion_flags = (
sublime.INHIBIT_WORD_COMPLETIONS |
sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
return (proposals, completion_flags)
return proposals
def on_post_save_async(self, view, *args):
proxy = proxy_for(view)
path = view.file_name()
proxy.report_changed(root_folder_for(view), path)
class PythonGetDocumentationCommand(sublime_plugin.WindowCommand):
'''Retrieves the docstring for the identifier under the cursor and
displays it in a new panel.'''
def run(self):
view = self.window.active_view()
row, col = view.rowcol(view.sel()[0].a)
offset = view.text_point(row, col)
path = view.file_name()
source = view.substr(sublime.Region(0, view.size()))
if view.substr(offset) in [u'(', u')']:
offset = view.text_point(row, col - 1)
proxy = proxy_for(view)
doc = proxy.documentation(source, root_folder_for(view), path, offset)
if doc:
open_pydoc_in_view = get_setting("open_pydoc_in_view")
if open_pydoc_in_view:
self.display_docs_in_view(doc)
else:
self.display_docs_in_panel(view, doc)
else:
word = view.substr(view.word(offset))
self.notify_no_documentation(view, word)
def notify_no_documentation(self, view, word):
view.set_status(
"rope_documentation_error",
"No documentation found for %s" % word
)
def clear_status_callback():
view.erase_status("rope_documentation_error")
sublime.set_timeout_async(clear_status_callback, 5000)
def display_docs_in_panel(self, view, doc):
out_view = view.window().get_output_panel(
"rope_python_documentation")
out_view.run_command("simple_clear_and_insert", {"insert_string": doc})
view.window().run_command(
"show_panel", {"panel": "output.rope_python_documentation"})
def display_docs_in_view(self, doc):
create_view_in_same_group = get_setting("create_view_in_same_group")
v = self.find_pydoc_view()
if not v:
active_group = self.window.active_group()
if not create_view_in_same_group:
if self.window.num_groups() == 1:
self.window.run_command('new_pane', {'move': False})
if active_group == 0:
self.window.focus_group(1)
else:
self.window.focus_group(active_group-1)
self.window.new_file(sublime.TRANSIENT)
v = self.window.active_view()
v.set_name("*pydoc*")
v.set_scratch(True)
v.set_read_only(False)
v.run_command("simple_clear_and_insert", {"insert_string": doc})
v.set_read_only(True)
self.window.focus_view(v)
def find_pydoc_view(self):
'''
Return view named *pydoc* if exists, None otherwise.
'''
for w in self.window.views():
if w.name() == "*pydoc*":
return w
return None
class SimpleClearAndInsertCommand(sublime_plugin.TextCommand):
def run(self, edit, block=False, **kwargs):
doc = kwargs['insert_string']
r = sublime.Region(0, self.view.size())
self.view.erase(edit, r)
self.view.insert(edit, 0, doc)
class PythonGotoDefinitionCommand(sublime_plugin.WindowCommand):
'''
Shows the definition of the identifier under the cursor, project-wide.
'''
def run(self, *args):
view = self.window.active_view()
row, col = view.rowcol(view.sel()[0].a)
offset = view.text_point(row, col)
path = view.file_name()
source = view.substr(sublime.Region(0, view.size()))
if view.substr(offset) in [u'(', u')']:
offset = view.text_point(row, col - 1)
proxy = proxy_for(view)
def_result = proxy.definition_location(
source, root_folder_for(view), path, offset)
if not def_result or def_result == [None, None]:
return
target_path, target_lineno = def_result
current_lineno = view.rowcol(view.sel()[0].end())[0] + 1
if None not in (path, target_path, target_lineno):
self.save_pos(view.file_name(), current_lineno)
path = target_path + ":" + str(target_lineno)
self.window.open_file(path, sublime.ENCODED_POSITION)
elif target_lineno is not None:
self.save_pos(view.file_name(), current_lineno)
path = view.file_name() + ":" + str(target_lineno)
self.window.open_file(path, sublime.ENCODED_POSITION)
else:
# fail silently (user selected whitespace, etc)
pass
def save_pos(self, file_path, lineno):
GOTO_STACK.append((file_path, lineno))
class PythonGoBackCommand(sublime_plugin.WindowCommand):
def run(self, *args):
if GOTO_STACK:
file_name, lineno = GOTO_STACK.pop()
path = file_name + ":" + str(lineno)
self.window.open_file(path, sublime.ENCODED_POSITION)