Sublime Forum

Are there any more resources for creating code completion files for Sublime Text 4?

#21

That is correct; the syntax rule that is matching that only consumes the text that it thinks is invalid, so if the cursor is after !< it’s not invalid at that point, only within.

Yes, but only via plugin code, similar to what the plugin that DeathAxe linked above from the HTML package.

0 Likes

#22

A minimal example for a plugin based completions plugin would be:

import sublime
import sublime_plugin


class YamlCompletions(sublime_plugin.EventListener):

    def on_query_completions(self, view, prefix, locations):
        if not view.match_selector(locations[0], "source.yaml"):
            return

        pt = locations[0]

        # get the text in front of first caret
        token = view.substr(sublime.Region(pt - 2, pt))
        if token == "!<":
            return sublime.CompletionList(
                [
                    sublime.CompletionItem(
                        trigger="!<Rule",
                        kind=[sublime.KindId.FUNCTION, "r", "Rule"],
                        annotation="My Rule",
                        details="This is a rule to..."
                    )
                ]
            )

We need to read content from before caret’s location as prefix is empty due to !< being word separator characters (which is also the reason why static completions don’t work).

So we get the text left to caret manually and return completions, if they match certain criteria.

0 Likes

#23

Thank you for the example. Very helpful.

Am I correct that it would also be possible to move the cursor one character to the left, then read the context of that block? I was looking at the ScopeContext plugin and it appears to have functions that use view.scope_name to retrieve the current scope.

I am investigating further…

0 Likes

#24

You can do literly anything to analyze code in order to select completions.

I would however suggest using match_selector as it returns boolean result for whether a given selector (expression of scopes) matches given text point. scope_name returns the whole - probably complicated - list of scope names applied to a a token on given text point. You’d need to manually evaluate it further to check, whether your desired conditions match.

0 Likes