Skip to content

VS Code

Hellebore edited this page Apr 20, 2021 · 3 revisions

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 the Sourcery Plugin

VS Code Installation

  • Select the VS Code Preferences menu and select Extensions (typically Ctrl Shift X or Cmd Shift X as a shortcut) Search for Sourcery in the extension marketplace and click Install

Getting Started

Adding the Sourcery Token

Sourcery needs a token to run in VS Code. To get your token, just log into your Sourcery account and copy the token on the page.

Then:

Enter token

  • Select the VS Code Preferences menu and select Extensions (typically Ctrl Shift X or Cmd Shift X as a shortcut) Search for Sourcery, click onto the settings icon, and select Extension Settings
  • Paste in the token

Seeing your first refactoring suggestion

Sourcery will run in the background whenever you're working on a Python file. When Sourcery detects a refactoring opportunity it will underline the first line of code it recommends that you change. Hover over the underlined line to get an explanation of the change along with a diff of the proposed changes to your code.

To accept a suggestion you can:

  • Press the quickfix shortcut (typically Ctrl . or Cmd .) when you have selected the underlined line and then choose to apply the refactoring
  • Click on the lightbulb icon next to the underlined line and then select to accept the refactoring
  • Open the Problems pane, right click on the suggested refactoring, and select the suggested change.

Accept refactoring

How To

Accept a Suggestion

To accept a suggestion you can:

  • Press the quickfix shortcut (typically Ctrl . or Cmd .) when you have selected the underlined line and then choose to apply the refactoring
  • Click on the lightbulb icon next to the underlined line and then select to accept the refactoring
  • Open the Problems pane, right click on the suggested refactoring, and select the suggested change.

Accept refactoring

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

See Quality Metrics

Sourcery gives every one of your functions a quality score on 4 different metrics:

  • Complexity
  • Method Length
  • Working Memory
  • Overall Quality

To see the metrics for any function, simply hover your mouse of the line defining the function and the Sourcery metrics will pop up. Each metric will have a numeric score, along with a quick qualitative guide ranging from bad to excellent.

Quality metrics

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).

Skip a Suggestion

Sometimes you might see a Sourcery suggestion you don't want to accept. You can ignore these suggestions in one of three ways:

  • Add a comment # sourcery skip to a function to skip all reactorings for that function
  • Add a comment # sourcery skip: <refactoring-id> to a function to skip the specific refactoring in that function. A full list of refactorings and their IDs are available at Current Refactorings.
  • Select Skip Suggested Refactoring from the drop down menu when you press the quickfix shortcut or click on the lightbulb icon.

Skip a suggestion

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:

Full Project / Multi-File Code Review

You can have Sourcery review multiple files, a folder, or an entire project at once:

  • Right click on the folder or file you're interested in.
  • Hover over the Sourcery menu item and select "Scan with Sourcery"
  • Suggestions will appear in the Problems pane

Full project scan

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.

Method extraction

Duplicate Code Detection (Beta)

Sourcery can help you find duplicate sections of code across your project.

  • Right click on files or folders in the Explorer window
  • Hover over the Sourcery menu item and select "Detect clones"
  • A list of duplicate and near duplicate sections of code will appear in the Problems pane.

Detect clones