Home Download Buy Blog Forum Support

Push list to query completions

Push list to query completions

Postby jbrooksuk on Wed Jan 18, 2012 2:54 pm

I'm further developing my SublimeWebColors plugin so that the colors will get added to the query completions drop down, however I'm unable to push my list through from the WindowCommand to the EventListener.

Code: Select all

import sublime, sublime_plugin
import webcolors

class WebColorsCommand(sublime_plugin.WindowCommand):
   colorList = []
   def __init__(self, *args, **kwargs):
      super(WebColorsCommand, self).__init__(*args, **kwargs)
      colorList = []
      self.generateColorDialog()

   def run(self):
      self.window.show_quick_panel(self.colorList , self.callback, sublime.MONOSPACE_FONT)

   def callback(self, index):
      if (index > -1): # No value is -1
         colorValue = self.colorList[index][1]
         self.window.active_view().run_command("insert_web_colors", {"value": colorValue})

   def generateColorDialog(self):
      for name, color in webcolors.css3_names_to_hex.iteritems():
         self.colorList.append([name, color.upper()])

      self.window.active_view().run_command("web_colors_complete")

class InsertWebColorsCommand(sublime_plugin.TextCommand):
   def run(self, edit, value):
      for region in self.view.sel():
         self.view.replace(edit, region, value)

class WebColorsCompleteCommand(sublime_plugin.EventListener):
   tempList = []
   def on_query_completions(self, view, prefix, locations):
      for name, color in WebColorsCommand.colorList.iteritems():
         print name, color.upper()
         self.tempList.append([name, color.upper()])
         
      return [(x) for x in list(set(self.tempList))]

Ideas?
jbrooksuk
 
Posts: 772
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Push list to query completions

Postby C0D312 on Wed Jan 18, 2012 10:03 pm

on_query_completions is called every time autocomplete is called. So calling it within init wouldn't make sense. Additionally, on_query_completions expects you to return a list of tuples. I'm assuming your list of webcolors doesn't change, so I'd recommend taking a look at Packages/HTML/html_completions.py.

Thanks castles. Damn school's making me dumb again...
Last edited by C0D312 on Wed Jan 18, 2012 11:53 pm, edited 11 times in total.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Push list to query completions

Postby sublimator on Wed Jan 18, 2012 10:56 pm

@COD312

That's a syntax error dude. All that PHP they teaching you in school is frying your mind haha
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Push list to query completions

Postby jbrooksuk on Thu Feb 02, 2012 10:19 am

Sadly I still haven't figured out how to make this work :( my colours aren't added to the completions list.
jbrooksuk
 
Posts: 772
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Push list to query completions

Postby jbrooksuk on Thu Feb 02, 2012 10:39 am

Well this seems to work:

Code: Select all
class WebColorsCompleteCommand(sublime_plugin.EventListener):
   def on_query_completions(self, view, prefix, locations):   
      css3ColorList = [(str(x),) * 2 for x in webcolors.css3_names_to_hex]
      return css3ColorList


However I seem to have to iterate the webcolors class, even though I already have a color list from the WebColorsCommand class?
jbrooksuk
 
Posts: 772
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Push list to query completions

Postby Cjkjvfnby on Thu Feb 02, 2012 1:30 pm

jbrooksuk wrote:Well this seems to work:

Code: Select all
class WebColorsCompleteCommand(sublime_plugin.EventListener):
   def on_query_completions(self, view, prefix, locations):   
      css3ColorList = [(str(x),) * 2 for x in webcolors.css3_names_to_hex]
      return css3ColorList


However I seem to have to iterate the webcolors class, even though I already have a color list from the WebColorsCommand class?


I have same goal. I want to clear autocomplete before pushing my values.

PS. Adding complete items via Snippets
http://sublimetext.info/docs/en/extensi ... tions.html

Note that completions in the broader sense of words that Sublime Text will look up and insert for you are not limited to completions files, because other sources contribute to the list of words to be completed, namely:
Snippets
API-injected completions
Buffer contents


Looks like Buffer completes added after on_query_completions.
Cjkjvfnby
 
Posts: 20
Joined: Wed Feb 01, 2012 11:35 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests