python - Sublime Text plugin - how to find all regions in selection -
how find regions in selection (regeon type too)? if calling method:
def chk_links(self,vspace): url_regions = vspace.find_all("https?://[^\"'\s]+") i=0 region in url_regions: cl = vspace.substr(region) code = self.get_response(cl) vspace.add_regions('url'+str(i), [region], "mark", "packages/user/icons/"+str(code)+".png") = i+1 return
in view context, e.g.:
chk_links(self.view)
all works fine, in way:
chk_links(self.view.sel()[0])
i error: attributeerror: 'region' object has no attribute 'find_all'
full code of plugin can find here
the selection
class (returned view.sel()
) list of region
objects represent current selection. region
can empty, list contains least 1 region length of 0.
the methods available on selection
class modify , query it's extents. similar methods available on region
class.
what can instead find of interesting regions code doing, , you're iterating them perform check, see if contained in selection or not.
here's stripped down version of example above illustrate (some of logic has been removed clarity). first entire list of url's collected, , list iterated each region considered if there no selection or if there is selection and url region contained in selection bounds.
import sublime, sublime_plugin class examplecommand(sublime_plugin.textcommand): # check links in view def check_links(self, view): # view selection list has @ least 1 item; if length # 0, there no selection; otherwise 1 or more regions # selected. has_selection = len(view.sel()[0]) > 0 # find url's in view url_regions = view.find_all ("https?://[^\"'\s]+") = 0 region in url_regions: # skip url regions aren't contained in selection. if has_selection , not view.sel ().contains (region): continue # region either in selection or there no selection; process # check , view.add_regions ('url'+str(i), [region], "mark", "packages/default/icon.png") = + 1 def run(self, edit): if self.view.is_read_only() or self.view.size () == 0: return self.check_links (self.view)
Comments
Post a Comment