# Getting Started with Sourcery for Sublime¶

## Installation¶

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
}
}
}
}
}

1. Go to your Sourcery Dashboard, copy your token, and replace the <Your token here> section with your token.

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 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.