Sublime Forum

Custom autocomplete plugin starts working only after force reload

#1

Hi everyone. I made a simple autocomplete plugin using someone else’s template. It works fine on my Windows 10 machine but on Mac it only works after I open the .py file, hit Save to see a reloading plugin User.case_completions message. Could you please help me: why is this happening?

The plugin:

import sublime
import sublime_plugin

class CaseCompletionsListener(sublime_plugin.EventListener):
	items = []

	def __init__(self, *args, **kwargs):
		self.settings = sublime.load_settings('cases_dictionary.sublime-settings')
		self.items  = self.settings.get('items')

	def on_query_completions(self, view, prefix, locations):
		self.completions = []

		if not view.match_selector(locations[0], 'plain.strings'):
			return []

		default_completions = [view.extract_completions(prefix)]
		default_completions = [(item, item) for sublist in default_completions for item in sublist if len(item) > 3]
		default_completions = list(set(default_completions))
		completions         = list(self.items)
		completions         = [tuple(attr) for attr in self.items]
		completions.extend(default_completions)

		return (completions)

Sample content of the cases_dictionary.sublime-settings:

{
	"items": [
		["pinkpowder\tlab_402", "402"],
		["water\tlab_430", "430"],
		["securitycamera\tlab_405", "405"],
		["whitepowder\tele_514", "514"],
		["result_whitepowder\tele_515", "515"],
		["restoredphoto\tele_516", "516"],
		["result_sandwich\tele_506", "506"],
		["sandwich\tele_507", "507"],
		["result_momsnote\tele_508", "508"],
		["restoredcardboard\tele_509", "509"],
		["result_ironchains\tele_519", "519"],
	]
}

If I don’t force save it, here’s the message I’m getting:

Traceback (most recent call last):
 File "/Applications/Sublime [Text.app/Contents/MacOS/sublime_plugin.py](http://text.app/Contents/MacOS/sublime_plugin.py)", line 688, in on_query_completions
   res = callback.on_query_completions(v, prefix, locations)
 File "/Users/zahra/Library/Application Support/Sublime Text 3/Packages/User/case_completions.py", line 22, in on_query_completions
   completions         = list(self.items)
TypeError: 'NoneType' object is not iterable

It’s like it didn’t read the .sublime-settings file?

0 Likes

#2

I guess the event listener is initialized before the plugin API, so the settings are unavailable at that point, as mentioned in the docs.

1 Like

#3

Plugin classes, specifically event listeners, are instantiated when the plugin is loaded, but the API may not be ready at the time. Thus, reading settings in __init__ won’t work off a fresh start.

Instead, you should just fetch the settings when you actually need them. That way you also allow users to change their settings during runtime.

Other than that, I’m not exactly sure what this plugin is supposed to do. If you just want to add a couple static completions, you should use a .sublime-completions file

1 Like

#4

Thank you, moving items initialisation from __init__ to on_query_completions solved the issue.

FichteFoll, there will be a lot of completions actually (around ~300), they’re generated from spreadsheets, that are also different for different people. People type the name and it’s replaced by a number from the spreadsheet. And sometimes there can be items like phoneyellow, phonered, phoneblue: so I want it to be possible to select an item from the list. This is a crutch for a bad pipeline at work but I’m not in power to change the pipeline.

0 Likes