-
Notifications
You must be signed in to change notification settings - Fork 73
Emacs
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 lsp-mode LSP client for Emacs.
- 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.
- Add the following code to your emacs config:
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection '("<Command to run Sourcery>" "lsp"))
:initialization-options '((token . "<your_token_here>")
(extension_version . "emacs-lsp")
(editor_version . "emacs"))
:activation-fn (lsp-activate-on "python")
:server-id 'sourcery
:add-on? t
:priority 2))
Sourcery scans the currently open Python file. If lsp-modeline-diagnostics-mode
is enabled it highlights the relevant lines of code with improvements.
When on a suggestion line, you can hover to see the suggestion, and run the code action to refactor that code.
When on a suggestion line, you can hover to see the suggestion, and run the code action to refactor that code.
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
```
When on a suggestion line, you can hover to see it, and run the code action to skip it if you don't like it.
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