-
Notifications
You must be signed in to change notification settings - Fork 2
/
sycommands.py
executable file
·265 lines (211 loc) · 8.21 KB
/
sycommands.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
#!/usr/bin/env python3
'''
sycommands.py
Main Sublime Text Commands. Utilities for controlling or querying ycmd.
'''
import logging
import logging.config
from .lib.subl.view import (
get_path_for_view,
get_file_types,
)
from .lib.util.format import (
json_pretty_print,
json_flat_iterator,
)
from .plugin.state import get_plugin_state
from .plugin.ui import display_plugin_message
logger = logging.getLogger('sublime-ycmd.' + __name__)
try:
import sublime_plugin
except ImportError:
from .lib.subl.dummy import sublime_plugin
class SublimeYcmdEditSettings(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
window = view.window()
if not window:
logger.error('no window, cannot launch settings')
return
settings_base_file = \
'${packages}/YouCompleteMe/sublime-ycmd.sublime-settings'
settings_placeholder = (
'{\n'
'\t\"ycmd_root_directory\": \"$0\"\n'
'}\n'
)
window.run_command('edit_settings', {
'base_file': settings_base_file,
'default': settings_placeholder,
})
class SublimeYcmdListServers(sublime_plugin.TextCommand):
def run(self, edit):
state = get_plugin_state()
if not state:
return
window = self.view.window()
if not window:
logger.warning('failed to get window, cannot display servers')
return
servers = state.servers
if not servers:
logger.debug('no servers are active')
def on_select_empty(selection_index):
pass
window.show_quick_panel([
['empty', 'no servers are running']
], on_select_empty)
return
panel_options = [
[server.label or '', server.pretty_str()] for server in servers
]
def on_select_server(selection_index):
# do nothing for now
# in the future, this may be used for server management as well
pass
window.show_quick_panel(panel_options, on_select_server)
def description(self):
return 'list ycmd servers'
class SublimeYcmdShowViewInfo(sublime_plugin.TextCommand):
def run(self, edit):
state = get_plugin_state()
if not state:
return
view = self.view
window = view.window()
if not window:
logger.warning('no window, cannot display info')
return
server = state.lookup_server(view)
if server:
server_desc = server.pretty_str()
else:
server_desc = 'none'
view_working_directory = get_path_for_view(view)
if not view_working_directory:
view_working_directory = 'none'
view_file_types = get_file_types(view)
if not view_file_types:
view_file_types = []
if hasattr(view_file_types, '__iter__'):
view_file_types = ', '.join(view_file_types)
else:
view_file_types = str(view_file_types)
def on_select_info(selection_index):
pass
window.show_quick_panel([
['Server', server_desc],
['Directory', view_working_directory],
['Type', view_file_types],
], on_select_info)
class SublimeYcmdGetDebugInfo(sublime_plugin.TextCommand):
def run(self, edit):
state = get_plugin_state()
if not state:
return
view = state.lookup_view(self.view)
window = self.view.window()
if not view or not view.ready():
logger.info('view has not been seen by plugin, ignoring it')
display_plugin_message('view is ignored, cannot get debug info')
return
server = state.lookup_server(view)
if not server:
logger.info('no server for view, cannot get debug info')
display_plugin_message('view has no associated ycmd server')
return
if not (server.is_starting() or server.is_alive(timeout=0)):
logger.info('server is unavailable, cannot get debug info')
display_plugin_message('server is unavailable: %s' % (server))
return
request_params = view.generate_request_parameters()
if not request_params:
logger.info('failed to generate request params, abort')
display_plugin_message('failed to generate parameters from view')
return
logger.info('sending request for server debug info: %r', server)
try:
debug_info = server.get_debug_info(request_params, timeout=1)
except TimeoutError: # noqa
logger.info('request timed out...')
display_plugin_message('request timed out, server: %s' % (server))
return
if debug_info:
pretty_debug_info = json_pretty_print(debug_info)
display_plugin_message('got debug info: %s' % (pretty_debug_info))
else:
display_plugin_message('got debug info: %s' % (debug_info))
def on_select_info(selection_index):
pass
if window and debug_info:
# NOTE : Quick panel API is very picky, needs to be `list` & `str`:
flattened_properties = list(sorted(
([str(k), str(v)] for k, v in json_flat_iterator(debug_info)),
key=lambda i: i[0],
))
window.show_quick_panel(flattened_properties, on_select_info)
class SublimeYcmdManageServer(sublime_plugin.TextCommand):
def run(self, edit):
state = get_plugin_state()
if not state:
return
view = state.lookup_view(self.view)
window = self.view.window()
if not view or not view.ready():
logger.debug('view has not been seen by plugin, ignoring it')
display_plugin_message('view is ignored, cannot manage its server')
return
server = state.lookup_server(view)
if not server:
logger.debug('no server for view, cannot manage it')
display_plugin_message('view has no associated ycmd server')
return
if not (server.is_starting() or server.is_alive(timeout=0)):
logger.debug('server is unavailable, cannot manage it')
display_plugin_message('server is unavailable: %s' % (server))
return
startup_parameters = server.startup_parameters
stdout_log_path = startup_parameters.stdout_log_path
stderr_log_path = startup_parameters.stderr_log_path
def _describe_log_path():
if not stdout_log_path or not stderr_log_path:
return 'buffered in-memory'
if startup_parameters.keep_logs:
return 'retained temporary files'
return 'auto-deleted temporary files'
server_desc = server.pretty_str()
process_desc = 'process id: %s' % (server.pid)
log_desc = _describe_log_path()
def on_select_item(selection_index):
if selection_index == -1:
pass
elif selection_index == 0:
shutdown_server()
elif selection_index == 1:
kill_server()
elif selection_index == 2:
open_logs()
else:
logger.warning(
'unhandled selection index for manage server: %r',
selection_index,
)
raise ValueError(
'unknown selection, index: %r' % (selection_index)
)
def shutdown_server():
server.stop(hard=False, timeout=1)
def kill_server():
server.stop(hard=True, timeout=1)
def open_logs():
if not stdout_log_path or not stderr_log_path:
display_plugin_message('cannot display in-memory logs')
# TODO : Use `read_spooled_output` to extract logs and display.
raise NotImplementedError
window.open_file(stdout_log_path)
window.open_file(stderr_log_path)
window.show_quick_panel([
['Shutdown', server_desc],
['Kill', process_desc],
['Logs', log_desc],
], on_select_item)