Skip to content

Sublime

Installing Sourcery

  1. Install the sourcery-cli pypi package with pip install sourcery-cli. Note down the application path e.g. /path/to/sourcery or C:\path\to\sourcery.exe
  2. Open the command palette and run Package Control: Install Package, then select LSP.
  3. 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": {
            "token": "<Your token here>",  // Update this
            "extension_version": "sublime lsp",
            "editor_version": "sublime"
          },
          "settings": {
            "sourcery": {
              "metricsEnabled": true
            }
          }
        }
      }
    }
    
  4. Go to your Sourcery Dashboard, copy your token, and replace the <Your token here> section with your token.

Getting Started

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

How To

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 all refactorings or a specific type of refactoring for a function.

  • Add a comment # sourcery skip to a function to skip all reactorings for that function
  • Add a comment # sourcery skip: <refactoring-id> to a function to skip the specific refactoring in that function. A full list of refactorings and their IDs are available at Current Refactorings.

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.

Advanced Features

There are more advanced ways you can improve your code with a Sourcery Pro or Sourcery Team subscription:

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.

Back to top