Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of specific words in sublime text 3

I search on this site how to change color of some specific words in Sublime Text 3, but cannot find it without replacing the tmlanguage file.

What I'ld like is to change color for true (make it green) and false (make it red) or things like the $this.

I'm specially interested for PHP and JS scope, as I don't really use any other language. I tried PersistentRegexHighlight plugin, but I'm not quite satisfy with the result. The words are not colored on file opening, and it uses highlight, so I cannot just change text color.

I read some answers about changing the PHP.tmlanguage file, but doing that, I'll loose any change in future versions, right? I thought making another PHP.tmlanguage in the user dir will 'add it' to the main one, like keybinding file, but no.

Do you have any idea how to do this?

Thank you.

like image 624
Constantin Guay Avatar asked Oct 07 '13 22:10

Constantin Guay


1 Answers

Your own PHP.tmlanguage in User directory is not working because editing a .tmlanguage file is a way to describe language and set up scopes, not a way to highlight terms. So you need additional actions in order to highlight those things in the editor window.

Highlighting is done in .tmTheme file. In the file, you can use scopes from .tmLanguage in order to apply color for the text suitable for the scope.

1. PHP.tmLanguage

As I know, maybe I wrong, it's not possible to create a "patch" file for syntax definition. You can't create a little patch file which will extend PHP syntax. The syntax is described in one file, so all scopes for PHP must be described in one file. If you want to add, delete, or change something for PHP language, you need to edit appropriate file, which must contain syntax definition for the whole PHP. Good thing is, it is possible to copy .tmLanguage file to the User directory and keep native file safe.

Let's copy PHP.tmLanguage in your User folder and edit it a bit. You can find those strings inside:

<dict>
    <key>match</key>
    <string>(?i)\b(TRUE|FALSE|NULL|__(FILE|DIR|FUNCTION|CLASS|METHOD|LINE|NAMESPACE)__|ON|OFF|YES|NO|NL|BR|TAB)\b</string>
    <key>name</key>
    <string>constant.language.php</string>
</dict>

As you can see, right now both true and false are inside the same scope, constant.language.php. You can change it by removing them from the scope and adding two separate scopes for them.

<dict>
    <key>match</key>
    <string>(?i)\b(NULL|__(FILE|DIR|FUNCTION|CLASS|METHOD|LINE|NAMESPACE)__|ON|OFF|YES|NO|NL|BR|TAB)\b</string>
    <key>name</key>
    <string>constant.language.php</string>
</dict>
<dict>
    <key>match</key>
    <string>(?i)\b(TRUE)\b</string>
    <key>name</key>
    <string>true.constant.language.php</string>
</dict>
<dict>
    <key>match</key>
    <string>(?i)\b(FALSE)\b</string>
    <key>name</key>
    <string>false.constant.language.php</string>
</dict>

2. Theme modification

It's not possible to setup colors in tmLanguage file because its purpose is determine scopes — language elements. All syntax highlighting done in .tmTheme files. So best thing I can imagine is copying current theme file to the User directory and edit it.

For example, if your current theme is Slush & Poppies, you would copy it from ST3/Packages/Color Scheme - Default.sublime-package\Slush & Poppies.tmTheme to the ST3/User/Data/Packages/User dir and edit.

Search for the string, constant.language. It is part of this block:

<dict>
    <key>name</key>
    <string>Built-in constant</string>
    <key>scope</key>
    <string>constant.language</string>
    <key>settings</key>
    <dict/>
</dict>

Looks like Slush & Poppies has no settings for coloring constants. But it's not a problem. In the code below I've written a sample which helps understand how to apply color settings. So, let's change above block to this one:

<dict>
    <key>name</key>
    <string>Built-in constant</string>
    <key>scope</key>
    <string>constant.language</string>
    <key>settings</key>
    <dict/>
</dict>
<dict>
    <key>name</key>
    <string>TRUE</string>
    <key>scope</key>
    <string>true.constant.language</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#008900</string>
    </dict>
</dict>
<dict>
    <key>name</key>
    <string>FALSE</string>
    <key>scope</key>
    <string>false.constant.language</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#890000</string>
    </dict>
</dict>

Restart ST3 (looks like ST3 do not completely update scopes list from changing .tmLanguage file during runtime, so better restart it) and select menu "Preferences" → "Color Scheme" → "User" → "Slush & Poppies". In this theme you will have true highlighted with green and false highlighted with red. Of course, you can edit any color scheme you want, not only this one.

One more thing, in .tmTheme file you can use not the whole scope name, but its part. For example, it's possible to use false.constant.language instead of false.constant.language.php. When you use false.constant.language, the settings will be applied to all scopes whose name is started from false.constant.language. For example, if you have defined false.constant.language.php in PHP.tmLanguage and false.constant.language.js in JavaScript.tmLanguage, you can use false.constant.language in .tmTheme file and its setting will be applied both to JS and PHP false.

like image 107
Ronin Avatar answered Sep 30 '22 05:09

Ronin