Skip to content

Getting Started with Sourcery for Vim

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

  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. Install the coc.nvim LSP client for Vim/Neovim.

  4. In Vim, run :CocConfig and this to your settings:

{
  "languageserver": {
    "sourcery": {
      "command": "<Command to run Sourcery>",
      "args": [
        "lsp"
      ],
      "filetypes": [
        "python"
      ],
      "initializationOptions": {
        "extension_version": "coc.vim",
        "editor_version": "vim"
      },
      "settings": {
        "sourcery": {
          "metricsEnabled": true
        }
      }
    }
  }
}
  1. Optionally you can set up key bindings:
nnoremap <silent> <leader>cl :CocDiagnostics<cr>
nnoremap <silent> <leader>ch :call CocAction('doHover')<cr>
nnoremap <silent> <leader>cf <plug>(coc-codeaction-cursor)
nnoremap <silent> <leader>ca <plug>(coc-fix-current)

nmap <silent> [c <plug>(coc-diagnostic-prev)
nmap <silent> ]c <plug>(coc-diagnostic-next)

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 call CocActionAsync('codeAction', 'cursor') 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

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 call CocActionAsync('codeAction', 'cursor') 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 call CocActionAsync('codeAction', 'cursor') 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

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.

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?