-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsublime_python_commands.py
176 lines (146 loc) · 6.1 KB
/
sublime_python_commands.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
import sublime
import sublime_plugin
from SublimePythonIDE.sublime_python import proxy_for, root_folder_for,\
get_setting, file_or_buffer_name, GOTO_STACK, python_only
class PythonCompletionsListener(sublime_plugin.EventListener):
'''Retrieves completion proposals from external Python
processes running Rope'''
@python_only
def on_query_completions(self, view, prefix, locations):
path = file_or_buffer_name(view)
source = view.substr(sublime.Region(0, view.size()))
loc = view.rowcol(locations[0])
# t0 = time.time()
proxy = proxy_for(view)
if not proxy:
return []
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
@python_only
def on_post_save_async(self, view, *args):
proxy = proxy_for(view)
if not proxy:
return
path = file_or_buffer_name(view)
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.'''
@python_only
def run(self):
view = self.window.active_view()
row, col = view.rowcol(view.sel()[0].a)
offset = view.text_point(row, col)
path = file_or_buffer_name(view)
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)
if not proxy:
return
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 PythonGotoDefinitionCommand(sublime_plugin.WindowCommand):
'''
Shows the definition of the identifier under the cursor, project-wide.
'''
@python_only
def run(self, *args):
view = self.window.active_view()
row, col = view.rowcol(view.sel()[0].a)
offset = view.text_point(row, col)
path = file_or_buffer_name(view)
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)
if not proxy:
return
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_rowcol = view.rowcol(view.sel()[0].end())
current_lineno = current_rowcol[0] + 1
current_colno = current_rowcol[1] + 1
if None not in (path, target_path, target_lineno):
self.save_pos(file_or_buffer_name(view), current_lineno, current_colno)
path = target_path + ":" + str(target_lineno)
self.window.open_file(path, sublime.ENCODED_POSITION)
elif target_lineno is not None:
self.save_pos(file_or_buffer_name(view), current_lineno, current_colno)
path = file_or_buffer_name(view) + ":" + 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, colno=0):
GOTO_STACK.append((file_path, lineno, colno))
class PythonGoBackCommand(sublime_plugin.WindowCommand):
@python_only
def run(self, *args):
if GOTO_STACK:
file_name, lineno, colno = GOTO_STACK.pop()
path = "%s:%d:%d" % (file_name, lineno, colno)
self.window.open_file(path, sublime.ENCODED_POSITION)