Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

syntax highlighting - Sublime Text - C++ Highlight

How can i make my class be highlighted as a normal primitive type like the int or double?

See a exemple:

enter image description here

I want that when i declare the Test aloha; Test be highlighted of the same way that int a.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The syntax highlighting is controlled by the tmLanguage file. In ST3 these are held in .cache files.

So follow these steps.

  • Open Tools -> Command Pallet and using Package Manager install PackageResourceViewer
  • In the Command Pallet select PackageResourceViewer: Open Resource
  • In the presented panel choose C++
  • In the presented panel choose C++.tmLanguage

Now you have the language definition file displayed for you.

Have a look at the form of how the parts of a language are defined. If we search for class we find the following as one of the matches:

<dict>
    <key>match</key>
    <string>(class|struct)s+([_A-Za-z][_A-Za-z0-9]*);</string>
    <key>name</key>
    <string>meta.class-struct-block.c++</string>
    <key>captures</key>
    <dict>
        . . .
    . . . 
</dict>

I chose that one because it involves some regular expresssion (regex) matching and that's the hint to do what you want to do.

Because ST has a lovely open framework any of us who know a little programming can add our own language component definitions to these .tmLanguage files. This flexibility is part of why ST is my choice.

So let's see how to do that.

First thing is to save the C++.tmLanguage file we have open in the editor into our User folder.

We do this both to keep the original as a rollback if things go wrong and because it means that our version in User will not get overwritten each time ST updates. The way ST loads files means that duplicates in the User folder overwrite values in the default location so our User versions take precedence in what is running when we use ST. Cool hey?

So now we have our own version of the C++.tmLanguage file let's play with it.

Find a definition that looks a bit like what we want to do and duplicate it.

I am going to copy this:

<dict>
    <key>match</key>
    <string>(class|wchar_t|nullptr_t)</string>
    <key>name</key>
    <string>storage.type.c++</string>
</dict>

First thing I am going to do is decide what I am going to name my new language component.

I could simply give it the name of an existing language component that has the colouring I want to have. This has the advantage that if that name is already in place in the existing colour schemes I will get the highlighting for free. Feel free to make this choice.

But to explore more of the system I am going to invent my own name and scope it to myself. So I will call it duncan.name.class. You can use any name you like that does not clash with an existing name.

So I will make that change in the duplicate:

<dict>
    <key>match</key>
    <string>(class|wchar_t|nullptr_t)</string>
    <key>name</key>
    <string>duncan.name.class</string>
</dict>

Now I need to write the regex to identify my new language component. This is pretty easy since I am going to assume we are all following the fairly standard practice of naming our classes with leading Caps and not using that for other language components so my regex just needs to find any string that starts with an UPPERCASE alpha, which is easily defined as [A-Z] followed by any alphanumeric character/s. Note that this precludes punctuation (like underscore _) so if you use those in your class names you'll need to expand the regex to include those characters.

So let's change the match condition to use that regex:

<dict>
    <key>match</key>
    <string>([A-Z][a-zA-Z0-9]+)</string>        
    <key>name</key>
    <string>duncan.name.class</string>
</dict>

Now I have a new language component I need to tell my theme how to handle it. I am going to work on the Amy theme for this example.

So back to the ever helpful PackageResourceViewer like this:

  • In the Command Pallet select PackageResourceViewer: Open Resource
  • In the presented panel choose Color Scheme - Default
  • In the presented panel choose Amy.tmTheme

Now you have the theme file displayed for you.

Again I want to save my copy into the User folder and then duplicate an existing example. I am going to duplicate String but anything that grabs your fancy is fine.

<dict>
    <key>name</key>
    <string>String</string>
    <key>scope</key>
    <string>string</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#999999</string>
    </dict>
</dict>

Now I'll change the duplicate of String to do colouring for my new language component.

<dict>
    <key>name</key>
    <string>Class Names :: Duncan</string>
    <key>scope</key>
    <string>duncan.name.class</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#999999</string>
        <key>background</key>
        <string>#FFFFFF</string>
        <key>fontStyle</key>
        <string>bold</string>
    </dict>
</dict>

I hope you recognise the names I am using from the work we did above?

I have chosen a slightly mad set of syntax highlighting preferences just to show some of the options you have available. Knock yourself out playing with ideas here.

Does all this make sense?

With these ideas in place you can fix any syntax highlighting niggles you have in ST3 to work just exactly as you like. Perhaps my regex above is not too great? I didn't think about it a whole lot so you may find you need to do more work there to get just what you want.

Let me know if some of this doesn't make sense and I'll try and do better.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...