Sublime Forum

How does a color scheme interact with the definition of language syntax?

#1

I find that some color schemes correctly color function calls, some don’t. Do the color schemes have any knowledge of the language syntax? How does it work?

1 Like

#2

There are two parts to syntax highlighting going on.

The first is the language syntax in use. It contains a series of rules that allow it to determine the syntax of the file, like detecting when a sequence of characters represents a function call or a string or a comment or so on.

Those rules apply what we call scopes to each character in the buffer that describes what the character is supposed to be for. Since it’s possible for a character to semantically represent more than one thing at a time, the syntax can apply more than one scope to each character to convey that information. Scopes are a pretty big topic; there’s a video I made here that may help a bit with visualizing things.

var s = "hello";
var n = 12;

Here every character has a scope that indicates that every character is a piece of a javascript file. Additionally the 12 has a scope indicating that it’s a number and "hello" has a scope that says that it’s a string. Going even further, the " characters also have a scope that says that they are starting and ending a string respectively.

That allows you to build up something like “the text 12 is a constant number in a javascript source file”.

The second thing that’s going on is the color scheme. Along with overall colors for things like the background, the cursor, selected text and so on it also contains a list of rules. The rules map scope selectors to font styles. So there would be a rule that says for example that comments should be an italic gray while strings should be green.

Given this, the person that makes the color scheme and the person that makes the language syntax need to work together to agree on what scopes to apply in what situations. That way all syntaxes scope a string the same way and all color schemes style a string using that known scope, and everyone is happy. To that end there are some scope guidelines that lay out what is generally expected so that things work

Some color schemes not being able to syntax highlight something like a function call correctly is generally a symptom that one end of that equation is using a scope that is not expected. Perhaps the syntax specification is not using one of the “approved” scopes for the function call, or perhaps the color scheme does not have an appropriate rule for it.

The latter is sometimes a result of using an older color scheme; the scope guidelines are an ever-changing thing as the community works on enhancing the syntax definitions. The Sublime developers tend to keep the color schemes that ship with Sublime up to date as these changes occur, but third party color schemes can quickly get out of date if the author isn’t paying attention.

4 Likes

#3

Thank you very much for the comprehensive response. I think I understand the complexities of making those to work together now.

0 Likes