Skip to content

Vim

Installing Sourcery

  1. Install the coc.nvim LSP client for Vim/Neovim.
  2. 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 which sourcery.
  3. In Vim, run :CocConfig and this to your settings:
    {
        "languageserver": {
            "sourcery": {
                "command": "<Command to run Sourcery>",
                "args": [
                    "lsp"
                ],
                "filetypes": [
                    "python"
                ],
                "initializationOptions": {
                    "token": "<Your Token Here>",
                    "extension_version": "coc.vim",
                    "editor_version": "vim"
                },
                "settings": {
                    "sourcery": {
                        "metricsEnabled": true
                    }
                }
            }
        }
    }
    
  4. Go to your Sourcery Dashboard, copy your token, and replace the <Your token here> section with your token.
  5. Optionally you can set up key bindings:
    nnoremap <leader>cl :CocDiagnostics<cr>
    nnoremap <leader>cf :CocFix<cr>
    nnoremap <leader>ch :call CocAction('doHover')<cr>
    

Getting Started

Seeing Your First Suggestions

Any suggestion from Sourcery will be highlighted/underlined. Hover your mouse over the underlined/highlighted section to see a description of the proposed change and a diff of the changes. There are three wys you can interact with a Sourcery suggestion

To show all the Sourcery suggestions from the scanned file run :CocDiagnostics and they will appear in the location list.

When on a suggestion line, you can run :call CocAction('doHover') to view a description of the refactoring along with a diff. Then you can run :CocFix and select the option to accept the change.

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

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 a suggestion you can run :call CocAction('doHover') to view a description of the refactoring along with a diff. Then you can run :CocFix and select the option to accept the change.

Skip a Suggestion

To skip a suggestion you can run :call CocAction('doHover') to view a description of the refactoring along with a diff. Then you can run :CocFix 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

Run :call CocAction('doHover') on a function's definition to get an instant view of its code quality,

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