Skip to content

VS Code

Installing Sourcery

  1. In VS Code, press Cmd+P, paste this command ext install sourcery.sourcery, and press Enter.
  2. Go to your Sourcery Dashboard, copy your token, and enter it into the Sourcery Token input in VS Code.

OR

  1. Go to the Preferences menu and select Extensions
  2. Search for Sourcery and click Install
  3. Go to your Sourcery Dashboard, copy your token, and enter it into the Sourcery Token input in VS Code.

Getting Started

Getting Your Token

Sourcery requires that you have a token to analyze your code and to give you refactoring suggestions. To get your token go to your Sourcery Dashboard) and copy the token.

Then return to VS Code and enter the token in the text box that prompts you on install.

If you do not see the Sourcery prompt to enter your token then open up Preferences > Settings > Extensions > Sourcery Configuration and enter the token in the input box.

Adding your Sourcery Token in VS Code

Seeing Your First Suggestions

When you first install Sourcery you should see a welcome file open in VS Code. This will include a few instructions for getting started and a demo function with a suggested refactoring.

Any suggestion from Sourcery will be highlighted/underlined. Hover your mouse over the underlined/highlighted section to see a description of the proposed change and a diff of the changes. There are three ways to interact with a Sourcer suggestion: - Select the highlighted line & open up the Quick Fix menu (usually Cmd . or Ctrl .) to bring up the menu of Sourcery actions to accept or skip the change. - 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.

Interacting with a suggestion in Sourcery

You can also try out Sourcery by copying this code into a Python file in VS Code:

def merge_nested_if(a, b):
    if a:
        if b:
            return c

Sourcery will suggest merging together the nested if statements so you get:

def merge_nested_if(a, b):
    if a and b:
        return c

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 accept the suggested change.

Skip a Suggestion

You can skip a Sourcery suggestion once by choosing the skip option from the Sourcery action items menu:

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

You can also tell Sourcery not to suggest all refactorings or a specific type of refactoring for a function.

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

Skipping refactorings in VS Code

Permanently Ignore a Type of Refactoring

You can choose to never see a certain type of refactoring when it is suggested to you by Sourcery. Bring up the Quick Fix menu and select the third menu item Sourcery - Never show me this refactoring.

Sourcery will then add this refactoring to your list of excluded refactorings. You can reverse this by going to your project's sourcery.yaml file and removing the refactoring from the skip list.

See Code Quality Metrics

Sourcery gives each 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.

Sourcery's Quality Metrics in VS Code

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 adjust this threshold.

Advanced Features

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

Detect Duplicate (and Near Duplicate) Code

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.

Sourcery finding duplicate code in a project

By default Sourcery will flag exact duplicates as well as near duplicates (sections of code that almost match, except for some altered parameters).

Extract Duplicate Code Into Methods

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.

Sourcery Extracting Duplicate Methods

Full Project or 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

Configuring Sourcery

See our section on Configuring Sourcery for details on customizing your Sourcery configuration.

Back to top