Access to the quick panel in the Sublime Text 2 plugin

I’m participating in the process of learning how to create Sublime Text 2 plugins. One of the things I would like to do is to take any selected text, check if the site will return 200 to this address, and then put some information in the quick access panel (and yes I know that I have to do the url look in the stream so that it does not block the main user interface). I currently have:

import sublime import sublime_plugin import urllib2 class CheckUrlPanel(sublime_plugin.WindowCommand): def quick_panel(self, messages, flags): self.window.show_quick_panel(messages, None, flags) class CheckUrlsCommand(sublime_plugin.TextCommand): def run(self, edit): urls = [] selections = self.view.sel() for selection in selections: urls.append(self.view.substr(selection)) messages = self.validate_urls(urls) panel = CheckUrlPanel() panel.quick_panel(messages, sublime.MONOSPACE_FONT) def validate_urls(self, urls): messages = [] for url in urls: try: request = urllib2.Request(url, headers={ "User-Agent" : "Sublime URL Checker" }) response = urllib2.urlopen(request, timeout=3) message = '"%s" is a valid URL.' % url except Exception as (e): message = '"%s" is an invalid URL.' % url messages.append(message) return messages 

The error I am getting is:

 Traceback (most recent call last): File "./sublime_plugin.py", line 362, in run_ File "./CheckUrls.py", line 19, in run panel = CheckUrlPanel() TypeError: __init__() takes exactly 2 arguments (1 given) 

The problem is that I do not know how to properly initialize the WindowCommand class, and I can not find the documentation for it. Any help or tips here would be greatly appreciated.

+6
source share
1 answer

You do not need to create another instance of WindowCommand . Btw, you usually write commands, but don't create your instances in your plugins. They are created and called using key bindings or the run_command method of View / Window / sublime.

You can get the currently active window inside your check_urls command check_urls and display the quick access toolbar.

 window = sublime.active_window() window.show_quick_panel(messages, None, sublime.MONOSPACE_FONT) 

Here is the complete source:

 import sublime import sublime_plugin import urllib from urllib.request import urlopen class CheckUrlsCommand(sublime_plugin.TextCommand): def run(self, edit): urls = [] selections = self.view.sel() for selection in selections: urls.append(self.view.substr(selection)) messages = self.validate_urls(urls) window = sublime.active_window() window.show_quick_panel(messages, None, sublime.MONOSPACE_FONT) def validate_urls(self, urls): messages = [] for url in urls: try: response = urlopen(request, timeout=3) message = '"%s" is a valid URL.' % url except Exception as e: message = '"%s" is an invalid URL.' % url messages.append(message) return messages 
+12
source

Source: https://habr.com/ru/post/928124/


All Articles