Python 升华文本插件-如何找到选择中的所有区域

Python 升华文本插件-如何找到选择中的所有区域,python,sublimetext2,sublimetext3,sublimetext,sublime-text-plugin,Python,Sublimetext2,Sublimetext3,Sublimetext,Sublime Text Plugin,如何找到所选区域中的所有区域(也是regeon类型)? 如果我们调用此方法: def chk_links(self,vspace): url_regions = vspace.find_all("https?://[^\"'\s]+") i=0 for region in url_regions: cl = vspace.substr(region) code = self.get_response(cl) vspace.

如何找到所选区域中的所有区域(也是regeon类型)? 如果我们调用此方法:

def chk_links(self,vspace):
    url_regions = vspace.find_all("https?://[^\"'\s]+")

    i=0
    for 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 = i+1
    return i
在视图上下文中,例如:

chk_links(self.view)
所有这些都很好,但以这种方式:

chk_links(self.view.sel()[0])
我得到错误:AttributeError:“Region”对象没有“find_all”属性

你能找到的插件的完整代码


选择
类(由
View.sel()
返回)本质上只是表示当前选择的
区域
对象的列表。
区域
可以为空,因此列表始终包含至少一个长度为0的区域

唯一的方法是修改和查询其范围。类似的

您可以做的是找到所有感兴趣的区域,就像您的代码当前所做的那样,然后在迭代它们以执行检查时,查看它们是否包含在选择中

下面是上面示例的精简版本来说明这一点(为了清晰起见,您的一些逻辑已被删除)。首先收集整个URL列表,然后在迭代列表时,仅当存在无选择或存在选择URL区域包含在选择边界中时,才考虑每个区域

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    # Check all links in view
    def check_links(self, view):
        # The view selection list always has at least one item; if its length is
        # 0, then there is no selection; otherwise one or more regions are
        # selected.
        has_selection = len(view.sel()[0]) > 0

        # Find all URL's in the view
        url_regions = view.find_all ("https?://[^\"'\s]+")

        i = 0
        for region in url_regions:
            # Skip any URL regions that aren't contained in the selection.
            if has_selection and not view.sel ().contains (region):
                continue

            # Region is either in the selection or there is no selection; process
            # Check and
            view.add_regions ('url'+str(i), [region], "mark", "Packages/Default/Icon.png")
            i = i + 1

    def run(self, edit):
        if self.view.is_read_only() or self.view.size () == 0:
            return
        self.check_links (self.view)
进行“包含在选择中”检查是个好主意。非常感谢你。