-
Notifications
You must be signed in to change notification settings - Fork 23
/
expand_to_line.py
51 lines (44 loc) · 1.27 KB
/
expand_to_line.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
import re
try:
# Block it from trying to import something which should not be on the python sys.path
# https://github.com/hktonylee/SublimeNumberKing/issues/4
import expand_region_handler
import utils
except:
from . import utils
def expand_to_line(string, startIndex, endIndex):
linebreakRe = re.compile(r'\n')
spacesAndTabsRe = re.compile(r'([ \t]+)')
searchIndex = startIndex - 1;
while True:
if searchIndex < 0:
newStartIndex = searchIndex + 1
break
char = string[searchIndex:searchIndex+1]
if linebreakRe.match(char):
newStartIndex = searchIndex + 1
break
else:
searchIndex -= 1
searchIndex = endIndex;
while True:
if searchIndex > len(string) - 1:
newEndIndex = searchIndex
break
char = string[searchIndex:searchIndex+1]
if linebreakRe.match(char):
newEndIndex = searchIndex
break
else:
searchIndex += 1
s = string[newStartIndex:newEndIndex]
r = spacesAndTabsRe.match(s)
if r and r.end() <= startIndex:
newStartIndex = newStartIndex + r.end();
try:
if startIndex == newStartIndex and endIndex == newEndIndex:
return None
else:
return utils.create_return_obj(newStartIndex, newEndIndex, string, "line")
except NameError:
return None