-
Notifications
You must be signed in to change notification settings - Fork 73
VS Code
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.
- Select the VS Code Preferences menu and select Extensions (typically
Ctrl Shift X
orCmd Shift X
as a shortcut) Search for Sourcery in the extension marketplace and click Install
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:
- Select the VS Code Preferences menu and select Extensions (typically
Ctrl Shift X
orCmd Shift X
as a shortcut) Search for Sourcery, click onto the settings icon, and select Extension Settings - Paste in the token
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 .
orCmd .
) 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.
To accept a suggestion you can:
- Press the quickfix shortcut (typically
Ctrl .
orCmd .
) 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.
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
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 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).
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.
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:
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
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 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.
Please visit our newer docs at https://docs.sourcery.ai