Sublime Forum

Change Some Comments' Color

#1

I want to make certain comments (e.g. network connection subtleties) RED so that they stand out to the code reader/editor.
But I see no way to override the preset comment color for individual comments or parts of comments.
Another post suggested a package called HighlightWords but this is not recognized by Package Control indexer.

0 Likes

#2

In Sublime, the syntax definition is responsible for recognizing what the different parts of a file mean and applying a scope to them to classify them, and the color scheme is a set of rules that say what colors and styles should be applied to each scope.

So generally speaking you can make comments in different types of files different colors, but you can’t make certain parts of some comments visually distinct unless the syntax definition recognizes that those comments are special and applies unique scopes to their parts so that you can target a rule at them in the color scheme.

What syntax definition are you using for these particular files? Depending on the syntax and how complex it is, it’s possible that it could be modified to contain extra rules that match what you want to highlight.

It seems to work OK for me here; I assume it doesn’t appear in the list when you say Install Package? There may have been a transient network glitch that’s resolved now.

Also sometimes this is caused by the package already being installed, or being installed but in the list of ignored_packages in your settings; Package Control won’t list a package if it thinks it’s already installed.

1 Like

#3

@OdatNurd

Thanks for quick and useful response.
I must have made an error searching for HighlightWords first time out - it worked just now.
The settings for coloration were customized - just did the important word (in my situation, the word “ALWAYS” in a comment) in orange and dumped the rest of the colors.

Now, what’s all this about customizable syntax definitions ?
I’d love to try it but I’m not able to readily find these settings. :thinking:

0 Likes

#4

Going this route offers the most flexibility, but it’s also somewhat more complicated to do as well. In the documentation down in the Package Development section there are links to how to create syntax definitions and how color schemes work. However I’m not aware of any tutorials that cover this in any great detail, which complicates things if you’re new to it.

Essentially a syntax is just a structured set of regular expressions that match parts of a file and assign one or more scopes to them, to designate what those bits of text mean. The scopes can be anything you like (the documentation includes a scope naming guide) and rules in the color schemes match scopes to styles.

As a (sorta-kinda) simple example, the CSS.sublime-syntax file that provides the syntax definition for CSS (you can look at it by using View Package File from the command palette and looking for that file) contains a rule that looks like this:

    - match: /\*
      scope: punctuation.definition.comment.css
      push:
        - meta_scope: comment.block.css
        - match: \*/
          scope: punctuation.definition.comment.css
          pop: true

The rule says that when the syntax sees /* in a file, that bit of text gets the scope punctuation.definition.comment.cs, then that text and everything up to and including a */ character is scoped as comment.block.css. Also the */ itself is scoped the same as the /* is.

The Monokai color scheme that ships with Sublime (also viewable as above if you look for Monokai.sublime-color-scheme) contains a rule that looks like this:

{
    "name": "Comment",
    "scope": "comment",
    "foreground": "var(yellow5)"
},

The rule says that anything that matches the scope comment (which in this example is all of the text because the scopes start with comment) get assigned a specific color, which in this case ends up being (paradoxically based on the name) a gray-ish color.

Since the /* and */ parts have a unique scope applied, you could add a rule that matches punctuation.definition.comment that styles those parts of the comment differently (bolder, for example). Also, the full scope of the text also includes the type of the file, so if you wanted comments in one file type to appear in a different style over another, you can do that as well.

However it all comes down to the rules in the syntax making sure that the parts you want to style can be uniquely targeted with a rule, and creating a syntax (as you can tell if you look at a few of the files that ship with Sublime) is not easy when you’re new to it.

There’s a lot of talented people on the forum though, so we can provide guidance and help clarify things.

1 Like

#5

Okay.

I found that file CSS.sublime-syntax but it doesn’t allow edits.
I’ll read up on it and report later.

0 Likes

#6

When you use View Package File to view a package resource, the buffer will be read-only if the source of the file is a sublime-package file (this generally applies to most files except for ones from your User package, though a few packages are installed not as a sublime-package file).

To edit one you need to create an override of the package file to tell Sublime to use a different version.

The OverrideAudit package (disclaimer: I’m the author) contains commands to create overrides and this page from the documentation has some explanatory text on what overrides are and how they work.

0 Likes