-
Notifications
You must be signed in to change notification settings - Fork 7
/
MultiFill.py
286 lines (229 loc) · 8.05 KB
/
MultiFill.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
import sublime
import sublime_plugin
import random
import string
import time
import re
#
# inner function
#
def nums(i):
return str(i)
def nums2(i):
return str(i + 1)
def upAlphabet(i):
return chr(i + 65)
def lwAlphabet(i):
return chr(i + 97)
def lowerFirstCharacter(m):
return '_' + m.group(0).lower()
def camelCase(s):
# 将正则表达式编译成Pattern对象
pattern = re.compile(r'[^a-zA-Z0-9]')
s = re.sub(pattern, ' ', s);
#使用空格隔开后,每个单词首字母大写
s = s.title()
#删除空格
s = s.replace(' ','');
#小写字符串的首字母
s = s[0:1].lower() + s[1:len(s)]
return s;
def under_score(s):
s = s.replace('ID','Id');
# 将正则表达式编译成Pattern对象
pattern = re.compile(r'([A-Z])')
s = re.sub(pattern, lowerFirstCharacter, s);
#去掉两端下划线
s = s.strip('_')
return s;
func = [nums, nums2, upAlphabet, lwAlphabet]
def userCustom(order, data, length, pos , str_sel):
if (order == 'ordered'):
if pos >= length:
pos %= length
return data[pos]
elif (order == 'random'):
return data[random.randint(0, length - 1)]
elif (order == 'time'):
return time.strftime(data[random.randint(0, length - 1)])
elif (order == 'replace'):
s = ''
for i in range(0,len(str_sel)):
s = s + data[random.randint(0, length - 1)]
return s[0:len(str_sel)]
elif (order == 'camelCase'):
return camelCase(str_sel)
elif (order == 'under_score'):
return under_score(str_sel)
elif (order == 'camelSwitch'):
if str_sel.find('_')>=0 :
return camelCase(str_sel)
else:
return under_score(str_sel)
#
# text insert part
#
def getList():
return ['Numbers since 0 (Ordered)', 'Numbers since 1 (Ordered)', 'Uppercase letters (Ordered)', 'Lower case letters (Ordered)']
class MultiFillSetTextCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
chosen = args.get('chosen')
if (chosen > len(func) - 1):
# custom fill
custom = args.get("custom")
item = custom[chosen - len(func)]
points = self.view.sel()
count = len(points)
for i in range(0, count):
self.view.replace(edit, points[i], userCustom(
item['way'], item["values"], len(item["values"]), i , self.view.substr(points[i]) ))
else:
points = self.view.sel()
count = len(points)
for i in range(0, count):
self.view.replace(edit, points[i], func[chosen](i))
#
# select panel
#
class MultiFillCommand(sublime_plugin.WindowCommand):
def run(self):
# get the fill list
self.command_list = getList() + self.get_custom()
if not self.command_list:
echo('There may be some error with the config file.')
return
# call the panel
self.window.show_quick_panel(self.command_list, self.on_done)
def get_custom(self):
# get the custom list from the config file
self.settings = sublime.load_settings('MultiFill.sublime-settings')
custom = self.settings.get("custom")
result = []
for i in range(0, len(custom)):
result += [custom[i]["name"]]
return result
def on_done(self, index):
if (index == -1):
return
# call the text fill
view = sublime.active_window().active_view()
if view:
view.run_command('multi_fill_set_text', {'chosen': index, 'custom': self.settings.get("custom")})
#
# formula insert part
#
class MultiIntegerSetTextCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
text = args.get('formula')
points = self.view.sel()
count = len(points)
for x in range(0, count):
try:
self.view.replace(edit, points[x], str(eval(text)))
except:
echo("Sorry! Parse error, MultiFill dose not support pure math-like formula yet.")
return
#
# formula input
#
def isDigit(c):
if c not in string.digits:
return False
else:
return True
class MultiIntegerCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Multi Integer formula: ", "y = ", self.on_done, None, None)
def on_done(self, text):
text = text.replace(" ", "").replace("y=", "")
flag = 1
deal = []
for i in range(0, len(text)):
if text[i] == 'x':
flag = 0
if i > 0 and isDigit(text[i - 1]):
deal.append('*')
deal.append('x')
elif text[i] == '(':
if i > 0 and isDigit(text[i - 1]):
deal.append('*')
deal.append('(')
else:
deal.append(text[i])
if flag:
echo("You should type the independent variable 'x' in the formula.")
return
deal = str(''.join(deal))
# echo(deal)
# call the formula text fill
view = sublime.active_window().active_view()
if view:
view.run_command('multi_integer_set_text', {'formula': deal})
class MultiSelectWindowsCommand(sublime_plugin.WindowCommand):
def run(self, **args):
dir = args.get('direction')
group_num = self.window.num_groups()
group_now = self.window.active_group()
group_to = 0
if (dir == 'left'):
group_to = (group_now - 1) % group_num
elif (dir == 'right'):
group_to = (group_now + 1) % group_num
self.window.focus_group(group_to)
class MultiMoveWindowCommand(sublime_plugin.WindowCommand):
def run(self, **args):
dir = args.get('direction')
view = self.window.active_view()
group_num = self.window.num_groups()
group_now, view_now = self.window.get_view_index(view)
if (dir == 'left'):
group_to = (group_now - 1) % group_num
elif (dir == 'right'):
group_to = (group_now + 1) % group_num
self.window.set_view_index(view, group_to, 0)
class MultiWindowCompareCommand(sublime_plugin.WindowCommand):
def run(self, **args):
dir = args.get('direction')
sync = args.get('sync')
view = self.window.active_view()
group_now, view_now = self.window.get_view_index(view)
group_num = self.window.num_groups()
group_to = 0;
if (group_num == 1):
return
elif (group_num == 2):
group_to = 1 - group_now
elif (group_num == 3):
group_to = 1 - group_now
if (group_now == 2):
group_to = 1
elif (group_num == 4):
group_to = group_now + 1
if (group_now == 3):
group_to = 2
compare_view = self.window.active_view_in_group(group_to)
points = view.sel();
(row,col) = view.rowcol(points[0].begin())
(region_start_row, region_start_col) = view.rowcol(view.visible_region().begin())
(region_end_row, region_end_col) = view.rowcol(view.visible_region().end())
screen_start = region_start_row + 1
if (dir == 'up'):
amount = 1.0
elif (dir == 'down'):
amount = -1.0
view.run_command("scroll_lines", {"amount": amount });
if (sync == 'true'):
point = view.text_point((region_start_row + (region_end_row - region_start_row)/2), 0)
compare_view.show_at_center(point)
# compare_view.run_command("goto_line", {"line": row+1} )
compare_view.run_command("scroll_lines", {"amount": amount });
class CloseOtherTabsCommand(sublime_plugin.TextCommand):
def run(self, edit):
window = self.view.window()
group_index, view_index = window.get_view_index(self.view)
window.run_command("close_others_by_index", { "group": group_index, "index": view_index})
#
# debug
#
def echo(message):
sublime.message_dialog(message)