Search code examples
idesublimetext3sublime-anaconda

Linting customization in Anaconda Package Sublime


I am using the Anaconda package for Sublime 3, and it works perfectly. My only problem is that linting highlights all kind of errors, warnings and PEP8 recommendations in the same color, white. I would like to change it so errors are red, warnings are yellow etc... But I can't figure out how to do that. I found this resource that basically tells me to add some XML code to my current theme to change the linting color, but I tried doing that, and it did nothing.

First of all I believe now themes are using json and not XML anymore, in filenames of the type ".sublime-color-scheme" or ".sublime-theme". Thus I found a custom theme that indeed had a ".tmtheme" file, and pasted the XML code given here, but linting was still completely white.

Then I tried translating this XML code into json, and pasting it into the ".sublime-color-scheme" file, but then again, linting was white. To be precise, I pasted it in the "rules" :[] entry, since that seemed to fit the format.

Anybody with more experience could tell me what I'm doing wrong, or point me to another way of changing linting color ? I am aware of package such as SublimeLinter, but I'd rather stick with the built-in linter of the Anaconda package since its working perfectly beside the coloring.


Solution

  • As you've noted, there are two different types of color schemes in Sublime Text:

    1. The tmTheme format, which is an XML plist file. This format is still supported, but is considered to be a legacy file format at this point.

    2. The sublime-color-scheme format, which is a JSON file. This format can do everything that the tmTheme format can do, and has extra capabilities that tmTheme files don't.

    NOTE: Despite the name, tmTheme files are color schemes, not Themes. The format is unfortunately named, but in Sublime Text a Theme is an entirely different thing that styles the application look as a whole.

    This distinction isn't particularly important here, but it can catch you unaware if you go searching for information because you sometimes end up finding information that doesn't seem to apply to anything.

    It's hard to say exactly what you might have done wrong since you didn't include the content that you tried adding to the files. Generally I would expect that anything you added in there would either work or cause an error of some sort (either a popup or in the Sublime console), barring any configuration issues with the package itself.

    As an added note, I don't use this package (or linters in general), so for test purposes the Anaconda settings I tested with are stock except for the following two settings:

        // The default is "basic", but that seemed too generic for testing
        "anaconda_gutter_theme": "bright",
    
        // I don't use linters, but I happened to have pylint already installed
        // so this was the path of least resistance.
        "use_pylint": true,
    

    How you would go about adding the extra rules to your color scheme depends first on where your color scheme lives. In particular, if you have created your own custom color scheme that's stored in your User package, the steps are different than if you're using a color scheme that's being provided by someone else (Sublime Text or some package that you installed).

    Since most people tend to use pre-existing color schemes that they obtained from somewhere else, we'll cover that first.

    The color rules that are given on the page you linked are in the tmTheme XML format, and look like this:

    <!-- Anaconda -->
    <dict>
      <key>name</key>
      <string>anaconda Error Outline</string>
      <key>scope</key>
      <string>anaconda.outline.illegal</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#FF4A52</string>
          <key>foreground</key>
          <string>#FFFFFF</string>
      </dict>
    </dict>
    <dict>
      <key>name</key>
      <string>anaconda Error Underline</string>
      <key>scope</key>
      <string>anaconda.underline.illegal</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#FF0000</string>
      </dict>
    </dict>
    <dict>
      <key>name</key>
      <string>anaconda Warning Outline</string>
      <key>scope</key>
      <string>anaconda.outline.warning</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#DF9400</string>
          <key>foreground</key>
          <string>#FFFFFF</string>
      </dict>
    </dict>
    <dict>
      <key>name</key>
      <string>anaconda Warning Underline</string>
      <key>scope</key>
      <string>anaconda.underline.warning</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#FF0000</string>
      </dict>
    </dict>
    <dict>
      <key>name</key>
      <string>anaconda Violation Outline</string>
      <key>scope</key>
      <string>anaconda.outline.violation</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#ffffff33</string>
          <key>foreground</key>
          <string>#FFFFFF</string>
      </dict>
    </dict>
    <dict>
      <key>name</key>
      <string>anaconda Violation Underline</string>
      <key>scope</key>
      <string>anaconda.underline.violation</string>
      <key>settings</key>
      <dict>
          <key>background</key>
          <string>#FF0000</string>
      </dict>
    </dict>
    

    To add these rules, we need to first convert them into the sublime-color-scheme JSON format, even if your color scheme is a tmTheme color scheme. The method that you use to adjust a color scheme is to create a file in your User package with the additional content, and that file is always in the sublime-color-scheme format.

    The conversion of these rules to the new JSON format of a sublime-color-scheme looks like this:

    {
        "name": "anaconda Error Outline",
        "scope": "anaconda.outline.illegal",
        "foreground": "#FFFFFF",
        "background": "#FF4A52"
    },
    {
        "name": "anaconda Error Underline",
        "scope": "anaconda.underline.illegal",
        "background": "#FF0000"
    },
    {
        "name": "anaconda Warning Outline",
        "scope": "anaconda.outline.warning",
        "foreground": "#FFFFFF",
        "background": "#DF9400"
    },
    {
        "name": "anaconda Warning Underline",
        "scope": "anaconda.underline.warning",
        "background": "#FF0000"
    },
    {
        "name": "anaconda Violation Outline",
        "scope": "anaconda.outline.violation",
        "foreground": "#FFFFFF",
        "background": "#ffffff33"
    },
    {
        "name": "anaconda Violation Underline",
        "scope": "anaconda.underline.violation",
        "background": "#FF0000"
    },
    

    Depending on your actual color scheme, the colors in here may or may not be appropriate, so you'll have to play with them to get them looking as you want. This would also require knowing what classifies as illegal versus warning versus violation as well as how you've set up Anaconda's linting style. Your rules might need foreground, background or both depending on how you've set things up.

    With this content in hand, we're ready to begin. In order to make the adjustment, you need to know what the name of your color scheme is. You can get this by using Preferences > Settings and looking at the color_scheme setting (if you're using syntax specific settings, then open a file of that type and use Preferences > Settings - syntax specific and get the color scheme from there).

    The part that we're interested in here is the name of the file; we don't care what package it might say it lives in, only the name of the file itself. We also don't care what extension the file has because we're going to assume that the extension is sublime-color-scheme anyway.

    For example, with the setting set as the following, the name of the color scheme is Cobalt and that's all we care about.

        "color_scheme": "Packages/Color Scheme - Legacy/Cobalt.tmTheme",
    

    With this color scheme, the configuration above and some sample code, the results I see in the buffer are this:

    Cobalt Sample without adjustments

    In order to put the new rules in place, we need to create a sublime-color-scheme file in the User package named for our color scheme. So in this case that would be Cobalt.sublime-color-scheme. If you're unsure of where your User package lives, you can use Preferences > Browse Packages to find it.

    The content of the file that you create should look like this (paste the content from above as appropriate; for brevity I'm not including it again here):

    {
        "rules": [
            // Paste the JSON rules above here
        ]
    }
    

    As soon as you save the file, it will take effect. The result of that is this:

    Cobalt with new rules

    Presuming that you're using a color scheme that you created yourself that's in your User package, this won't work and you would instead need to add the rules directly to your color scheme file in your User package. This would also be the case if you have a "patch" file like this already in your User package (if you were adding extra color rules for somethign else, for example).

    If your custom color scheme is in sublime-color-scheme format, then you can just add these rules into the rules section of your color scheme just like above to have them take effect.

    If your color scheme is in tmTheme format, then instead you'd need to copy the XML version of the settings into your color scheme. Here how you'd do that is not entirely as straight forward as with the sublime-color-scheme formatted file due to the XML nature of the file.

    In this case you'd instead need to note that each rule is a <dict></dict> tag with specific keys, then examine the color scheme to see where it has similar color rules and inject yours into the correct location.

    Generally speaking if you get it wrong the color scheme will be ignored (everything will turn black and white) and you'll get an error dialog to tell you that something has gone wrong.

    More information on how color schemes work, how to apply colors, etc can be found in this video series on color schemes in Sublime Text (with the disclaimer that I'm the author of the videos in question).