Skip to content

Vim Usage

Brendan Maginnis edited this page Nov 9, 2020 · 7 revisions

Installation

  1. Install the coc.nvim LSP client for Vim/Neovim.
  2. Install the sourcery-cli pypi package pip install --user sourcery-cli. Note down the full command path with
    which sourcery.
  3. Sign up for Sourcery on the website and get your token.
  4. 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
                }
            }
        }
    }
}
  1. Optionally you can set up key bindings:
nnoremap <leader>cl :CocDiagnostics<cr>
nnoremap <leader>cf :CocFix<cr>
nnoremap <leader>ch :call CocAction('doHover')<cr>

How does it work?

Sourcery scans the currently open Python file. If it finds improvements in your code it highlights the relevant lines of code. 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 to either make the suggested change or to disable it.

The extension can also show you inline code metrics, giving you an instant view of your code quality. Run :call CocAction('doHover') on a function's definition to get an instant view of its code quality, and functions with very low quality will additionally be highlighted as warnings.

See the Sourcery Tutorial for more detailed info.

How do I configure Sourcery?

Please see the Sourcery configuration section for details.

Clone this wiki locally