1

如何在選擇中找到所有區域(也包括regeon類型)? 如果我們調用這個方法:Sublime Text插件 - 如何在選擇中找到所有區域

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的:'區域'對象沒有屬性'find_all'

插件的完整代碼,你可以找到here

Sublime "View" method documentation

回答

2

Selection類(由View.sel()返回)基本上只是一個Region對象表示當前選擇的列表。 A Region可以是空的,所以列表總是包含至少一個長度爲0的區域。

唯一的methods available on the Selection class是修改和查詢它的擴展。類似methods are available on the Region class

可以做什麼,而是找你的代碼當前正在做的所有有趣的區域,然後當你迭代他們來執行你的檢查,看看他們是否包含在選擇或不。

下面是您的示例的精簡版本,以說明此問題(爲清楚起見,您的某些邏輯已被刪除)。首先URL的整個列表收集,然後如果有NO選擇作爲列表迭代每個區域只考慮或者選擇的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) 
+0

這是使「包含在選擇」檢查中的好主意。非常感謝你。 –

相關問題