Sublime Forum

Syntax highlighting (C++)

#1

The symbols/operators in C++ (stuff like brackets, == && += etc) colored just like normal text.
It is what’s intended or is it a bug?
Am I missing some settings in language/theme definition?

ST3 build#3012

0 Likes

#2

There are two things that affect this, 1. The chosen colouriser and 2. the syntax definition for the language. I assume you are using the correct syntax for you file (check the bottom right on Sublime’s status, it should say C++).

The chosen colour scheme defines “how much” colourising goes on typically; I’ve seen some colourisers with more variation than others.

However in Sublime’s C/C++/Objective C++ language definitions, brackets, operators and symbols are coloured the same. The Java language definition by contrast highlights them differently, even with the same colouriser chosen (the default Monokai for example). I’d suggest having a looksee at the Java syntax defs, migrating some of these across to the C/C++ ones if you really want operators and the like coloured differently.

0 Likes

#3

if you do whatq gates suggests, feel free to share your results :smile:

0 Likes

#4

[quote=“qgates”]There are two things that affect this, 1. The chosen colouriser and 2. the syntax definition for the language.
…]
[/quote]

Thanks, now I understand what I need to do.

Yeah, of course.

0 Likes

#5

I have also noticed that in classes Struct.dataType will work but Struct->ptDataType wont work the same… idk but something else to see if you can solve haha

0 Likes

#6

In contrast to the thread starter’s issue, your problem comes down to the colouriser that’s in use.

In other words, Sublime’s C++ syntax definition does differentiate these items (ie. data member of struct vs. data member of struct pointer); some colourisers choose to show this difference and some don’t. Which is fine, because either choice is probably a matter of taste.

The colouriser I use (Tomorrow Night) does show the difference; Monokai (Sublime’s default) doesn’t.

Personally, I prefer it because in complex lines like:

structptr->someclassinstance.anotherclassptr->anotherclassinstance.member;

…it’s far easier to instantly see what’s going on when data members of instances are colourised differently to members of pointers:

structptr->someclassinstance.anotherclassptr->anotherclassinstance.member;

Fixing your chosen colouriser to not show this difference would likely be very straightforward.

0 Likes

#7

I have figured out to highlight a data member of a struct that is a pointer (struct->variable).
In sublime test, go to preferences, browse packages. Go to the c++ folder and open C.tmLanguage. For me it was line 453, but go to this part of the code:

<key>access</key> <dict> <key>match</key> <string>\.[a-zA-Z_][a-zA-Z_0-9]*\b(?!\s*\()</string> <key>name</key> <string>variable.other.dot-access.c</string> </dict>

Now, in the string with the regex expression, at the end, add the following regex \->[a-zA-Z_][a-zA-Z_0-9]*\b(?!\s*\() The final result should look like this:

<key>access</key> <dict> <key>match</key> <string>\.[a-zA-Z_][a-zA-Z_0-9]*\b(?!\s*\()|\->[a-zA-Z_][a-zA-Z_0-9]*\b(?!\s*\()</string> <key>name</key> <string>variable.other.dot-access.c</string> </dict>

Your color scheme has to have a color option for the scope variable.other.dot-access (tomorrow night has this, you can add it to other color schemes as well). Just add this to your color scheme:

<dict> <key>name</key> <string>Variable</string> <key>scope</key> <string>variable</string> <key>settings</key> <dict> <key>foreground</key> <string>#Pick Color Here</string> </dict> </dict>

0 Likes