forked from sublimehq/Vintage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vintage_motions.py
289 lines (234 loc) · 10.9 KB
/
vintage_motions.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
import sublime, sublime_plugin
from vintage import transform_selection
from vintage import transform_selection_regions
class ViMoveByCharactersInLine(sublime_plugin.TextCommand):
def run(self, edit, forward = True, extend = False, visual = False):
delta = 1 if forward else -1
transform_selection(self.view, lambda pt: pt + delta, extend=extend,
clip_to_line=(not visual))
class ViMoveByCharacters(sublime_plugin.TextCommand):
def advance(self, delta, visual, pt):
pt += delta
if not visual and self.view.substr(pt) == '\n':
pt += delta
return pt
def run(self, edit, forward = True, extend = False, visual = False):
delta = 1 if forward else -1
transform_selection(self.view, lambda pt: self.advance(delta, visual, pt),
extend=extend)
class ViMoveToHardEol(sublime_plugin.TextCommand):
def run(self, edit, repeat = 1, extend = False):
repeat = int(repeat)
if repeat > 1:
for i in xrange(repeat - 1):
self.view.run_command('move',
{'by': 'lines', 'extend': extend, 'forward': True})
transform_selection(self.view, lambda pt: self.view.line(pt).b,
extend=extend, clip_to_line=False)
class ViMoveToFirstNonWhiteSpaceCharacter(sublime_plugin.TextCommand):
def first_character(self, pt):
l = self.view.line(pt)
lstr = self.view.substr(l)
offset = 0
for c in lstr:
if c == ' ' or c == '\t':
offset += 1
else:
break
return l.a + offset
def run(self, edit, repeat = 1, extend = False):
# According to Vim's help, _ moves count - 1 lines downward.
for i in xrange(repeat - 1):
self.view.run_command('move', {'by': 'lines', 'forward': True, 'extend': extend})
transform_selection(self.view, lambda pt: self.first_character(pt),
extend=extend)
g_last_move_command = None
class ViMoveToCharacter(sublime_plugin.TextCommand):
def find_next(self, forward, char, before, pt):
lr = self.view.line(pt)
extra = 0 if before else 1
if forward:
line = self.view.substr(sublime.Region(pt, lr.b))
idx = line.find(char, 1)
if idx >= 0:
return pt + idx + 1 * extra
else:
line = self.view.substr(sublime.Region(lr.a, pt))[::-1]
idx = line.find(char, 0)
if idx >= 0:
return pt - idx - 1 * extra
return pt
def run(self, edit, character, extend = False, forward = True, before = False, record = True):
if record:
global g_last_move_command
g_last_move_command = {'character': character, 'extend': extend,
'forward':forward, 'before':before}
transform_selection(self.view,
lambda pt: self.find_next(forward, character, before, pt),
extend=extend)
# Helper class used to implement ';'' and ',', which repeat the last f, F, t
# or T command (reversed in the case of ',')
class SetRepeatMoveToCharacterMotion(sublime_plugin.TextCommand):
def run_(self, args):
if args:
return self.run(**args)
else:
return self.run()
def run(self, reverse = False):
if g_last_move_command:
cmd = g_last_move_command.copy()
cmd['record'] = False
if reverse:
cmd['forward'] = not cmd['forward']
self.view.run_command('set_motion', {
'motion': 'vi_move_to_character',
'motion_args': cmd,
'inclusive': True })
class ViMoveToBrackets(sublime_plugin.TextCommand):
def move_by_percent(self, percent):
destination = int(self.view.rowcol(self.view.size())[0] * (percent / 100.0))
destination = self.view.line(self.view.text_point(destination, 0)).a
destination = advance_while_white_space_character(self.view, destination)
transform_selection(self.view, lambda pt: destination)
def run(self, edit, repeat=1):
repeat = int(repeat)
if repeat == 1:
bracket_chars = ")]}"
def adj(pt):
if (self.view.substr(pt) in bracket_chars):
return pt + 1
else:
return pt
transform_selection(self.view, adj)
self.view.run_command("move_to", {"to": "brackets", "extend": True, "force_outer": True})
else:
self.move_by_percent(repeat)
class ViGotoLine(sublime_plugin.TextCommand):
def run(self, edit, repeat = 1, explicit_repeat = True, extend = False):
repeat = int(repeat)
if not explicit_repeat:
self.view.run_command('move_to', {'to': 'eof', 'extend':extend})
else:
target_pt = self.view.text_point(repeat - 1, 0)
transform_selection(self.view, lambda pt: target_pt,
extend=extend)
def advance_while_white_space_character(view, pt, white_space="\t "):
while view.substr(pt) in white_space:
pt += 1
return pt
class MoveCaretToScreenCenter(sublime_plugin.TextCommand):
def run(self, edit, extend = True):
screenful = self.view.visible_region()
row_a = self.view.rowcol(screenful.a)[0]
row_b = self.view.rowcol(screenful.b)[0]
middle_row = (row_a + row_b) / 2
middle_point = self.view.text_point(middle_row, 0)
middle_point = advance_while_white_space_character(self.view, middle_point)
transform_selection(self.view, lambda pt: middle_point, extend=extend)
class MoveCaretToScreenTop(sublime_plugin.TextCommand):
def run(self, edit, repeat, extend = True):
# Don't modify offset so not fully visible regions have a lower chance
# of scrolling the screen.
# lines_offset = int(repeat) - 1
lines_offset = int(repeat)
screenful = self.view.visible_region()
target = screenful.begin()
for x in xrange(lines_offset):
current_line = self.view.line(target)
target = current_line.b + 1
target = advance_while_white_space_character(self.view, target)
transform_selection(self.view, lambda pt: target, extend=extend)
class MoveCaretToScreenBottom(sublime_plugin.TextCommand):
def run(self, edit, repeat, extend = True):
# Don't modify offset so not fully visible regions have a lower chance
# of scrolling the screen.
# lines_offset = int(repeat) - 1
lines_offset = int(repeat)
screenful = self.view.visible_region()
target = screenful.end()
for x in xrange(lines_offset):
current_line = self.view.line(target)
target = current_line.a - 1
target = self.view.line(target).a
target = advance_while_white_space_character(self.view, target)
transform_selection(self.view, lambda pt: target, extend=extend)
def expand_to_whitespace(view, r):
a = r.a
b = r.b
while view.substr(b) in " \t":
b += 1
if b == r.b:
while view.substr(a - 1) in " \t":
a -= 1
return sublime.Region(a, b)
class ViExpandToWords(sublime_plugin.TextCommand):
def run(self, edit, outer = False, repeat = 1):
repeat = int(repeat)
transform_selection_regions(self.view, lambda r: sublime.Region(r.b + 1, r.b + 1))
self.view.run_command("move", {"by": "stops", "extend":False, "forward":False, "word_begin":True, "punct_begin":True})
for i in xrange(repeat):
self.view.run_command("move", {"by": "stops", "extend":True, "forward":True, "word_end":True, "punct_end":True})
if outer:
transform_selection_regions(self.view, lambda r: expand_to_whitespace(self.view, r))
class ViExpandToBigWords(sublime_plugin.TextCommand):
def run(self, edit, outer = False, repeat = 1):
repeat = int(repeat)
transform_selection_regions(self.view, lambda r: sublime.Region(r.b + 1, r.b + 1))
self.view.run_command("move", {"by": "stops", "extend":False, "forward":False, "word_begin":True, "punct_begin":True, "separators": ""})
for i in xrange(repeat):
self.view.run_command("move", {"by": "stops", "extend":True, "forward":True, "word_end":True, "punct_end":True, "separators": ""})
if outer:
transform_selection_regions(self.view, lambda r: expand_to_whitespace(self.view, r))
class ViExpandToQuotes(sublime_plugin.TextCommand):
def compare_quote(self, character, p):
if self.view.substr(p) == character:
return self.view.score_selector(p, "constant.character.escape") == 0
else:
return False
def expand_to_quote(self, character, r):
p = r.b
a = p
b = p
while a >= 0 and not self.compare_quote(character, a):
a -= 1
sz = self.view.size()
while p < sz and not self.compare_quote(character, b):
b += 1
return sublime.Region(a + 1, b)
def expand_to_outer(self, r):
a, b = r.a, r.b
if a > 0:
a -= 1
if b < self.view.size():
b += 1
return expand_to_whitespace(self.view, sublime.Region(a, b))
def run(self, edit, character, outer = False):
transform_selection_regions(self.view, lambda r: self.expand_to_quote(character, r))
if outer:
transform_selection_regions(self.view, lambda r: self.expand_to_outer(r))
class ViExpandToTag(sublime_plugin.TextCommand):
def run(self, edit, outer = False):
self.view.run_command('expand_selection', {'to': 'tag'})
if outer:
self.view.run_command('expand_selection', {'to': 'tag'})
class ViExpandToBrackets(sublime_plugin.TextCommand):
def run(self, edit, character, outer = False):
self.view.run_command('expand_selection', {'to': 'brackets', 'brackets': character})
if outer:
self.view.run_command('expand_selection', {'to': 'brackets', 'brackets': character})
class ScrollCurrentLineToScreenTop(sublime_plugin.TextCommand):
def run(self, edit, repeat, extend=False):
bos = self.view.visible_region().a
caret = self.view.line(self.view.sel()[0].begin()).a
offset = self.view.rowcol(caret)[0] - self.view.rowcol(bos)[0]
caret = advance_while_white_space_character(self.view, caret)
transform_selection(self.view, lambda pt: caret, extend)
self.view.run_command('scroll_lines', {'amount': -offset})
class ScrollCurrentLineToScreenCenter(sublime_plugin.TextCommand):
def run(self, edit, repeat, extend=True):
line_nr = self.view.rowcol(self.view.sel()[0].a)[0] if \
int(repeat) == 1 else int(repeat) - 1
point = self.view.line(self.view.text_point(line_nr, 0)).a
point = advance_while_white_space_character(self.view, point)
transform_selection(self.view, lambda pt: point, extend)
self.view.run_command('show_at_center')