Skip to content
Hellebore edited this page Apr 17, 2021 · 1 revision

Installation

New Users - Creating Your Sourcery Account

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.

Installing Sourcery for Emacs

  1. Install the lsp-mode LSP client for Emacs.
  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. 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))

Getting Started

Seeing your first refactoring suggestion

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.

How To

Accept a Suggestion

When on a suggestion line, you can hover to see the suggestion, and run the code action to refactor that code.

Disable Sourcery on a File or Path

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
    ```

Skip a Suggestion

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.

Advanced Usage

There are more advanced ways you can improve your code with a Sourcery Pro or Sourcery Team subscription:

Method Extraction

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.

Clone this wiki locally