Sublime Forum

[Clang] Syntax highlighting based on language tokens


Given that I have identified all the tokens in the file using clang, how can i create a plugin in sublime that uses this token information to create a custom syntax highlighting. I couldn’t find an API that could do this. I did find Yaml and tmL, but they are more regex based and have limited information. And i could not find a way to use the clang token information in these Yaml/tml based syntax highlighting files.



Syntax definitions in Sublime indeed rely on a regular expression based parsers to detect the syntax and structure of the language that they’re highlighting so that they can apply an appropriate scope so the color scheme can highlight it. They are also generic enough to apply to all files of a particular type.

As such there is no API mechanism for doing syntax highlighting via a plugin. To do something like that you would need to generate an appropriate file in the sublime-syntax or tmLanguage formats based on the information that you gathered.

That syntax would also be (from the sounds of it) unique to the file that it was created for, which means that you would need to re-generate the file for every file that’s opened, and generate it again every time the file is changed.

What you can do is apply colored regions to files that are opened, which is what things like Linters do (or spell checking, which underlines spelling errors). Depending on what you’re trying to do (e.g. highlight all variables of a given name) that could be an avenue of investigation.