-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceblockjumper.py
180 lines (149 loc) · 6.84 KB
/
spaceblockjumper.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
# -*- coding: utf-8 -*-
from sublime import *
from sublime_plugin import *
import re
SETTINGS = "Base File.sublime-settings"
class SpaceBlockJumper(TextCommand):
def __init__(self, view):
super(View).__init__()
self.view = view
self.skipClosestEdge = load_settings(SETTINGS).get("Skip Closest Edge", True)
self.jumpToBlockSeparator = load_settings(SETTINGS).get("Jump To Block Separator", False)
self.ShowCursorAtCenter = load_settings(SETTINGS).get("Show Cursor At Center", True)
self.mode = 0
@classmethod
def updateContext(self, fun):
def wrapper(self, *args, **kwargs):
self.selection = self.view.sel()[0]
self.selections = self.view.sel()
self.startCursorPoint = self.selection.b
self.startCursorRow, self.startCursorCol = self.rowcol(self.startCursorPoint)
return fun(self, *args, **kwargs)
return wrapper
def jump(self, direction, select):
print("jump")
if select:
for region in self.selections:
newRegions = ExtendRegion(region, direction)
self.view.sel().clear()
self.view.sel().add_all(newRegions)
else:
self.view.sel().clear()
rightCheck = min(self.getLineRegion(newLine, full=True).size(), self.startCursorCol)
leftCheck = max(self.getLineStart(newLine), rightCheck)
newRegion = Region(self.point(newLine, leftCheck ))
self.view.sel().add(newRegion)
if self.ShowCursorAtCenter:
self.view.show_at_center(newRegion)
else:
self.view.show(newRegion)
def ExtendRegion(self, region, direction):
startPoint = region.end() if direction -1 else region.begin()
newLine = self.getNewLine(startPoint, direction)
newRegion = self.getLineRegion(newLine)
print(region, region.intersection(newRegion))
return region.intersection(newRegion)
def selectBlock(self):
topLine = self.getNewLine(self.startCursorRow, -1, stayInBlock=True)
bottomLine = self.getNewLine(self.startCursorRow, 1, stayInBlock=True)
newRegionList = []
# modes: "minimum", "expanded", "multiline-minimum", "multiline-expanded"
if self.mode in [0, 2]:
r1 = self.getLineRegion(topLine)
r2 = self.getLineRegion(bottomLine)
elif self.mode in [1, 3]:
r1 = self.getLineRegion(topLine, full=True)
r2 = self.getLineRegion(bottomLine, full=True)
newRegion = r1.cover(r2)
if self.mode in [2, 3]:
newRegionList = self.view.split_by_newlines(newRegion)
if self.mode == 2:
tmpList = []
for lineRegion in list(newRegionList):
tmpList.append(self.getLineRegion(self.rowcol(lineRegion.a)[0]))
newRegionList = tmpList
if re.sub('[\t\s\n]', '', self.view.substr(newRegion)) == "":
self.view.sel().add(newRegion)
elif (newRegion == self.view.sel()[0] or newRegionList == self.view.sel()):
self.mode = (self.mode+1)%4
self.selectBlock()
else:
self.view.sel().clear()
if len(newRegionList) > 0:
self.view.sel().add_all(newRegionList)
self.view.show(newRegionList[0].cover(newRegionList[-1]))
else:
self.view.sel().add(newRegion)
self.view.show(newRegion)
def getNewLine(self, currLine, direction, stayInBlock=False):
nextLine = currLine + direction
if not self.isInBounds(nextLine):
return currLine
if self.jumpToBlockSeparator:
currLine += direction
while self.isInBounds(currLine+direction) and self.isEmptyLine(currLine) == self.isEmptyLine(currLine+direction):
currLine += direction
return currLine+(direction if not self.isEmptyLine(currLine) else 0)
# moving in emptiness
if (self.isEmptyLine(currLine) and self.isEmptyLine(nextLine)):
currLine += direction
while self.isInBounds(currLine+direction) and self.isEmptyLine(currLine+direction):
currLine += direction
if not stayInBlock:
currLine += direction
# moving inside a block
elif not self.isEmptyLine(currLine) and not self.isEmptyLine(nextLine):
currLine += direction
while self.isInBounds(currLine+direction) and not self.isEmptyLine(currLine+direction):
currLine += direction
# moving from an edge of a block
elif not self.isEmptyLine(currLine) and self.isEmptyLine(nextLine) and not stayInBlock:
currLine += direction
if self.skipClosestEdge:
while self.isInBounds(currLine+direction) and not (not self.isEmptyLine(currLine) and self.isEmptyLine(currLine+direction)):
currLine += direction
else:
while self.isInBounds(currLine+direction) and self.isEmptyLine(currLine):
currLine += direction
# moving to an edge of a block
elif self.isEmptyLine(currLine) and not self.isEmptyLine(nextLine) and not stayInBlock:
currLine += direction
return currLine
def isInBounds(self, line):
bufferRegion = Region(0,self.view.size())
return True if line >= 0 and line < len(self.view.lines(bufferRegion)) else False
def isEmptyLine(self, line):
linestr = self.view.substr(self.getLineRegion(line))
return True if linestr == "" else False
def getLineRegion(self, line, full=False):
if full:
lineRegion = self.view.line(self.point(line, 0))
else:
wordStartCol = self.getLineStart(line)
wordStartPoint = self.point(line, wordStartCol)
lineRegion = Region(wordStartPoint, self.view.line(wordStartPoint).end())
return lineRegion
def getLineStart(self, line):
n = 0
p = self.point(line, n)
while(self.view.substr(p) == " " or self.view.substr(p) == "\t"):
n += 1
p = self.point(line, n)
return n
def getCurrentRowCol(self, whichEnd):
if whichEnd == "begin":
return self.view.rowcol(self.view.sel()[0].begin())
elif whichEnd == "end":
return self.view.rowcol(self.view.sel()[0].end())
def rowcol(self, point):
return self.view.rowcol(point)
def point(self, row, col):
return self.view.text_point(row, col)
class SpaceBlockJumpCommand(SpaceBlockJumper):
@SpaceBlockJumper.updateContext
def run(self, edit, direction, select=False):
self.jump(direction, select)
class SpaceBlockSelectBlockCommand(SpaceBlockJumper):
@SpaceBlockJumper.updateContext
def run(self, edit):
self.selectBlock()