Getting Started with Sourcery for JetBrains - Intellij, PyCharm and more¶
Installation¶
- In your JetBrains IDE (Intellij, PyCharm, ...), press
Ctrl+Alt+S
orCmd+,
to open the Preferences dialog. Select Plugins. - Search for Sourcery in the Marketplace tab and click Install.
- Click the Restart IDE button.
- Click the login button in the top left of the Sourcery window. When using Sourcery on an open source project you do not need to log in, but you'll need to for non-open source projects.
Seeing Your First Suggestions¶
When you first install Sourcery you should see a welcome file open in your JetBrains IDE. 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 wys you can interact with a Sourcery suggestion
- Press
Alt Enter
when you have selected the highlighted line and then choose to apply the refactoring - Click on the lightbulb icon next to the highlighted line and then select to accept the refactoring
- In the Sourcery tool window, right click on the suggested refactoring, and select Refactor Function
You can also try out Sourcery by copying this code into a Python file in PyCharm or Intellij Ultimate:
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
Accept a Suggestion¶
To accept a suggestion you can:
- Press
Alt Enter
when you have selected the highlighted line and then choose to apply the refactoring - Click on the lightbulb icon next to the highlighted line and then select to accept the refactoring
- In the Sourcery tool window, right click on the suggested refactoring, and select Refactor Function
Sourcery Hub¶
The Sourcery Hub allows you to quickly configure Sourcery. To open it, find the
section on the status bar which says Sourcery
and click on it. The hub should
then open in your default browser.
The Project Config
screen allows you to switch Sourcery rules on and off. When
you do so it will edit or create a .sourcery.yaml
file in your project. This
is the configuration file used by Sourcery.
You can also go to the Your Plan
section to get information about your
Sourcery account, and the benefits of upgrading to Pro
and Team
accounts.
Skip a Suggestion¶
You can skip a Sourcery suggestion once by choosing the skip option from the Sourcery action items menu:
- Press
Alt Enter
when you have selected the highlighted line and then choose to skip the refactoring - Click on the lightbulb icon next to the highlighted line and then select to skip the refactoring
- In the Sourcery tool window, right click on the suggested refactoring, and select Skip
You can also tell Sourcery not to suggest anything or a specific type of rule for a function.
-
Add a comment
# sourcery skip
to a function to skip all rules for that function -
Add a comment
# sourcery skip: <rule-id>
to a function to skip the specific rule in that function. A full list of rules and their IDs are available at: - JavaScript Rules.
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 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.
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
- Select "Sourcery - Detect clones"
- A list of duplicate and near duplicate sections of code will appear in the Sourcery tool window.
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.
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.
- Select "Inspect Code"
- Click on the three dots next to the "Inspection Profile"
- Make sure Sourcery is selected and click "Ok"
- Suggestions from Sourcery will appear in the inspection results
Configuring Sourcery¶
See our section on Configuring Sourcery for details on customizing your Sourcery configuration.