-
Notifications
You must be signed in to change notification settings - Fork 1
/
region_helpers.py
188 lines (141 loc) · 4.66 KB
/
region_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
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
181
182
183
184
185
186
187
188
#coding: utf8
#################################### IMPORTS ###################################
# Sublime Libs
import sublime
# Std Libs
import bisect
import pprint
import difflib
################################### CONSTANTS ##################################
TESTS = 0
################################ REGION HELPERS ################################
def normalized_region(r):
return sublime.Region(*(sorted((r.begin(), r.end())) + [r.xpos]))
def normalized_regions(rs):
return (normalized_region(r) for r in rs)
def subtract_region(r1, r2):
if not r1.contains(r2): r2 = r1.intersection(r2)
r1s, r1e = r1.begin(), r1.end()
r2s, r2e = r2.begin(), r2.end()
if r1s == r2s and r1e == r2e:
return []
elif r1s == r2s:
return [sublime.Region(r2e, r1e)]
elif r1e == r2e:
return [sublime.Region(r1s, r2s)]
else:
return [sublime.Region(r1s, r2s), sublime.Region(r2e, r1e)]
class PyRegionSet(list):
def __init__(self, l=[], merge=False):
if merge:
list.__init__(self)
for r in l: self.add(l)
else:
list.__init__(self, l)
def _bisect(self, r, bisector):
ix = min(bisector(self, r), len(self) -1)
reg = self[ix]
if r < reg and not (reg.contains(r) or reg.intersects(r)): ix -= 1
return max(0, ix)
def bisect(self, r):
return self._bisect(r, bisect.bisect)
def clear(self):
del self[:]
def contains(self, r):
return self and self.closest_selection(r).contains(r)
def closest_selection(self, r):
return self[self.bisect(r)]
def add(self, r):
if not self: return self.append(r)
for ix in range(self.bisect(r), -1, -1):
closest = self[ix]
if closest.contains(r) or closest.intersects(r):
self[ix] = closest.cover(r)
return
elif r.contains(closest) or r.intersects(closest):
r = r.cover(closest)
if ix: del self[ix]
else: self[ix] = r
else:
self.insert(ix+1, r)
return
def subtract(self, r):
ix = self.bisect(r)
while self:
closest = self[ix]
if closest.contains(r) or closest.intersects(r):
del self[ix]
for reg in subtract_region(closest, r):
bisect.insort(self, reg)
if ix == len(self): break
continue
break
def test_PyRegionSet():
cb = sublime.set_clipboard
def eq(d1, d2, msg='Objects unequal'):
if d1 != d2:
if not isinstance(d1, str):
d1, d2 = list(map(pprint.pformat, (d1, d2)))
cb(d1)
diff_msg = ('\n' + '\n'.join(difflib.ndiff(
d1.splitlines(),
d2.splitlines())))
raise ValueError('%s:\n\n:%s' % (msg, diff_msg))
RS = PyRegionSet
class R(sublime.Region):
def __repr__(self):
return 'R%s' % sublime.Region.__repr__(self)
def RS(*pts):
def inner(buf=[]): # fucking stupid linter
for i, pt in enumerate(pts):
if i % 2: # 1
buf.append(pt)
yield R(*buf)
else: # 0
buf=[pt]
return PyRegionSet(inner())
def PRS(*pts):
r = RS(*pts)
cb = lambda r: sublime.set_clipboard("assert r == %r" % r)
s = lambda *a: r.subtract(R(*a)) or cb(r)
a = lambda *a: r.add(R(*a)) or cb(r)
return r, s, a
r, s, a = PRS(1,1, 5,15, 19,19)
eq(r.closest_selection(R(5, 7)), r[1])
if ALL_TESTS:
r, S, A = PRS(0, 10)
A(5, 15)
ex = [R(0, 15)]
eq (r, ex)
if ALL_TESTS:
r, S, A = PRS(0, 15)
A(20, 30)
eq(r, [R(0, 15), R(20, 30)])
A(25, 30)
if ALL_TESTS:
r, S, A = PRS(0, 15, 20, 30)
A(56, 100)
A(80, 300)
eq (r, [R(0, 15), R(20, 30), R(56, 300)])
A(16, 18)
eq (r, [R(0, 15), R(16, 18), R(20, 30), R(56, 300)])
S(0, 10)
eq (r, [R(10, 15), R(16, 18), R(20, 30), R(56, 300)])
S(5, 80)
eq (r, [R(80, 300)])
r.clear()
eq (r, [])
if ALL_TESTS:
r, s, a = PRS(1,1, 2,2, 3,3, 5,8)
s(2)
eq (r, [R(1, 1), R(3, 3), R(5, 8)])
if ALL_TESTS:
r, s, a = PRS(1,1, 5,15, 19,19)
assert R(5, 7) < R(5, 15)
s(5, 7)
eq (r, [R(1, 1), R(7, 15), R(19, 19)])
if ALL_TESTS:
r, s, a = PRS(1,1, 19,19)
a(1, 2)
eq (r, [R(1, 2), R(19, 19)])
if TESTS: test_PyRegionSet()