Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the Quick Panel in a Sublime Text 2 Plugin

I'm in the process of learning how to create Sublime Text 2 plugins. One of the things that I would like to do is take any highlighted text, check if a website will return a 200 at that address, and then place some information into the quick panel (and yes, I know that I should be doing the url lookup in a thread so that it doesn't block the main UI). Currently I 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 get 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 don't know how to initialize the WindowCommand class correctly, and I can't seem to find any documentation on it. Any help or hints here would be much appreciated.

like image 243
Jack Slingerland Avatar asked Oct 19 '12 14:10

Jack Slingerland


1 Answers

You don't need to create another instance of WindowCommand to accomplish this. Btw, you usually write commands but don't create their instances in your plugins. They are instantiated and invoked via key bindings or run_command method of View/Window/sublime.

You can get the current active window inside your check_urls command handler and display a quick panel.

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

Here is a full 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
like image 187
Maksim Skurydzin Avatar answered Nov 13 '22 04:11

Maksim Skurydzin