Skip to content

Getting Started with Sourcery for Sublime

Installation

  1. Install the sourcery pypi package with pip install sourcery. Note down the application path e.g. /path/to/sourcery or C:\path\to\sourcery.exe
  2. Run sourcery login to login. When using Sourcery on an open source project you do not need to log in, but you'll need to for non-open source projects.
  3. Open the command palette and run Package Control: Install Package, then select LSP.
  4. Open Preferences > Package Settings > LSP > Settings from the menu and add the following settings:
    {
      "clients": {
        "sourcery": {
          "command": [
            "<Command to run Sourcery>",  // Update this
            "lsp"
          ],
          "enabled": true,
          "languageId": "python",
          "scopes": ["source.python"],
          "syntaxes": [
            "Packages/Python/Python.sublime-syntax",
            "Packages/MagicPython/grammars/MagicPython.tmLanguage",
            "Packages/Djaneiro/Syntaxes/Python Django.tmLanguage"
          ],
          "initializationOptions": {
            "extension_version": "sublime lsp",
            "editor_version": "sublime"
          },
          "settings": {
            "sourcery": {
              "metricsEnabled": true
            }
          }
        }
      }
    }
    

Seeing Your First Suggestions

Sourcery will run in the background whenever you're working on a Python file. To show all Sourcery suggestions run LSP: Toggle Diagnostics Panel from the command palette. Use F4/shift+F4 to jump to next/previous suggestion.

Hover over the underlined line to get an explanation of the change along with a diff of the proposed changes to your code. To accept the suggestion press super+. and select the option to accept the change.

You can also try out Sourcery by copying this code into a Python file in Sublime:

def merge_nested_if(a, b):
    if a:
        if b:
            return c

Sourcery will suggest merging together the nested if statements so you get:

def merge_nested_if(a, b):
    if a and b:
        return c

Accept a Suggestion

To accept the suggestion press super+. and select the option to accept the change.

Skip a Suggestion

Sometimes you might see a Sourcery suggestion you don't want to accept. You can ignore these suggestions by pressing super+. and select the option to skip the change.

You can also tell Sourcery not to suggest anything or a specific type of rule for a function.

  • Add a comment # sourcery skip to a function to skip all rules for that function

  • Add a comment # sourcery skip: <rule-id> to a function to skip the specific rule in that function. A full list of rules and their IDs are available at:

  • Python Rules.

  • JavaScript Rules.

See Code Quality Metrics

Sourcery gives every one of your functions a quality score on 4 different metrics:

  • Complexity
  • Method Length
  • Working Memory
  • Overall Quality

To see the metrics for any function, simply hover your mouse of the line defining the function and the Sourcery metrics will pop up. Each metric will have a numeric score, along with a quick qualitative guide ranging from bad to excellent.

Sourcery will also automatically flag functions with too low of an overall quality score. By default this is set for functions with a quality score under 25%, but you can adjust this threshold.

Extract Duplicate Code Into Methods

Sourcery will automatically detect opportunities for repeated or nearly repeated sections of code within a function to be extracted out into their own methods. When these refactorings are suggested, the new methods will be given a generic name based on the function it was extracted from and you can easily rename it.

Configuring Sourcery

See our section on Configuring Sourcery for details on customizing your Sourcery configuration.

Was this page helpful?