Repository navigation
Expand file tree
/
Copy pathmotion.py
More file actions
287 lines (220 loc) · 8.67 KB
/
Copy pathmotion.py
File metadata and controls
287 lines (220 loc) · 8.67 KB
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
from itertools import product
from itertools import permutations
from itertools import chain
from string import digits
from string import ascii_letters
import sublime
import sublime_plugin
class LabelObject:
def __init__(self):
self.label_region = {}
self.empty = True
self.focused_label = []
def add_label_region(self, label, region, displacement):
if self.empty:
self.empty = False
self.label_region[label] = (region, displacement)
def is_empty(self):
return self.empty
def get_region_by_label(self, label):
if label in self.label_region:
return self.label_region[label][0]
return None
def focused_query(self, label):
if label in self.focused_label:
return True
return False
def get_region_by_range(self, beg_label, end_label=None):
if not beg_label:
return None
region_list = []
for label in self.label_region:
if (end_label and label >= beg_label and label <= end_label) or \
(not end_label and label >= beg_label):
region_list.append(self.get_displaced_by_label(label))
return region_list
def get_all_regions(self):
return [region for region in self.label_region.values()]
def get_all_labels(self):
return [label for label in self.label_region]
def get_displaced_by_label(self, label):
if label in self.label_region:
region, displacement = self.label_region[label]
region = sublime.Region(region.begin() + displacement,
region.end() + displacement)
return region
return None
def split_partial_label(self, partial_label):
focus_list = []
unfocus_list = []
matched_region = None
for (label, region) in self.label_region.items():
region, displacement = region
region = sublime.Region(region.begin() + displacement,
region.end() + displacement)
if label == partial_label:
matched_region = region
if label.startswith(partial_label):
focus_list.append(region)
else:
unfocus_list.append(region)
# maybe we only need the focus list
# return (focus_list, unfocus_list, matched_region)
return (focus_list, unfocus_list)
def get_all_displaced_regions(self):
res = []
for region, displacement in self.label_region.values():
region = sublime.Region(region.begin() + displacement,
region.end() + displacement)
res.append(region)
return res
def clear(self):
self.label_region = {}
self.label_list = []
self.empty = True
def BufferUndoCommand(view, edit, keys, is_empty):
"""
Command for removing LABELS from views
only calling run_command('undo') iff:
Labels is not empty
"""
for key in keys:
view.erase_regions(key)
view.end_edit(edit)
if not is_empty:
view.run_command("undo")
return True
def AddLabelsCommand(view, edit, regex, labels, label_gen, beg=None,
end=None, literal=False):
if beg is None:
beg = view.visible_region().begin()
if end is None:
end = view.visible_region().end()
work_region = sublime.Region(beg, end)
displacement = 0
while(beg is not None and beg < end):
if literal is True:
region = view.find(regex, beg, sublime.LITERAL)
else:
region = view.find(regex, beg)
if region is not None and not region.empty() and work_region.contains(region):
label = ''.join(next(label_gen))
word_region = view.word(region)
target_size = word_region.end() - word_region.begin()
while (target_size + displacement) < len(label):
view.insert(edit, region.end() + displacement, ' ')
displacement += 1
replace_region = sublime.Region(
region.begin(), region.begin() + len(label))
view.replace(edit, replace_region, label)
actual_region = sublime.Region(
region.begin() - displacement, region.begin() + len(label) - displacement)
labels.add_label_region(label, actual_region, displacement)
beg = region.end() + displacement
else:
return
def JumpToLabelCommand(view, edit, keys, target_point,
multiple_selection=False,
select_till=False):
'''
1. select_till == True, highlight everything from current cursor till label
2. multiple.multiple_selection == True, the curcor won't be clear, just more
cursors are placed at the labels
'''
if select_till is True:
# select_till will only work with a single region
region = sublime.Region(target_point[0])
region_list = [view.sel()[0].begin(),
view.sel()[0].end(),
region.begin(),
region.end()]
beg = min(region_list)
end = max(region_list)
view.sel().add(sublime.Region(beg, end))
else:
region = sublime.Region(target_point)
cursor = view.sel()[0]
if not multiple_selection:
view.sel().clear()
else:
view.sel().subtract(cursor)
view.sel().add_all(target_point)
# view.show(target_point)
def draw_labels_in_range(view, keys, scopes, labels,
unfocus_flag, labels_range):
'''
Take a list of individual labels or a range of labes.
The labels in this list will be focused.
Other labels go into the unfocused list.
'''
focus_key, unfocus_key, viewing_key = keys
focus_scope, unfocus_scope = scopes
unfocus_region = view.get_regions(focus_key)
if unfocus_region:
view.erase_regions(focus_key)
view.add_regions(unfocus_key, unfocus_region,
unfocus_scope, flags=unfocus_flag)
#Consider all labels to be unfocused here.
unfocus_set = set([label for label in labels.get_all_labels()])
focus_region = []
unfocus_region=[]
# Adding the labels into the focused list
for label in labels_range:
tmp = labels.get_displaced_by_label(label)
if tmp:
focus_region.append(tmp)
# Remove the focused labels from the unfocus list
unfocus_set.discard(label)
for label in unfocus_set:
tmp = labels.get_displaced_by_label(label)
if tmp:
unfocus_region.append(tmp)
if focus_region:
''' draw the focus list '''
view.add_regions(focus_key, focus_region, focus_scope)
if unfocus_region:
''' draw the unfocus list '''
view.add_regions(unfocus_key, unfocus_region,
unfocus_scope, flags=unfocus_flag)
return focus_region
def draw_labels(view, keys, scopes, labels, unfocus_flag, partial_label=None):
# multiple_focuses = True
focus_key, unfocus_key, viewing_key = keys
focus_scope, unfocus_scope = scopes
view.erase_regions(focus_key)
view.erase_regions(unfocus_key)
focus_list = []
unfocus_list = []
matched_region = None
if not partial_label:
''' partial label is blank, draw all the labels'''
displaced_regions = labels.get_all_displaced_regions()
# if displaced_regions:
# view.show_at_center(displaced_regions[0])
view.add_regions(focus_key,
labels.get_all_displaced_regions(),
focus_scope)
else:
'''
partial label is not blank,
match all region starts with the partial lable
'''
# focus_list, unfocus_list, matched_region = \
focus_list, unfocus_list = \
labels.split_partial_label(partial_label)
if focus_list:
''' draw the focus list '''
# view.show_at_center(focus_list[0])
view.add_regions(focus_key, focus_list, focus_scope)
if unfocus_list:
''' draw the unfocus list '''
view.add_regions(unfocus_key, unfocus_list, unfocus_scope,
flags=unfocus_flag)
return focus_list
def label_generator_singledouble():
# product = (tup[0] + tup[1] for tup in permutations(digits+ascii_letters, 2))
# return chain(ascii_letters,digits,product)
return chain(ascii_letters, digits[::-1],\
product(digits + ascii_letters, repeat=2))
def label_generator_double():
return (tup[0] + tup[1] for tup in permutations(ascii_letters + digits, 2))