Sublime Forum

Push list to query completions

#1

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.


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?

0 Likes

#2

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…

0 Likes

#3

@COD312

That’s a syntax error dude. All that PHP they teaching you in school is frying your mind haha

0 Likes

#4

Sadly I still haven’t figured out how to make this work :frowning: my colours aren’t added to the completions list.

0 Likes

#5

Well this seems to work:

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?

0 Likes

#6

[quote=“jbrooksuk”]Well this seems to work:

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?[/quote]

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

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

[quote]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
[/quote]

Looks like Buffer completes added after on_query_completions.

0 Likes