Home Download Buy Blog Forum Support

New to Python, looking for critique. Select tag contents.

Re: New to Python, looking for critique. Select tag contents.

Postby edanm on Sun May 09, 2010 2:10 pm

Technically, you could just create one by doing "rs = sublime.RegionSet()" like you create a new Region.
But I tried this, and I got an error saying that you can't create a RegionSet from Python. So supposedly, it's only for internal use in Sublime, not for plugins.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: New to Python, looking for critique. Select tag contents.

Postby Anomareh on Sun May 09, 2010 2:45 pm

Ah ok. Thanks.
Anomareh
 
Posts: 221
Joined: Thu Oct 01, 2009 7:32 pm

Re: New to Python, looking for critique. Select tag contents.

Postby Anomareh on Sun May 09, 2010 4:24 pm

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.
Anomareh
 
Posts: 221
Joined: Thu Oct 01, 2009 7:32 pm

Previous

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests

cron