-
Notifications
You must be signed in to change notification settings - Fork 53
/
helpers.py
55 lines (43 loc) · 1.55 KB
/
helpers.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
import re
from sublime import Region
import sublime_plugin
class LineIndexError(Exception):
pass
class BaseBlockCommand(sublime_plugin.TextCommand):
def _get_row_text(self, row):
if row < 0 or row > self.view.rowcol(self.view.size())[0]:
raise LineIndexError('Cannot find table bounds.')
point = self.view.text_point(row, 0)
region = self.view.line(point)
text = self.view.substr(region)
return text
def get_cursor_position(self):
return self.view.rowcol(self.view.sel()[0].begin())
def get_block_bounds(self):
"""given the cursor position as started point,
returns the limits and indentation"""
row, col = self.get_cursor_position()
upper = lower = row
try:
while self._get_row_text(upper - 1).strip():
upper -= 1
except LineIndexError:
pass
else:
upper += 1
try:
while self._get_row_text(lower + 1).strip():
lower += 1
except LineIndexError:
pass
else:
lower -= 1
block_region = Region(self.view.text_point(upper - 1, 0),
self.view.text_point(lower + 2, 0))
lines = [self.view.substr(region) for region in self.view.lines(block_region)]
try:
row_text = self._get_row_text(upper - 1)
except LineIndexError:
row_text = ''
indent = re.match('^(\s*).*$', row_text).group(1)
return block_region, lines, indent