Sublime Forum

Getting full prefix for auto-completion

#1

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
0 Likes

#2

There’s a setting for what characters are considered word separators. It defaults to this:

    // Characters that are considered to separate words
    "word_separators": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?",

Presumably overriding it with a value that doesn’t include a colon should work for you. This may also be a setting that can be set per-syntax, although I’m not sure.

0 Likes

#3

Yes, I actually just found that answer and was coming back to post an update.

In short, you can make it syntax-specific. Easiest way:

  • Open up a file in the specific syntax you want it limited to

  • Go to Sublime Text -> Preferences -> Settings - More -> Syntax Specific - User

  • Past the above syntax, in my case I used:

    {
    “word_separators”: “./\()”’-,.;<>~!@#$%^&*|+=[]{}`~?",
    }

0 Likes

#4

There is a downside to this, though. ST will now treat all : colons as being part of a word, so double-clicking Assets:Savings:House anywhere will end up selecting the entire thing, as would Ctrl+D do. Same story for Ctrl+Left/Right navigation.

0 Likes