-
Notifications
You must be signed in to change notification settings - Fork 72
Vim
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.
- Install the coc.nvim LSP client for Vim/Neovim.
- Install the sourcery-cli pypi package
pip install --user sourcery-cli
. Note down the full command path with
which sourcery
. - Sign up for Sourcery on the website and get your token.
- 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
}
}
}
}
}
- Optionally you can set up key bindings:
nnoremap <leader>cl :CocDiagnostics<cr>
nnoremap <leader>cf :CocFix<cr>
nnoremap <leader>ch :call CocAction('doHover')<cr>
Sourcery will run in the background whenever you're working on a 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
and select the option to accept the change.
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.
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
```
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 configure this](Sourcery Configuration).
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 configure Sourcery to never suggest a particular refactoring. For details see the Sourcery Configuration page.
There are more advanced ways you can improve your code with a Sourcery Pro or Sourcery Team subscription:
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.
Please visit our newer docs at https://docs.sourcery.ai