-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtags_navigation.py
200 lines (160 loc) · 6.57 KB
/
gtags_navigation.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
import sublime
import sublime_plugin
import logging
import os
import math
logging.basicConfig(format="[%(asctime)s] - [%(name)s] - [%(levelname)s] - \n%(message)s\n")
logger = logging.getLogger('Navigation')
logger.setLevel(logging.DEBUG)
class GtagsNavigation(object):
trace = [];
current_index = 0;
trace0 = [];
current_index0 = 0;
trace1 = [];
current_index1 = 0;
cursor_lock = 0;
def lock(self):
GtagsNavigation.cursor_lock = 1
def unlock(self):
GtagsNavigation.cursor_lock = 0
def add(self, path, line, symbol=None):
logger.info("GtagsNavigation add")
GtagsNavigation.trace = GtagsNavigation.trace0
GtagsNavigation.current_index = GtagsNavigation.current_index0
self.__add(path, line, symbol)
GtagsNavigation.trace0 = GtagsNavigation.trace
GtagsNavigation.current_index0 = GtagsNavigation.current_index
def show_trace(self):
GtagsNavigation.trace = GtagsNavigation.trace0
GtagsNavigation.current_index = GtagsNavigation.current_index0
self.__show_trace()
GtagsNavigation.trace0 = GtagsNavigation.trace
GtagsNavigation.current_index0 = GtagsNavigation.current_index
def forward(self):
GtagsNavigation.cursor_lock = 1
GtagsNavigation.trace = GtagsNavigation.trace0
GtagsNavigation.current_index = GtagsNavigation.current_index0
self.__forward()
GtagsNavigation.trace0 = GtagsNavigation.trace
GtagsNavigation.current_index0 = GtagsNavigation.current_index
def backward(self):
GtagsNavigation.cursor_lock = 1
GtagsNavigation.trace = GtagsNavigation.trace0
GtagsNavigation.current_index = GtagsNavigation.current_index0
self.__backward()
GtagsNavigation.trace0 = GtagsNavigation.trace
GtagsNavigation.current_index0 = GtagsNavigation.current_index
def set_cur_index(self, index):
GtagsNavigation.trace = GtagsNavigation.trace0
GtagsNavigation.current_index = GtagsNavigation.current_index0
self.__set_cur_index(index)
GtagsNavigation.trace0 = GtagsNavigation.trace
GtagsNavigation.current_index0 = GtagsNavigation.current_index
def add_cursor_trace(self, path, line, symbol=None):
if GtagsNavigation.cursor_lock == 1:
GtagsNavigation.cursor_lock = 0
return
if path is None:
logger.info("GtagsNavigation add_cursor_trace, path is None")
return
logger.info("GtagsNavigation add_cursor_trace")
GtagsNavigation.trace = GtagsNavigation.trace1
GtagsNavigation.current_index = GtagsNavigation.current_index1
self.__add(path, line, symbol)
GtagsNavigation.trace1 = GtagsNavigation.trace
GtagsNavigation.current_index1 = GtagsNavigation.current_index
def show_cursor_trace(self):
GtagsNavigation.trace = GtagsNavigation.trace1
GtagsNavigation.current_index = GtagsNavigation.current_index1
self.__show_trace()
GtagsNavigation.trace1 = GtagsNavigation.trace
GtagsNavigation.current_index1 = GtagsNavigation.current_index
def forward_cursor_trace(self):
GtagsNavigation.trace = GtagsNavigation.trace1
GtagsNavigation.current_index = GtagsNavigation.current_index1
self.__forward()
GtagsNavigation.trace1 = GtagsNavigation.trace
GtagsNavigation.current_index1 = GtagsNavigation.current_index
def backward_cursor_trace(self):
GtagsNavigation.trace = GtagsNavigation.trace1
GtagsNavigation.current_index = GtagsNavigation.current_index1
self.__backward()
GtagsNavigation.trace1 = GtagsNavigation.trace
GtagsNavigation.current_index1 = GtagsNavigation.current_index
def set_cursor_trace_cur_index(self, index):
GtagsNavigation.trace = GtagsNavigation.trace1
GtagsNavigation.current_index = GtagsNavigation.current_index1
self.__set_cur_index(index)
GtagsNavigation.trace1 = GtagsNavigation.trace
GtagsNavigation.current_index1 = GtagsNavigation.current_index
def __add(self, path, line, symbol=None):
# view = sublime.active_window().active_view()
# symbol = view.substr(view.word(view.sel()[0].a))
if len(GtagsNavigation.trace) == 0:
index = 0;
GtagsNavigation.current_index = 0
else:
index = GtagsNavigation.current_index + 1
# we don't add if the new trace is close to the prev
line_abs = math.fabs(int(line) - int(GtagsNavigation.trace[index-1].get('line')))
#logger.info("line abs: %s", line_abs)
if path == GtagsNavigation.trace[index-1].get('path') and \
line_abs < 3.0:
logger.info("new trace is close to the prev, line abs: %s", line_abs)
return
# we don't add if the new trace is close to the next
if index < len(GtagsNavigation.trace):
line_abs = math.fabs(int(line) - int(GtagsNavigation.trace[index].get('line')))
if path == GtagsNavigation.trace[index].get('path') and \
line_abs < 3.0:
logger.info("new trace is close to the next, line abs: %s", line_abs)
if index < len(GtagsNavigation.trace):
GtagsNavigation.current_index = index
return
GtagsNavigation.current_index = index
logger.info("add trace: %s:%s:%s", symbol, line, path)
GtagsNavigation.trace.insert(index, {'path':path, 'line':line, 'symbol':symbol})
if len(GtagsNavigation.trace) > 1000:
GtagsNavigation.trace.pop(0)
GtagsNavigation.current_index = GtagsNavigation.current_index - 1
logger.info("trace len: %s, index: %s", len(GtagsNavigation.trace), GtagsNavigation.current_index)
def __show_trace(self):
return GtagsNavigation.trace
def __forward(self):
if GtagsNavigation.current_index >= len(GtagsNavigation.trace)-1:
return
if len(GtagsNavigation.trace) == 0:
return;
elif len(GtagsNavigation.trace) == 1:
index = 0;
else:
index = GtagsNavigation.current_index+1;
GtagsNavigation.current_index = index;
path = GtagsNavigation.trace[index].get('path')
line = GtagsNavigation.trace[index].get('line')
logger.info("forward to file: %s:%s", line, path)
position = '%s:%d:0' % (os.path.normpath(path), int(line))
sublime.active_window().open_file(position, sublime.ENCODED_POSITION)
def __backward(self):
if GtagsNavigation.current_index == 0 and len(GtagsNavigation.trace) != 1:
return
if len(GtagsNavigation.trace) == 0:
return;
elif len(GtagsNavigation.trace) == 1:
index = 0;
else:
index = GtagsNavigation.current_index-1;
GtagsNavigation.current_index = index;
path = GtagsNavigation.trace[index].get('path')
line = GtagsNavigation.trace[index].get('line')
logger.info("backward to file: %s:%s", line, path)
position = '%s:%d:0' % (os.path.normpath(path), int(line))
sublime.active_window().open_file(position, sublime.ENCODED_POSITION)
def __set_cur_index(self, index):
if index > 0 and index < len(GtagsNavigation.trace):
GtagsNavigation.current_index=index
logger.info("set index: %s", index)
def __cleanup(self):
GtagsNavigation.trace = [];
GtagsNavigation.current_index = 0;