- Code: Select all
edited = self.view.get_regions("edited_rgns") or []
a, b = edited[-1]
Is there a way that I can coerce [region] into a standard list of tuples pl?
edited = self.view.get_regions("edited_rgns") or []
a, b = edited[-1]import sublime
import sublime_plugin
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()
class TupleRegionCommand(sublime_plugin.TextCommand):
def run(self, edit):
regions = self.view.get_regions("bh_round") or []
if len(regions):
a, b = regions[-1]
print a, b
Except someone else patched Region.__iter__ to return an xrange(r.begin(), r.end()) and all was hell in the world :/
IMO, you wanna avoid monkey patches unless really neededimport bisect
import sublime
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):
ix = min(bisect.bisect(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 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 xrange(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
castles_made_of_sand wrote:Except someone else patched Region.__iter__ to return an xrange(r.begin(), r.end()) and all was hell in the world :/
IMO, you wanna avoid monkey patches unless really needed
agibsonsw wrote:Thank you @facelessuser
- Code: Select all
Except someone else patched Region.__iter__ to return an xrange(r.begin(), r.end()) and all was hell in the world :/
IMO, you wanna avoid monkey patches unless really needed
I agree, and it turns out I may not need to - but it's nice to know that I can if necessary.
Continuing this topic slightly, how can I create a RegionSet? It seems it is only possible via sel() or add_regions?
Yeah, monkey patching is seductively cool and there are legitimate uses for it, however that doesn't seem to be one. I've written heaps of plugins I can't share cause I used a heap of monkey patches.
You can't actually instantiate a RegionSet, only get a reference to one via view.sel(). You can make a crappy implementation in Python though.
>>> rs = PyRegionSet(list(view.sel()))
>>> view.add_regions('derpa', rs, '')
>>> view.get_regions('derpa')
[(2131, 2131)]
Users browsing this forum: No registered users and 16 guests