forked from koko1000ban/SublimeGtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtagsplugin.py
218 lines (170 loc) · 6.38 KB
/
gtagsplugin.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
# -*- coding: utf-8 -*-
import os
import threading
from os.path import join, normpath, dirname
import sublime
import sublime_plugin
from sublime import status_message
# Gtags
if sublime.version().startswith('2'):
import gtags
from gtags import (TagFile, PP, find_tags_root)
else:
import SublimeGtags.gtags
from SublimeGtags.gtags import TagFile, PP, find_tags_root
settings = sublime.load_settings('GTags.sublime-settings')
def run_on_cwd(dir=None):
window = sublime.active_window()
def wrapper(func):
view = window.active_view()
filename = view.file_name()
if filename is None:
sublime.error_message('Cannot use GNU GLOBAL for non-file')
return
if dir is None:
tags_root = find_tags_root(dirname(filename))
if tags_root is None:
sublime.error_message("GTAGS not found. build tags by 'gtags'")
return
else:
tags_root = dir[0]
tags = TagFile(tags_root, settings.get('extra_tag_paths'))
func(view, tags, tags_root)
return wrapper
class ThreadProgress(object):
def __init__(self, thread, message, success_message, error_message):
self.thread = thread
self.message = message
self.success_message = success_message
self.error_message = error_message
self.addend = 1
self.size = 8
sublime.set_timeout(lambda: self.run(0), 100)
def run(self, i):
if not self.thread.is_alive():
if hasattr(self.thread, 'success') and not self.thread.success:
sublime.status_message(self.error_message)
else:
sublime.status_message(self.success_message)
return
before = i % self.size
after = (self.size - 1) - before
sublime.status_message('%s [%s=%s]' % \
(self.message, ' ' * before, ' ' * after))
if not before:
self.addend = 1
elif not after:
self.addend = -1
i += self.addend
sublime.set_timeout(lambda: self.run(i), 100)
class JumpHistory(object):
instance = None
def __init__(self):
self._storage = []
def append(self, view):
filename = view.file_name()
row, col = view.rowcol(view.sel()[0].begin())
self._storage.append('%s:%d:%d' % (filename, row + 1, col + 1))
def jump_back(self):
if self.empty():
sublime.status_message('Jump history is empty')
else:
filename = self._storage.pop()
sublime.active_window().open_file(filename, sublime.ENCODED_POSITION)
def jump_forward(self):
sublime.status_message('Not implemented')
def empty(self):
return len(self._storage) == 0
def jump_history():
if JumpHistory.instance is None:
JumpHistory.instance = JumpHistory()
return JumpHistory.instance
class GtagsJumpBack(sublime_plugin.WindowCommand):
def run(self):
jump_history().jump_back()
def gtags_jump_keyword(view, keywords, root, showpanel=False):
def jump(keyword):
jump_history().append(view)
position = '%s:%d:0' % (
os.path.normpath(keyword['path']), int(keyword['linenum']))
view.window().open_file(position, sublime.ENCODED_POSITION)
def on_select(index):
if index != -1:
jump(keywords[index])
if showpanel or len(keywords) > 1:
if settings.get('show_relative_paths'):
convert_path = lambda path: os.path.relpath(path, root)
else:
convert_path = os.path.normpath
data = [
[kw['signature'], '%s:%d' % (convert_path(kw['path']), int(kw['linenum']))]
for kw in keywords
]
view.window().show_quick_panel(data, on_select)
else:
jump(keywords[0])
class ShowSymbolsThread(threading.Thread):
def __init__(self, view, tags, root):
threading.Thread.__init__(self)
self.view = view
self.tags = tags
self.root = root
def run(self):
symbols = self.tags.start_with('')
self.success = len(symbols) > 0
if not self.success:
return
def on_select(index):
if index != -1:
definitions = self.tags.match(symbols[index])
gtags_jump_keyword(self.view, definitions, self.root)
sublime.set_timeout(
lambda: self.view.window().show_quick_panel(symbols, on_select), 0)
class GtagsShowSymbols(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
def and_then(view, tags, root):
thread = ShowSymbolsThread(view, tags, root)
thread.start()
ThreadProgress(thread,
'Getting symbols on %s' % root,
'Symbols have successfully obtained',
'No symbols found')
class GtagsNavigateToDefinition(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
def and_then(view, tags, root):
symbol = view.substr(view.word(view.sel()[0]))
matches = tags.match(symbol)
if not matches:
status_message("'%s' is not found on tag." % symbol)
return
gtags_jump_keyword(view, matches, root)
class GtagsFindReferences(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
def and_then(view, tags, root):
symbol = view.substr(view.word(view.sel()[0]))
matches = tags.match(symbol, reference=True)
if not matches:
status_message("'%s' is not found on rtag." % symbol)
return
gtags_jump_keyword(view, matches, root)
class TagsRebuildThread(threading.Thread):
def __init__(self, tags):
threading.Thread.__init__(self)
self.tags = tags
def run(self):
self.success = self.tags.rebuild()
class GtagsRebuildTags(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
# set root folder if run from sidebar context menu
root = kwargs.get('dirs')
@run_on_cwd(dir=root)
def and_then(view, tags, root):
thread = TagsRebuildThread(tags)
thread.start()
ThreadProgress(thread,
'Rebuilding tags on %s' % root,
'Tags rebuilt successfully on %s' % root,
'Error while tags rebuilding, see console for details')