-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjump_along_indent.py
89 lines (66 loc) · 2.82 KB
/
jump_along_indent.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
import sublime
import sublime_plugin
from .file_scanner import FileScanner
from .view_helper import ViewHelpers
class JumpIndentCommand(object):
def run(self, edit, extend_selection=False, indent_offset=0):
self.indent_offset = indent_offset
self.clear_selection()
for self.view_helper in ViewHelpers(self.view):
self.scanner = FileScanner(self.view, self.view_helper)
if not extend_selection:
self.jump()
elif self.check_selection_pos():
self.deselect()
else:
self.select()
self.update_selection()
def clear_selection(self):
self.listsel = []
def build_selection(self, new_region):
self.listsel.append(new_region)
def update_selection(self):
self.view.sel().clear()
for s in self.listsel:
self.view.sel().add(s)
self.view.show(s)
def jump(self):
target = self.target_point()
new_region = sublime.Region(target, target, self.view_helper.initial_xpos())
self.build_selection(new_region)
def select(self):
target = self.target_point()
new_region = sublime.Region(self.get_select_begin_pos(), target, self.view_helper.initial_xpos())
self.build_selection(new_region)
def deselect(self):
matched_row = self.scanner.scan(self.direction, self.indent_offset)
target = self.target_point(matched_row)
new_region = sublime.Region(self.get_deselect_begin_pos(), target, self.view_helper.initial_xpos())
self.build_selection(new_region)
def target_point(self, matched_row=None):
matched_row = matched_row or self.scanner.scan(self.direction, self.indent_offset)
selection_offset = self.indent_offset
if matched_row == self.view_helper.initial_row():
selection_offset = 0
matched_point_bol = self.view.text_point(matched_row, 0)
return self.view.text_point(matched_row, self.view_helper.target_column(matched_point_bol, selection_offset))
class JumpNextIndentCommand(JumpIndentCommand, sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
super(JumpNextIndentCommand, self).__init__(*args, **kwargs)
self.direction = 'forward'
def check_selection_pos(self):
return self.view_helper.cursor_at_top_of_selection()
def get_select_begin_pos(self):
return self.view_helper.initial_selection().begin()
def get_deselect_begin_pos(self):
return self.view_helper.initial_selection().end()
class JumpPrevIndentCommand(JumpIndentCommand, sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
super(JumpPrevIndentCommand, self).__init__(*args, **kwargs)
self.direction = 'backward'
def check_selection_pos(self):
return self.view_helper.cursor_at_bottom_of_selection()
def get_select_begin_pos(self):
return self.view_helper.initial_selection().end()
def get_deselect_begin_pos(self):
return self.view_helper.initial_selection().begin()