Hello,
I’m attempting to create a small plugin for adding some auto-completion results based on a specific syntax. Since the syntax is relevant, I will show some examples below:
Assets:Bank:Car
Assets:Savings:House
Basically these are “account strings” which define a financial account and include any parent accounts associated with that account. When a user types: “Assets:” and then queries for auto-complete, I want to return a list containing “Bank” and “Savings.”
I’ve already figured out the logic to complete this task, the problem is the way in which Sublime Text passes prefixes. Apparently it considers the colon character a “word separator” and as such when the user types in “Assets:” and hits the auto-complete key the actual value getting passed to my function is empty (because it sees the colon at the end and returns everything after it, which in the above case is nothing).
My logic will not work without getting the FULL context of what the user put in. In other words, I want to ignore all “word separators” EXCEPT a space.
I don’t know how auto-completion logic usually works, but it seems silly that Sublime Text would default to arbitrarily picking word separators for you. Is there a way I can override what word separates it’s using so that I can get the above functionality working as expected?
Here’s the full logic for those interested:
import sublime, sublime_plugin
import re
class BeancountAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
EXP = re.compile("(?:open )(?:%s)([A-Za-z]*)(?::*)" % prefix)
file_contents = view.substr(sublime.Region(0, view.size()))
accounts = EXP.findall(file_contents)
result = []
for account in accounts:
result.append((account, ""))
return result