-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtags_symbol.py
417 lines (334 loc) · 11.8 KB
/
gtags_symbol.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
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
import os
import re
import platform
import subprocess
import unittest
import time
import logging
import shlex
import queue
import SublimeGtags.gtags_project
import threading
import sublime
from SublimeGtags.gtags_project import GtagsProject
logging.basicConfig(format="[%(asctime)s] - [%(name)s] - [%(levelname)s] - \n%(message)s\n")
logger = logging.getLogger('Symbol')
logger.setLevel(logging.DEBUG)
BIN_PATH=os.path.dirname(os.path.abspath(__file__)) +"/bin/"+ platform.system()
SYMBOL_PATTERN = '(?P<symbol>[^\s]+)\s+(?P<line>[^\s]+)\s+(?P<path>[^\s]+)\s+(?P<signature>.*)'
SYMBOL_RE = re.compile(SYMBOL_PATTERN)
class GtagsSymbol(object):
kwargs=None
gtags_root=""
gtags_lib_path=""
pwd=None
file_changed_queue=queue.Queue(50)
auto_update_thread=None
build_tags_thread=None
def __init__(self, file_path=None):
GtagsSymbol.pwd = GtagsProject().get_pwd(file_path)
self.setup_kwargs()
if GtagsSymbol.auto_update_thread is None:
logger.info("creat auto update thread")
GtagsSymbol.auto_update_thread = threading.Thread(target=self.auto_update)
GtagsSymbol.auto_update_thread.start()
if GtagsSymbol.gtags_root is None or GtagsProject().is_changed():
# get the gtags root (GTAGSROOT) which contains the GTAGS files
config = GtagsProject().get_config()
logger.info("config: %s", config)
gtags_root = config.get('GTAGSROOT')
gtags_lib_path = config.get('GTAGSLIBPATH')
if gtags_lib_path is not None:
GtagsSymbol.gtags_lib_path = gtags_lib_path
if gtags_root is not None:
GtagsSymbol.gtags_root = os.path.normpath(gtags_root)
self.setup_kwargs()
logger.info("update gtags root: %s", GtagsSymbol.gtags_root)
else:
self.setup_kwargs()
logger.warning("the gtags_root is None!")
# get the additional library tag files if we have
# GtagsSymbol.gtags_lib_path = os.pathsep.join(gtags_lib_path)
logger.info("Symbol init, root: %s, lib: %s", GtagsSymbol.gtags_root, GtagsSymbol.gtags_lib_path)
def setup_kwargs(self):
# setup environment variable
exec_env = {}
exec_env['PATH'] = os.environ['PATH']
exec_env['PATH'] = os.path.normpath(BIN_PATH)
#exec_env['GTAGSROOT'] = GtagsSymbol.pwd
#if GtagsSymbol.gtags_lib_path is not None:
# exec_env['GTAGSLIBPATH'] = os.pathsep.join(GtagsSymbol.gtags_lib_path)
# setup the parameters for the subprocess interface
GtagsSymbol.kwargs={}
# shell=True will add "cmd /c" before "global -x", to avoid popup window
if platform.system() == "Windows":
GtagsSymbol.kwargs.update(shell=True)
# set the environment variable
GtagsSymbol.kwargs.update(env=exec_env)
# we will need the result of the global command
GtagsSymbol.kwargs.update(stdout=subprocess.PIPE)
GtagsSymbol.kwargs.update(stderr=subprocess.PIPE)
# set the current working directory to the gtags root dir
GtagsSymbol.kwargs.update(cwd=GtagsSymbol.pwd)
# the stdout will be sting lines instead of bytes when universal_newlines is True
GtagsSymbol.kwargs.update(universal_newlines=True)
logger.info("setup kwargs: %s", GtagsSymbol.kwargs)
# this function is used to exec the global command line
def exec_cmd(self, cmd):
if GtagsSymbol.gtags_root is None:
logger.warning("the gtags_root is None!")
#return None
logger.debug("time before run command: %s", time.time())
logger.info("run command: %s, kwargs: %s", cmd, GtagsSymbol.kwargs)
args = shlex.split(cmd)
return subprocess.Popen(args, **GtagsSymbol.kwargs)
def update(self, file_path):
# Update tag file incrementally
#h = self.exec_cmd("gtags --single-update " + file_path)
h = self.exec_cmd("global -u")
if h is None:
logger.warning("can not exec gtags!")
return -1
return 0
def version(self, file_path):
h = self.exec_cmd("global --version")
if h is None:
logger.warning("can not exec global!")
return -1
stdout, stderr = h.communicate()
if len(stderr) > 0:
logger.error(stderr)
logger.info(stdout)
return 0
def sfupdate(self, file_path):
GtagsProject().get_gtags_paths()
file_type = os.path.splitext(file_path)[1]
logger.info("sfupdate file type %s", file_type)
if not file_type in [".h", ".c", ".cpp", ".java"]:
logger.error("file type %s not supported", file_type)
return -1
h = self.exec_cmd("global -p")
if h is None:
logger.warning("can not exec gtags!")
return -1
# read the stdout and stderr
stdout, stderr = h.communicate()
if len(stderr) > 0:
logger.error(stderr)
logger.error("can not update when GTAGS is not builded")
return -3
logger.info(stdout)
# Update tag file incrementally
h = self.exec_cmd("gtags --single-update " + file_path)
#h = self.exec_cmd("global -u")
if h is None:
logger.warning("can not exec gtags!")
return -1
h.wait()
logger.info("sfupdate done!")
return 0
def auto_update(self):
logger.info("auto_update start!")
while True:
try:
file_path = GtagsSymbol.file_changed_queue.get()
self.sfupdate(file_path)
except queue.Empty:
time.sleep(2)
pass
def file_changed(self, file_path):
file_path = os.path.realpath(file_path)
ret = GtagsProject().is_file_in_project(file_path)
if ret is False:
return -2
logger.info("queue put: %s!", file_path)
GtagsSymbol.file_changed_queue.put(file_path)
return 0
def search_project(self, options, arg, file_path=None):
matches = []
gtags_paths = GtagsProject().get_gtags_paths()
if len(gtags_paths) is 0:
if file_path is None:
logger.info("not GTAGS in project or pwd")
return []
gtags_paths = [ os.path.dirname(file_path) ]
logger.info("not GTAGS in project, try pwd: %s", gtags_paths)
for gtags_path in gtags_paths:
GtagsSymbol.pwd = gtags_path
self.setup_kwargs()
matches = matches + self.search(options, arg)
return matches
def search(self, options, arg):
if options is None or arg is None:
logger.error("the options(%s) or arg(%s) error", options, arg)
return []
h = self.exec_cmd("global " + options + " " + arg)
#h = self.exec_cmd("global " + options) # test errors msg output
if h is None:
logger.warning("can not exec global!")
return []
# read the stdout and stderr
stdout, stderr = h.communicate()
if len(stderr) > 0:
logger.error(stderr)
lines = stdout.splitlines()
logger.debug("time after run command global: %s, lines: %s", time.time(), lines)
matches = []
if options == "-ax" or options == "-axr" or options == "-axf":
# format the definitions or references
for line in lines:
matches.append(SYMBOL_RE.search(line).groupdict())
elif options == "-axc":
# format the completions
matches = [ (line + "\tGtags", line) for line in lines ]
#matches = [ (line, line) for line in lines ]
else:
logger.error("unsported options %s!", options)
logger.debug("time after format the results: %s ", time.time())
logger.info("got %d results", len(matches))
return matches
# Description
# get the definitions of the symbol
# Peremeters
# name[in], the symbol which we want to search its definitions
# return
# it will return [] if we cann't find definitions in the gtags.
# and if we got the definitions, it will return the definitions
# formated as below:
# [
# {
# "symbol": "name",
# "line": "567",
# "path": "/tmp/test.c",
# "signature": "struct name {;"
# }
# { ... } // more definitions
# ]
def get_definitions(self, name, file_path):
logger.info("get the definitions of symbol")
"""
test = [ {"symbol": "name1", "line": "51", "path": "/tmp/test1.c", "signature": "struct name1 {;"},
{"symbol": "name2", "line": "52", "path": "/tmp/test2.c", "signature": "struct name2 {;"},
]
return test
"""
return self.search_project("-ax", name, file_path)
# Description
# get the references of the symbol
# Peremeters
# name[in], the symbol which we want to search its references
# return
# it will return [] if we cann't find references in the gtags.
# and if we got the references, it will return the references
# formated as below:
# [
# {
# "symbol": "name",
# "line": "567",
# "path": "/tmp/test.c",
# "signature": "struct name {;"
# }
# { ... }
# ... # more references
# ]
def get_references(self, name):
logger.info("get the references of symbol")
return self.search("-axr", name)
# Description
# get the completions of specific prefix
# Peremeters
# prefix[in], the prefix to search the completions
# return
# it will return [] if we cann't find references in the gtags.
# and if we got the references, it will return the references
# formated as below:
# [
# ("complt1\tGtags", "complt1"),
# ("complt2\tGtags", "complt2"),
# ... # more completions
# ]
def get_completions(self, prefix):
logger.info("get the completions")
return self.search_project("-axc", prefix)
# Description
# get the definitions in the specific file
# Peremeters
# file_path[in], the file to search the definitions
# return
# it will return [] if we cann't find definitions in the file.
# and if we got the definitions, it will return the definitions
# formated as below:
# [
# {
# "symbol": "name",
# "line": "567",
# "path": "/tmp/test.c",
# "signature": "struct name {;"
# }
# { ... }
# ... # more references
# ]
def get_all_symbol_definitions_of_file(self, file_path):
logger.info("get the symbol definitions in the file")
#return self.search("-axf", file_path)
#return self.search("-axf", "D:/SourceCode/U3/u3_sys/linux/drivers/usb/gadget/m66592-udc.c")
if platform.system() == "Windows":
file_path = file_path.replace("\\", "/")
return self.search("-axf", file_path)
def build_tags(self, gtags_path):
if GtagsSymbol.build_tags_thread is None:
if os.path.isfile(gtags_path):
GtagsSymbol.pwd = os.path.dirname(gtags_path)
elif os.path.isdir(gtags_path):
GtagsSymbol.pwd = gtags_path
else:
logger.info("build tags failed in %s", gtags_path)
return
logger.info("creat build_tags thread")
GtagsSymbol.build_tags_thread = threading.Thread(target=self.thread_build_tags)
GtagsSymbol.build_tags_thread.start()
building_tags = threading.Thread(target=self.building_tags)
building_tags.start()
def thread_build_tags(self):
logger.info("thread_build_tags begin")
self.setup_kwargs()
gtags_path = os.path.join(GtagsSymbol.pwd, "GTAGS")
if os.path.isfile(gtags_path):
logger.debug("update GTAGS: %s", gtags_path)
h = self.exec_cmd("global -u")
else:
logger.debug("build GTAGS: %s", gtags_path)
h = self.exec_cmd("gtags --sqlite3 --skip-unreadable")
#h = self.exec_cmd("global " + options) # test errors msg output
if h is None:
logger.warning("can not exec global!")
logger.debug("thread_build_tags failed!")
GtagsSymbol.build_tags_thread = None
return -1
# read the stdout and stderr
stdout, stderr = h.communicate()
if len(stderr) > 0:
logger.error(stderr)
logger.debug("thread_build_tags failed!")
GtagsSymbol.build_tags_thread = None
# return -1
#logger.debug("thread_build_tags successfully!")
self.version(gtags_path)
GtagsSymbol.build_tags_thread = None
sublime.message_dialog("build tags done!")
logger.debug(stdout)
return 0
def building_tags(self):
counter = 0;
while GtagsSymbol.build_tags_thread is not None:
counter = counter + 1
logger.info("building tags [%d]", counter)
time.sleep(1)
pass
return 0
class SymbolTest(unittest.TestCase):
def test_start_with(self):
print("test_start_with")
print("==============================================\n\n\n")
if __name__ == '__main__':
unittest.main()