-
Notifications
You must be signed in to change notification settings - Fork 0
/
classJumper.py
97 lines (84 loc) · 3.03 KB
/
classJumper.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
import sublime
import sublime_plugin
class ClassJumperCommand(sublime_plugin.TextCommand):
@staticmethod
def is_same_pointer_position(pointer_position, closest_area_to_pointer):
return pointer_position == closest_area_to_pointer
@staticmethod
def keep_current_area_index(
direction,
pointer_position,
current_position,
):
if direction == 'up':
return pointer_position < current_position
elif direction == 'down':
return pointer_position > current_position
@staticmethod
def get_new_pointer(
direction,
available_positions,
new_position_index,
keep_current_area_index,
is_same_pointer_position,
):
if direction == 'up':
new_pointer = (
available_positions[new_position_index - 1] if
(is_same_pointer_position or keep_current_area_index) else
available_positions[new_position_index]
)
elif direction == 'down':
try:
new_pointer = (
available_positions[new_position_index + 1] if
(is_same_pointer_position or keep_current_area_index) else
available_positions[new_position_index]
)
except IndexError:
new_pointer = available_positions[0]
return new_pointer
def move_pointer(self, direction, pointer_position, jump_area):
available_positions = [region.begin() for region in jump_area]
closest_area_to_pointer = min(
available_positions,
key=lambda x: abs(x - pointer_position)
)
is_same_pointer_position = self.is_same_pointer_position(
pointer_position,
closest_area_to_pointer,
)
new_position_index = (
available_positions.index(pointer_position) if
is_same_pointer_position else
available_positions.index(closest_area_to_pointer)
)
keep_current_area_index = self.keep_current_area_index(
available_positions[new_position_index],
direction,
pointer_position,
)
new_pointer = self.get_new_pointer(
direction,
available_positions,
new_position_index,
keep_current_area_index,
is_same_pointer_position,
)
return new_pointer
def jump_to_select(self, jump_to):
if jump_to == 'class':
return self.view.find_all(r'(class?)(\s{1}\w+\()')
elif jump_to == 'method':
return self.view.find_all(r'(def?)(\s{1}\w+\()')
def run(self, edit, direction, jump_to):
selected = self.view.sel()
pointer_position = selected[0].begin()
new_pointer = self.move_pointer(
direction,
pointer_position,
self.jump_to_select(jump_to),
)
selected.clear()
selected.add(sublime.Region(new_pointer))
self.view.show_at_center(new_pointer)