-
Notifications
You must be signed in to change notification settings - Fork 23
/
expand_to_regex_set.py
56 lines (52 loc) · 1.68 KB
/
expand_to_regex_set.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
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_regex_rule(string, startIndex, endIndex, regex, type):
# if there is a selection (and not only a blinking cursor)
if(startIndex != endIndex):
selection = string[startIndex:endIndex]
# make sure, that every character of the selection meets the regex rules,
# if not return here
if len(regex.findall(selection)) != len(selection):
return None
# look back
searchIndex = startIndex - 1;
while True:
# begin of string is reached
if searchIndex < 0:
newStartIndex = searchIndex + 1
break
char = string[searchIndex:searchIndex+1]
# character found, that does not fit into the search set
if regex.match(char) is None:
newStartIndex = searchIndex + 1
break
else:
searchIndex -= 1
# look forward
searchIndex = endIndex;
while True:
# end of string reached
if searchIndex > len(string) - 1:
newEndIndex = searchIndex
break
char = string[searchIndex:searchIndex+1]
# character found, that does not fit into the search set
if regex.match(char) is None:
newEndIndex = searchIndex
break
else:
searchIndex += 1
try:
if startIndex == newStartIndex and endIndex == newEndIndex:
return None
else:
return utils.create_return_obj(newStartIndex, newEndIndex, string, type)
except NameError:
# newStartIndex or newEndIndex might not have been defined above, because
# the character was not found.
return None