So I want to implement variable syntax highlighting for my custom c++ theme on sublime text 3. So I wanted to create a custom scope to then use on my tmTheme file so I went to add to the default sublime text c++ syntax and added this to detect variables:
c++.sublime-syntax:
contexts:
main:
- include: preprocessor-global
- include: global
# my custom scope
normal-variables:
- match: '([a-zA-Z0-9_]+(\ +=|;))'
captures:
1: variable.other.name.member.c++
2: variable.other.name.end.c++
Here is my objective: if string contains uppercase,lowercase, numbers AND is in the form of string -> one space or more -> one equal sign(=) OR the string just has a semicolon it is captured.
And so in this case I have two capture groups the first one will match the variable name and will be considered as variable.other.name.member.c++ and the second group will be the semicolon or the equal and additional spaces and would be considered as variable.other.name.end.c++.
Here are some examples of what I expect from sublime text to recognize as variables and assign them the custom scopes I included in the sublime-syntax file:
//var has to be captured
int var = //in the format anyVarName =
//or
int var;//in the format anyVarName;
Now I saved this file and tried to show the scope of a variable and it just recognize it as a source.c++ so my code does not work, would you guys have any ideas?.Thanks in advance
Unless your ultimate goal is to add syntax highlighting to the names of variables at the point where they're declared but nowhere else in the file, unfortunately you can't do what you're trying to do here.
The reason for this is that the rules in a syntax definition determine what the different portions of the file represent based solely on the context in which they're found and recognized in the input file without keeping any (usable) record of what the text meant.
As such, except for the point where the variable is declared, there's nothing to tell the syntax definition that the name of the variable is special so that it can apply the same scope later.
To help illustrate this, here's some sample nonsense code, as highlighted by the existing C++ syntax definition:
#define var X
int var = 10;
var = 29;
In line one, the syntax definition sees that there's a #define
token, which means that this is the start of a preprocessor directive. Based on that, it knows that the var
in that line is the name of a macro, and it gets assigned the scope source.c++ meta.preprocessor.macro.c++ entity.name.constant.preprocessor.c++
, which lets the syntax highlighter know that this is a named entity that's a preprocessor value; the rule for that in my color scheme colors it as green.
However the fact that var
is special isn't retained anywhere because Sublime isn't compiling the code, it's just looking at it lexically (as a series of tokens).
Hence on line 2 although it can determine that int
is an internal type, that =
is an operator and that 10
is a number, it doesn't remember that var
used to be a preprocessor directive. There isn't a rule that marks it as a variable, so it's just considered to be unscoped text.
It's certainly possible to apply a rule that would trigger on line 2 and apply your scope to the text var
; in that specific line it can be inferred that it's probably the name of a variable in the same way that line 1 can tell that it's a preprocessor macro.
The reason that the default syntax doesn't do that already is because it would only allow for syntax highlighting var
in the line where it's declared and nowhere else, which isn't terribly helpful.
For similar reasons, custom types defined via a typedef
aren't recognized in variable declarations either. You may also notice that the name of a class is recognized as such in the class definition, constructor and destructor, but not as a return type in methods, and when invoking the constructor it's seen as a function call instead.
The reason your syntax adjustment above doesn't have any effect on anything is because it provides a new context but doesn't include it anywhere, so it's effectively disabled. How syntax definitions work is fairly complicated, but in short you've provided a new named set (context
) of match
rules, but without telling the syntax definition where they should be applied, they have no effect on anything.