-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtags.py
371 lines (296 loc) · 13.1 KB
/
rtags.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
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
import collections
import sublime
import sublime_plugin
import subprocess
import threading
import re
import xml.etree.ElementTree as etree
# sublime-rtags settings
settings = None
# path to rc utility
RC_PATH = ''
def run_rc(switches, input=None, *args):
timeout = 0.5
if settings != None:
timeout = settings.get('rc_timeout', timeout)
p = subprocess.Popen([RC_PATH] + switches + list(args),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
print(' '.join(p.args))
return p.communicate(input=input, timeout=timeout)
# TODO refactor somehow to remove global vars
class NavigationHelper(object):
NAVIGATION_REQUESTED = 1
NAVIGATION_DONE = 2
def __init__(self):
# navigation indicator, possible values are:
# - NAVIGATION_REQUESTED
# - NAVIGATION_DONE
self.flag = NavigationHelper.NAVIGATION_DONE
# rc utility switches to use for callback
self.switches = []
# file contents that has been passed to reindexer last time
self.data = ''
# history of navigations
# elements are tuples (filename, line, col)
self.history = collections.deque()
class RConnectionThread(threading.Thread):
def notify(self):
sublime.active_window().active_view().run_command('rtags_location',
{'switches': navigation_helper.switches})
def run(self):
self.p = subprocess.Popen([RC_PATH, '-m', '--silent-query'],
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# `rc -m` will feed stdout with xml like this:
#
# <?xml version="1.0" encoding="utf-8"?>
# <checkstyle>
# <file name="/home/ramp/tmp/pthread_simple.c">
# <error line="54" column="5" severity="warning" message="implicit declaration of function 'sleep' is invalid in C99"/>
# <error line="59" column="5" severity="warning" message="implicit declaration of function 'write' is invalid in C99"/>
# <error line="60" column="5" severity="warning" message="implicit declaration of function 'lseek' is invalid in C99"/>
# <error line="78" column="7" severity="warning" message="implicit declaration of function 'read' is invalid in C99"/>
# </file>
# </checkstyle>
# <?xml version="1.0" encoding="utf-8"?>
# <progress index="1" total="1"></progress>
#
# So we need to split xml chunks somehow
# Will start by looking for opening tag (<checkstyle, <progress)
# and parse accumulated xml when we encounter closing tag
# TODO deal with < /> style tags
rgxp = re.compile(r'<(\w+)')
buffer = '' # xml to be parsed
start_tag = ''
for line in iter(self.p.stdout.readline, b''):
line = line.decode('utf-8')
self.p.poll()
if not start_tag:
start_tag = re.findall(rgxp, line)
start_tag = start_tag[0] if len(start_tag) else ''
buffer += line
if '</{}>'.format(start_tag) in line:
tree = etree.fromstring(buffer)
# OK, we received some chunk
# check if it is progress update
if (tree.tag == 'progress' and
tree.attrib['index'] == tree.attrib['total'] and
navigation_helper.flag == NavigationHelper.NAVIGATION_REQUESTED):
# notify about event
sublime.set_timeout(self.notify, 10)
buffer = ''
start_tag = ''
self.p = None
def stop(self):
if self.is_alive():
self.p.kill()
self.p = None
def get_view_text(view):
return bytes(view.substr(sublime.Region(0, view.size())), "utf-8")
reg = r'(\S+):(\d+):(\d+):(.*)'
class RtagsBaseCommand(sublime_plugin.TextCommand):
def run(self, edit, switches, *args, **kwargs):
# do nothing if not called from supported code
if not supported_file_type(self.view):
return
# file should be reindexed only when
# 1. file buffer is dirty (modified)
# 2. there is no pending reindexation (navigation_helper flag)
# 3. current text is different from previous one
# It takes ~40-50 ms to reindex 2.5K C file and
# miserable amount of time to check text difference
if (navigation_helper.flag == NavigationHelper.NAVIGATION_DONE and
self.view.is_dirty() and
navigation_helper.data != get_view_text(self.view)):
navigation_helper.switches = switches
navigation_helper.data = get_view_text(self.view)
navigation_helper.flag = NavigationHelper.NAVIGATION_REQUESTED
self._reindex(self.view.file_name())
# never go further
return
out, err = run_rc(switches, None, self._query(*args, **kwargs))
# dirty hack
# TODO figure out why rdm responds with 'Project loading'
# for now just repeat query
if out == b'Project loading\n':
def rerun():
self.view.run_command('rtags_location', {'switches': switches})
sublime.set_timeout_async(rerun, 500)
return
# drop the flag, we are going to navigate
navigation_helper.flag = NavigationHelper.NAVIGATION_DONE
navigation_helper.switches = []
self._action(out, err)
def _reindex(self, filename):
run_rc(['-V'], get_view_text(self.view), filename,
'--unsaved-file', '{}:{}'.format(filename, self.view.size()))
def on_select(self, res):
if res == -1:
return
(file, line, col, _) = re.findall(reg, self.last_references[res])[0]
nrow, ncol = self.view.rowcol(self.view.sel()[0].a)
navigation_helper.history.append(
(self.view.file_name(), nrow + 1, ncol + 1))
if len(navigation_helper.history) > int(settings.get('jump_limit', 10)):
navigation_helper.history.popleft()
view = self.view.window().open_file(
'%s:%s:%s' % (file, line, col), sublime.ENCODED_POSITION)
def on_highlight(self, res):
if res == -1:
return
(file, line, col, _) = re.findall(reg, self.last_references[res])[0]
nrow, ncol = self.view.rowcol(self.view.sel()[0].a)
view = self.view.window().open_file(
'%s:%s:%s' % (file, line, col), sublime.ENCODED_POSITION | sublime.TRANSIENT)
def _query(self, *args, **kwargs):
return ''
def _validate(self, stdout, stderr):
if stdout != b'Not indexed\n':
return True
self.view.window().show_quick_panel(
["Not indexed"],
None,
sublime.MONOSPACE_FONT,
-1,
None)
return False
def _action(self, stdout, stderr):
if not self._validate(stdout, stderr):
return
# pretty format the results
items = list(map(lambda x: x.decode('utf-8'), stdout.splitlines()))
self.last_references = items
def out_to_items(item):
(file, line, _, usage) = re.findall(reg, item)[0]
return [usage.strip(), "{}:{}".format(file.split('/')[-1], line)]
items = list(map(out_to_items, items))
# if there is only one result no need to show it to user
# just do navigation directly
if len(items) == 1:
self.on_select(0)
return
# else show all available options
self.view.window().show_quick_panel(
items, self.on_select, sublime.MONOSPACE_FONT, -1, self.on_highlight)
class RtagsGoBackwardCommand(sublime_plugin.TextCommand):
def run(self, edit):
try:
file, line, col = navigation_helper.history.pop()
view = self.view.window().open_file(
'%s:%s:%s' % (file, line, col), sublime.ENCODED_POSITION)
except IndexError:
pass
class RtagsSymbolNameCommand(RtagsBaseCommand):
def _query(self, *args, **kwargs):
return self.view.substr(self.view.word(self.view.sel()[0]))
class RtagsLocationCommand(RtagsBaseCommand):
def _query(self, *args, **kwargs):
row, col = self.view.rowcol(self.view.sel()[0].a)
return '{}:{}:{}'.format(self.view.file_name(),
row + 1, col + 1)
class RtagsSymbolInfoCommand(RtagsLocationCommand):
panel_name = 'cursor'
inforeg = r'(\S+):\s*(.+)'
def filter_items(self, item):
return re.match(self.inforeg, item)
def _action(self, out, err):
if not self._validate(out, err):
return
items = list(map(lambda x: x.decode('utf-8'), out.splitlines()))
items = list(filter(self.filter_items, items))
def out_to_items(item):
(title, info) = re.findall(self.inforeg, item)[0]
return [info.strip(), title.strip()]
def out_file_to_items(item):
(file, line, _, usage) = re.findall(reg, item)[0]
return [usage.strip(), "{}:{}".format(file.split('/')[-1], line)]
items = list(map(out_to_items, items))
self.last_references = items
self.view.window().show_quick_panel(
items,
None,
sublime.MONOSPACE_FONT,
-1,
None)
class RtagsNavigationListener(sublime_plugin.EventListener):
# def on_modified(self, view):
# if view.scope_name(0).split()[0] in ('source.c++',
# 'source.c'):
# navigation_helper.flag =
def on_post_save(self, v):
# do nothing if not called from supported code
if not supported_file_type(v):
return
# run rc --check-reindex to reindex just saved files
run_rc(['-x'], None, v.file_name())
def on_post_text_command(self, view, command_name, args):
# do nothing if not called from supported code
if not supported_file_type(view):
return
# if view get 'clean' after undo check if we need reindex
if command_name == 'undo' and not view.is_dirty():
run_rc(['-V'], None, view.file_name())
class RtagsCompleteListener(sublime_plugin.EventListener):
# TODO refactor
def _query(self, *args, **kwargs):
pos = args[0]
row, col = self.view.rowcol(pos)
return '{}:{}:{}'.format(self.view.file_name(),
row + 1, col + 1)
def on_query_completions(self, v, prefix, location):
switches = ['-l'] # rc's auto-complete switch
self.view = v
# libcland does auto-complete _only_ at whitespace and punctuation chars
# so "rewind" location to that character
location = location[0] - len(prefix)
# do nothing if not called from supported code
if not supported_file_type(v):
return []
# We launch rc utility with both filename:line:col and filename:length
# because we're using modified file which is passed via stdin (see --unsaved-file
# switch)
out, err = run_rc(switches, get_view_text(self.view),
self._query(location),
'--unsaved-file',
# filename:length
'{}:{}'.format(v.file_name(), v.size()),
'--synchronous-completions' # no async)
)
sugs = []
for line in out.splitlines():
# line is like this
# "process void process(CompletionThread::Request *request) CXXMethod"
# "reparseTime int reparseTime VarDecl"
# "dump String dump() CXXMethod"
# "request CompletionThread::Request * request ParmDecl"
# we want it to show as process()\tCXXMethod
#
# output is list of tuples: first tuple element is what we see in popup menu
# second is what inserted into file. '$0' is where to place cursor.
# TODO play with $1, ${2:int}, ${3:string} and so on
elements = line.decode('utf-8').split()
sugs.append(('{}\t{}'.format(' '.join(elements[1:-1]), elements[-1]),
'{}$0'.format(elements[0])))
# inhibit every possible auto-completion
return sugs, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
def supported_file_type(view):
if settings == None:
return False
file_types = settings.get('file_types', ["source.c", "source.c++"])
return view.scope_name(view.sel()[0].a).split()[0] in file_types
def update_settings():
globals()['settings'] = sublime.load_settings(
'sublime-rtags.sublime-settings')
globals()['RC_PATH'] = settings.get('rc_path', 'rc')
def init():
update_settings()
globals()['navigation_helper'] = NavigationHelper()
globals()['rc_thread'] = RConnectionThread()
rc_thread.start()
settings.add_on_change('rc_path', update_settings)
def plugin_loaded():
sublime.set_timeout(init, 200)
def plugin_unloaded():
# stop `rc -m` thread
sublime.set_timeout(rc_thread.stop, 100)