Skip to content

Sublime

Hellebore edited this page Apr 17, 2021 · 2 revisions

Installation

New Users - Creating Your Sourcery Account

If this is your first time using Sourcery you'll need to set up a Sourcery account. You can create an account with your GitHub account, Google account, or your email address.

Installing Sourcery for Sublime

  1. Install the sourcery-cli pypi package pip install --user sourcery-cli. Note down the full command path with
    which sourcery.
  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
        }
      }
    }
  }
}

Getting Started

Seeing your first refactoring suggestion

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.

How To

Accept a Suggestion

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

Disable Sourcery on a File or Path

We know there might be files or paths you don't want Sourcery to refactor - especially things like third party libraries. If you want Sourcery to ignore these you can add a file or a path to the ignore section in the .sourcery.yaml file in your project directory like this:

    ```
    ignore:
      - data/*
      - .venv/*
      - '*_test.py'  # Note that any strings beginning with * must be quoted
    ```

See 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 configure this](Sourcery Configuration).

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 configure Sourcery to never suggest a particular refactoring. For details see the Sourcery Configuration page.

Advanced Usage

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

Method Extraction

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.