I think I refactored this as best I can. Much cleaner than any of the previous iterations. Any last comments or critiques?
- Code: Select all
class selectTagContents(sublimeplugin.TextCommand):
def run(self, view, args):
selections = []
closed = zip(
[view.substr(tag) for tag in view.findAll('(?<=</)[a-zA-Z0-9]+(?=>)')],
[region.begin() for region in view.findAll('</[a-zA-Z0-9]+>')]
)
opened = zip(
[view.substr(tag) for tag in reversed(view.findAll('(?<=<)[a-zA-Z0-9]+(?!.*?/>)'))],
[region.end() for region in reversed(view.findAll('<[^/].*?[^/]>'))]
)
if len(closed) > 0 and len(opened) > 0:
for region in view.sel():
curBegin = region.begin()
curEnd = region.end()
for cTag, cPoint in closed:
for oTag, oPoint in opened:
if cTag == oTag and cPoint >= curEnd and oPoint <= curBegin:
selections.append(sublime.Region(oPoint, cPoint))
break
else:
continue
break
view.sel().clear()
for region in selections:
view.sel().add(region)
e: Not sure how I missed how the command is fundamentally broken : / It's easily broken with any repeated tags in a file. I'll have to mess with it later to see if I can rethink how it goes about figuring out what tag the cursor is in.