Sublime Forum

Programmatically set syntax scopes

#1

I’m working on plugins for the Swift language. As part of another feature, I’ve managed to load the compiler in-process and can run arbitrary queries to the compiler about the current buffer.

It occurs to me, now that I’ve set this up, it would be a convenient way to power syntax highlighting. After all, the compiler is the “final authority” about whether foo is a keyword or not. Moreover, Swift is a hard language to parse, the language is in a state of flux right now, and the parser changes almost weekly if you’re running the compiler out of master. (And if you are not, well, your syntax highlighting should be different than mine, but which definition should I distribute in the plugin?)

Is there any API I could use to just “assign” syntax highlighting scopes to the current buffer? I would prefer to just foist the compiler’s opinion of how to parse the language instead of writing a language definition that will go out of date as soon as it is written, and is already wrong for users with older language versions.

2 Likes

API Suggestions
#2

There is a method on the view called add_regions, but that will only set colors to regions as though they were specific scopes, without actually setting the scope - so fine for syntax highlighting but useless for semantics that rely on scopes for autocompletion etc.

One thing you could potentially do, is generate a sublime-syntax file specific for the current document on the fly, based on the output from the compiler, and get the view to use it. No idea how performant this would be, however, or how it would look while typing code…

0 Likes

#3

What else is scopes used by besides autocompletion?

I am actually using the compiler to power a full autocompletion engine, so if all I lose is the much weaker sublime completions I’m not very annoyed.

0 Likes

#4

I think just code-snippets, but I would guess that mainly they would rely on the “main” scope - i.e. the fact that it is a swift file - rather than a more specific scope.

0 Likes