Sublime Forum

How to improve C++ syntax highlighting?

#1

Hi I have the following chunk of code in ST3 using the default C++ syntax highlighting.

The reason it colors the call to countInsts() and DPRINTF() as red is because it mistakes them for variables. Same with the .value() of ->numCycles.

Using the C Improved plugin doesn’t help a ton:

While the call to countInsts(), DPRINTF(), and .value() are now correctly distinguished from variables (and as an added bonus the ->numCycles member variable is distinguished from local variables), the outer ::countInsts() method is no longer colored as it used to be.

Here is how that chunk of code looks like in VSCode:

Now granted, unlike ST3 + C Improved, VSCode fails to distinguish ->numCycles as a member variable. However, it correctly highlights the outer ::countInsts(), countInsts(), DPRINTF(), and .value(). As an added bonus, it also correctly colors the template and uint64_t keywords.

All-in-all, it seems that VSCode seems to “understand” C++ better. Note that I’ve done this syntax highlighting test not only with that snippet of code in the original source file, but also with that snippet copied to a separate blank file with nothing else in it. The results are the same for both editors, which confirms that they’re using context clues to do syntax highlighting rather than intimate knowledge of the project structure.

Is there any way to improve the C++ syntax highlighting of ST3?

0 Likes

#2

I didn’t type out the whole things because I am lazy, but with just the total += countInsts(tid); line the function call is scoped correctly for me. You can check that with Ctrl+Shift+Alt+P, which for me yields:

source.c++ meta.function-call.c++ variable.function.c++

Have you tried changing your color scheme?

0 Likes

#3

Here is what the code looks like using the default Monokai scheme along with both the default C++ syntax and the C Improved syntax (respectively):

Similar issues are there, though not as bad as with the other color scheme.

That keyboard shortcut isn’t working for me on Mac OS, what is it supposed to be doing?

0 Likes

#4

Could you paste your code here so I can look at it and make a screenshot from my scheme?

0 Likes

#5
template <class Impl>
int
ROB<Impl>::countInsts()
{
    int total = 0;

    for (ThreadID tid = 0; tid < numThreads; tid++)
        total += countInsts(tid);

    DPRINTF(ROB, "%d insts in ROB at cycle %lx\n", (uint64_t)cpu->numCycles.value());

    return total;
}
0 Likes

#6

This is with the default C++ syntax. I have my color scheme coded to render function calls in italics and not do color highlighting, which the syntax applies correctly. I believe that your issue is pretty much that variable.function and variable.other.readwrite (which is the scope of `numCycles) use the same color.

You could make an override for your scheme where you add a rule for variable.function. See the documentation for details.


Now, whether it is even correct or desired to use variable.other.readwrite for numCycles in this situation is not yet clear. There is a lengthy discussion about scope names of identifiers (usage and definition) on the issue tracker, but unless you are deep into scope names and customization, you probably won’t find it to be too enlighting.

0 Likes

#7

Why is ::countInsts() treated differently than countInsts() though?

0 Likes

#8

Because it’s the name of the function that is declared. Declarations are different from usages and get a scope under entity.name, i.e. entity.name.function for functions. They are scoped and highlighted differently because they are generally more important and should thus stand out more to be easier to notice. Like you can see above, I have a background color for such definitions.

0 Likes

#9

Ah I see… so the C Improved syntax was actually working as intended, all that is missing would be to add something for entity.name.function and I’d be set. Thanks!

0 Likes