Sublime Forum

Colouring typedefs in c++

#1

Hello, I would like Sublime to colour the name of my typedef variables in all my C++ files. For example:
typedef vector double_vector;
I would like “double_vector” to be shown on italic blue, just like the default C++ variables. Other people have made the same question, however I couldn’t find an answer in this forum.
I’m currently using the default C++ syntax.
Any thoughts?

Thank you.

1 Like

#2

What’s your ST build version?

0 Likes

#3

Hello, my build version is 3126.

0 Likes

#4

You’ll need to learn about scopes, .tmTheme files and use the show_scope_name command (look at the default key bindings for your platform).

Once you’ve figured out what scope it applied to the typedef, you can customize your .tmTheme file to apply the color you want to the scope selector for the typedef.

0 Likes

#5

Alright, thank you

0 Likes

#6

Ok, I have found the block of Monokai.thTheme file which colours all the variable types. How could I configure Monokai.thTheme to colour “double_vector”? The scope documentation is a little confusing.

0 Likes

#7

I wrote a post a few days ago that explains a bit how scopes work and how to get them, although you don’t need to concern yourself with the hierarchy part of that, just the specificity part:

Exploration exercise:

  • Throw your example line of typedef vector double_vector; into an empty buffer in sublime
  • Set one of the C based scopes for it (e.g. C++)
  • Place the cursor inside of the double_vector that you want to recolor
  • Trigger the command to view the scope from the menu

Depending on what syntax you applied, you will see a scope like one of the following:

source.c entity.name.type.typedef.c
source.c++ entity.name.type.typedef.c++
source.objc entity.name.type.typedef.objc
source.objc++ entity.name.type.typedef.objc++

Here the scope is two parts, source.blah and entity.blah; that’s the hierarchy part that you don’t care about. The second part (e.g. entity.name.type.typedef.c++) is the full scope of the thing that you’re trying to recolor,.

That’s the scope that is going to determine the color and style for what makes double_vector color the way that it is in this theme. The more parts of it you use, the more specific you dial it in. If you use the entire entity.name.type.typedef.c++ it will only change the color of a typedef in a C++ file. Drop off the .c++ part and it matches all typedefs regardless of file, etc.

So we know that entity.blahdeblahblah is what is coloring this. Now look inside of the Monokai.tmTheme file and search around for entity and you get a few hits. Check through all of them to see which one is the one that’s coloring this particular item. There are a few hits, but most of them are dialed to a specific exact scope which is not the one we want (for example entity.other.inherited-class, among others).

The only one that doesn’t work that way is the first one:

<dict>
    <key>name</key>
    <string>Entity name</string>
    <key>scope</key>
    <string>entity.name - (entity.name.filename | entity.name.section | entity.name.tag | entity.name.label)</string>
    <key>settings</key>
    <dict>
        <key>fontStyle</key>
        <string></string>
        <key>foreground</key>
        <string>#A6E22E</string>
    </dict>
</dict>

If you read through that post I linked above (I hope you did ;)) then we know that the scope that is trying to be matched here is anything that starts with entity.name except for some very specific ones like filenames and so on. That means that this is most likely the entry that we’re interested in.

To check, modify it by replacing the foreground color string from #A6E22E to something obvious, such as #FF00FF, and save the file. Now go back and check the example code in the test buffer; did double_vector change to a rather awesome looking magenta color? If so, that’s the one we want, if not, we need to keep looking. Either way, make sure to undo the change (unless you love magenta).

In this case it did what we want, so we know that this is the scope that’s currently being used. So we need to craft a new rule that will match our scope. For this purpose that’s as simple as duplicating the one that’s already being used and then modifying the scope to be more specific like the other examples in the file. Then you can select the appropriate font style and blue color for your use (or just use magenta, which is pretty awesome and an easy color code to remember ;)).

2 Likes

#8

Thank you, that’s some really useful infomation. I will look into it right now.

0 Likes

#9

This question has been answered by OdatNurd. These are his instructions:

  1. Open the C.sublime-syntax file inside of the C++ package (the C++ syntax borrows from it)
  2. Near the top is a section named variables: and inside of there the declaration of a variable named basic_types. Find that and go to the end of the line
  3. Add the text |double_vector right after the void line; The line should end like: unsigned|void|double_vector’

After doing that, double_vector is coloured the same as the other C++ variables.

Thank you again.

2 Likes

#10

Just realize that with each release, we often fix various bugs in syntaxes. Unless you port your changes to the newer versions, you won’t get these bugs fixes, since your customized version will override the default version.

2 Likes