diff --git a/README.md b/README.md index b2a73a4a..361fe63e 100644 --- a/README.md +++ b/README.md @@ -21,17 +21,16 @@ Sourcery is the pair programmer who will help you improve your code. It revie ## Sourcery -Sourcery is a VS Code extension to help make all of your code cleaner and more readable. Quickly find areas **where your code could be refactored**, see **instant suggestions for improvement**, and understand how new changes impact your **code quality**. +Sourcery is a VS Code extension to help you move faster and ship code with confidence. Sourcery gives you code reviews anytime you want, so you can get feedback before you need to open a PR. Plus Sourcery's coding assistant helps you speed up repetitive tasks while you work. Here are some of the features Sourcery offers to help improve your code: +- [Code reviews anytime, in IDE](#code-reviews-on-demand) +- [GitHub & GitLab Pull Request reviews](#github-pull-request-review) - [Real-time refactoring suggestions](#real-time-refactoring-suggestions) -- [Set up your own rules - or use public rulesets](#set-up-your-own-rules---or-use-pulic-rulesets) +- [Set up your own rules - or use public rulesets](#set-up-your-own-rules---or-use-public-rulesets) - [Continuous code quality feedback](#continuous-code-quality-feedback) -- [Multi-file analysis](#multi-file-analysis) -- [Duplicate code detection](#duplicate-code-detection) -- [Sourcery CLI, CI, & Pre-Commit Hook options](#sourcery-cli) -- [GitHub Pull Request reviews](#github-pull-request-review) + To start using Sourcery on your code, check out our [Getting Started guide](https://docs.sourcery.ai/getting-started/). @@ -46,6 +45,22 @@ To use Sourcery on non open sourced projects you'll need a Sourcery Pro or Sourc ## Features +### Code reviews on demand + +![Sourcery Code Reviews](https://raw.githubusercontent.com/sourcery-ai/sourcery-vscode/main/assets/code_review.png) + +Get feedback on your code anytime you want. Sourcery can review your code directly in your IDE, looking at the branch you're currently on or the uncommitted changes you have. + + + +### GitHub & GitLab Pull Request Review + +![Sourcery Reviewing GitHub PRs](https://raw.githubusercontent.com/sourcery-ai/sourcery-vscode/main/assets/Sourcery_Code_Review.gif) + +Sourcery can help you speed up code reviews and clean up every new commit by automatically reviewing each of your GitHub pull requests. + +To get started, add [Sourcery to your GitHub repo](https://github.com/apps/sourcery-ai/installations/new) or [GitLab project](https://app.sourcery.ai/login?connection=gitlab). Sourcery will then start reviewing every new pull request automatically! + ### Real-time refactoring suggestions ![Refactoring Code with Sourcery](https://raw.githubusercontent.com/sourcery-ai/sourcery-vscode/main/assets/Sourcery_VS_Code_Refactoring.gif) @@ -112,34 +127,6 @@ Right-click on any item in the Explorer window and select "Sourcery → Detect C By default, Sourcery will flag items where at least 3 lines are duplicates or near-duplicates that occur at least twice in the scanned files. -### Sourcery CLI - -If you want to clean up a bigger portion of legacy code, Sourcery CLI comes in handy. - -- Get started with `pip install sourcery-cli` -- With the `sourcery review` command, you can scan multiple files or directories for refactorings. -- With the `-in-place` option, you can apply the suggested refactorings immediately. - -![Sourcery CLI](https://raw.githubusercontent.com/sourcery-ai/sourcery-vscode/main/assets/sourcery-cli.gif) - -With Sourcery CLI, you can also integrate Sourcery with your favorite tools. - -#### CI / Pre-Commit Hook options: - -You can use Sourcery to review every new bit of code you and your team are working on by adding Sourcery into your CI or running it as a Pre-Commit Hook. - -Sourcery will review every new change and you can set it up to either automatically make changes to your code when it detects opportunities to refactor, or you can just have it notify you when it finds refactorings. - -Sourcery is fully free for Open Source projects, the CI and Pre-Commit Hook options require a Sourcery Team subscription for private projects. **[You can sign up for a Team plan here](https://sourcery.ai/team/)**. - -### GitHub Pull Request Review - -![Sourcery Reviewing GitHub PRs](https://raw.githubusercontent.com/sourcery-ai/sourcery-vscode/main/assets/Sourcery_GitHub-Refactor-Branch.gif) - -Sourcery can help you speed up code reviews and clean up every new commit by automatically reviewing each of your GitHub pull requests. - -To get started, add [Sourcery to your GitHub repo](https://github.com/apps/sourcery-ai/installations/new). Sourcery will then start reviewing every new pull request automatically! - --- ## Configuring Sourcery @@ -310,10 +297,6 @@ def __getstate__(self): Copy these examples into a Python file to see how Sourcery would handle them. -## Privacy / Security - -All of the analysis we do on your code is done fully locally. We never see any of your code or pass it back to our servers in any way. The only thing we collect is some basic analytics about the types of suggestions we make, and information about exceptions in Sourcery. - --- ## About us diff --git a/assets/Sourcery_Code_Review.gif b/assets/Sourcery_Code_Review.gif new file mode 100644 index 00000000..54e0df5f Binary files /dev/null and b/assets/Sourcery_Code_Review.gif differ diff --git a/assets/code_review.png b/assets/code_review.png new file mode 100644 index 00000000..0ec5402e Binary files /dev/null and b/assets/code_review.png differ diff --git a/package.json b/package.json index 6d91c7f9..7ecfa33d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sourcery", "displayName": "Sourcery", - "description": "Refactor Python instantly with Sourcery", + "description": "Instant Code Reviews in your IDE", "author": "Sourcery AI", "repository": "https://github.com/sourcery-ai/sourcery-vscode", "bugs": { @@ -14,6 +14,8 @@ "keywords": [ "ai", "copilot", + "code review", + "code quality", "tabnine", "python", "javascript", @@ -23,12 +25,14 @@ "node", "refactor", "refactoring", + "review", "typescript" ], "engines": { "vscode": "^1.76.0" }, "categories": [ + "AI", "Linters", "Machine Learning", "Other", @@ -104,23 +108,20 @@ ] }, { - "id": "sourcery.codeReviews", - "title": "Review Your PRs with Sourcery", - "description": "Add Sourcery to your GitHub repos to get instant, in depth code reviews on every pull request.\n[Add to GitHub](https://github.com/apps/sourcery-ai/installations/new)\n", + "id": "sourcery.acceptRecommendation", + "title": "Get Feedback on Your Current Changes", + "description": "Get an instant code review of your current branch or your uncommitted changes to get the type of feedback you expect from a peer review in seconds.", "media": { "markdown": "walkthrough/code_reviews.md" } - }, + }, { - "id": "sourcery.acceptRecommendation", - "title": "Use Sourcery's In-Line Suggestions", - "description": "Sourcery suggests ways to improve your code quality while you work. Put your cursor on an underline, then click the lightbulb or use the quick fix hotkey to show the code actions.\n To see this in action scroll down to the `refactoring_example` function and take the `Sourcery - Convert for loop into list comprehension` option to accept the change.\n", + "id": "sourcery.githubCodeReviews", + "title": "Review Your PRs with Sourcery", + "description": "Add Sourcery to your GitHub repos to get instant, in depth code reviews on every pull request.\n[Add to GitHub](https://github.com/apps/sourcery-ai/installations/new)\n", "media": { - "markdown": "walkthrough/accept_recommendation.md" - }, - "completionEvents": [ - "onContext:acceptRecommendationContextKey" - ] + "markdown": "walkthrough/github_code_reviews.md" + } }, { "id": "sourcery.configuration", @@ -233,6 +234,11 @@ "command": "sourcery.effects.disable", "title": "Hide Effects", "category": "Sourcery" + }, + { + "command": "sourcery.review", + "title": "Review", + "category": "Sourcery" } ], "submenus": [ diff --git a/walkthrough/NotImplemented-Comment.png b/walkthrough/NotImplemented-Comment.png deleted file mode 100644 index c33e8c52..00000000 Binary files a/walkthrough/NotImplemented-Comment.png and /dev/null differ diff --git a/walkthrough/NotImplemented-Fixed.png b/walkthrough/NotImplemented-Fixed.png deleted file mode 100644 index 569d5ff3..00000000 Binary files a/walkthrough/NotImplemented-Fixed.png and /dev/null differ diff --git a/walkthrough/NotImplemented-Quickfix.png b/walkthrough/NotImplemented-Quickfix.png deleted file mode 100644 index d15ac0d1..00000000 Binary files a/walkthrough/NotImplemented-Quickfix.png and /dev/null differ diff --git a/walkthrough/NotImplemented-Suggestion.png b/walkthrough/NotImplemented-Suggestion.png deleted file mode 100644 index 8e8d9242..00000000 Binary files a/walkthrough/NotImplemented-Suggestion.png and /dev/null differ diff --git a/walkthrough/RemoveOpenR-Fixed.png b/walkthrough/RemoveOpenR-Fixed.png deleted file mode 100644 index 5ef6df85..00000000 Binary files a/walkthrough/RemoveOpenR-Fixed.png and /dev/null differ diff --git a/walkthrough/RemoveOpenR-Hover.png b/walkthrough/RemoveOpenR-Hover.png deleted file mode 100644 index 55da8ca0..00000000 Binary files a/walkthrough/RemoveOpenR-Hover.png and /dev/null differ diff --git a/walkthrough/Sourcery_Duplicates_VS_Code.gif b/walkthrough/Sourcery_Duplicates_VS_Code.gif deleted file mode 100644 index 80fe9ac9..00000000 Binary files a/walkthrough/Sourcery_Duplicates_VS_Code.gif and /dev/null differ diff --git a/walkthrough/Sourcery_Extract_Method_VS_Code.gif b/walkthrough/Sourcery_Extract_Method_VS_Code.gif deleted file mode 100644 index 9f739cd1..00000000 Binary files a/walkthrough/Sourcery_Extract_Method_VS_Code.gif and /dev/null differ diff --git a/walkthrough/Sourcery_Full_Project_Scan_VS_Code.gif b/walkthrough/Sourcery_Full_Project_Scan_VS_Code.gif deleted file mode 100644 index 746a78a0..00000000 Binary files a/walkthrough/Sourcery_Full_Project_Scan_VS_Code.gif and /dev/null differ diff --git a/walkthrough/Sourcery_Token_VS_Code.gif b/walkthrough/Sourcery_Token_VS_Code.gif deleted file mode 100644 index 255fa472..00000000 Binary files a/walkthrough/Sourcery_Token_VS_Code.gif and /dev/null differ diff --git a/walkthrough/code_review.png b/walkthrough/code_review.png new file mode 100644 index 00000000..0ec5402e Binary files /dev/null and b/walkthrough/code_review.png differ diff --git a/walkthrough/code_reviews.md b/walkthrough/code_reviews.md index 953eb77d..29d679e0 100644 --- a/walkthrough/code_reviews.md +++ b/walkthrough/code_reviews.md @@ -1,13 +1,12 @@ -You can add Sourcery to your GitHub or GitLab repos to get immediate code review feedback on every pull/merge request. +Sourcery can review your code directly in your IDE. -![Sourcery Code Reviews](Sourcery_Code_Review.gif) +![Sourcery Code Reviews](Sourcery_Code_Review.png) -## GitHub Code Reviews -You can [directly add the Sourcery GitHub integration](https://github.com/apps/sourcery-ai/installations/new ) to any repo. +Click the Sourcery logo in the sidebar to open the coding assistant and bring up the code review panel. +Choose the context of the code you want to review. -## GitLab Code Reviews -You can run Sourcery code reviews as part of your CI to get reviews on every GitLab MR. [Follow the instructions in our documentation](https://docs.sourcery.ai/Code-Review/Getting-Started/GitLab/) to set up Sourcery for GitLab. +You can choose to review your current branch or your uncommitted changes. + +Then click review and Sourcery will get to work, giving you feedback. -## Code Reviews for Other Platforms -Sourcery is not yet available for Bitbucket or other platforms, but you can [sign up for our waitlist](https://research.typeform.com/to/GoVM9jYf) to get early access when it's available. \ No newline at end of file diff --git a/walkthrough/custom_rules_tutorial.md b/walkthrough/custom_rules_tutorial.md deleted file mode 100644 index 0f6e6202..00000000 --- a/walkthrough/custom_rules_tutorial.md +++ /dev/null @@ -1,148 +0,0 @@ -# Writing your first custom rules - -By the end of this tutorial you will have written two custom Sourcery rules and -used them to review your code. - -## Introduction - -Have you ever seen the same code smell come up again and again in different code -reviews? Wouldn't it be great if you could write a rule to make sure that it -could never happen again? - -Sourcery can be extended with your own custom rules that allow you to do this. - -## Creating your first rule - -Open or create `.sourcery.yaml` in your IDE and paste your first rule: - -```yaml -rules: - - id: raise-not-implemented - description: NotImplemented is not an Exception, raise NotImplementedError instead - pattern: raise NotImplemented -``` - -Save this file, then open a new Python file and paste this problematic code: - -```python -class ExampleBaseClass: - def abstract_method(self): - raise NotImplemented -``` - -Sourcery highlights the line where this issue occurs. Hover your mouse on the -highlight to see the problem: - -![NotImplemented Comment](NotImplemented-Comment.png) - -## Including a replacement in your rule - -This is really nice, but what would be even nicer is to have Sourcery fix this -for you. Update your `.sourcery.yaml` rule by adding a `replacement` key: - -```yaml -rules: - - id: raise-not-implemented - description: NotImplemented is not an Exception, raise NotImplementedError instead - pattern: raise NotImplemented - replacement: raise NotImplementedError -``` - -Save this file, return to the example code and hover over the highlight: - -![NotImplemented Suggestion](NotImplemented-Suggestion.png) - -Not only is the issue highlighted but the fix is suggested too! - -Let's accept the suggestion: - -1. Move the cursor to the broken code, this will show the lightbulb 💡 -2. Click the lightbulb to show Sourcery options -3. Select the first option - -![NotImplemented Quickfix](NotImplemented-Quickfix.png) - -and Sourcery fixes our code: - -![NotImplemented Fixed](NotImplemented-Fixed.png) - -Congratulations, you've written your first rule! - -## Dissecting a rule - -Let's look closer at our rule: - -```yaml -rules: - - id: raise-not-implemented - description: NotImplemented is not an Exception, raise NotImplementedError instead - pattern: raise NotImplemented - replacement: raise NotImplementedError -``` - -Firstly, we can see that our rule lives inside a `rules` sequence. This can -contain multiple rules as we'll see later on. - -Each rule has the following keys: - -| Field | Type | Required | Description | -| ----------- | ------ | -------- | -------------------------------------------------------------- | -| id | string | Required | Unique, descriptive identifier, e.g. `raise-not-implemented` | -| description | string | Required | A description of why this rule triggered and how to resolve it | -| pattern | string | Required | Search for code matching this expression | -| replacement | string | Optional | Replace the matched code with this | - -## More powerful patterns using captures - -Let's take it up a level. Add a second rule to `.sourcery.yaml` - -```yaml -rules: - - id: raise-not-implemented - description: NotImplemented is not an Exception, raise NotImplementedError instead - pattern: raise NotImplemented - replacement: raise NotImplementedError - - - id: remove-open-r - description: Files are opened in read mode `r` by default - pattern: open(${file}, "r") - replacement: open(${file}) -``` - -The new feature here is that we are now using the capture `${file}` to match any -code that is used as the first argument to `open`. This captured code is then -used in the replacement. - -Paste this code into your example file to see this in action: - -```python -def print_sourcery_yaml(): - with open(".sourcery.yaml", "r") as f: - print(f.read()) -``` - -Now hover over the highlighted line: - -![Hover remove open r](RemoveOpenR-Hover.png) - -Here we can see that `${file}` has captured `"sourcery.yaml"` and used it in the -replacement. - -Finally use the quickfix lightbulb to fix your code. - -![Fixed remove open r](RemoveOpenR-Fixed.png) - -Beautiful! - -## Conclusion - -You've written two rules; one that matches exact code, and a second that -captures code to reuse it in the replacement. - -Now go forth and write your own rules. Enjoy! - -## Next Steps - -- Check out our - [rules documentation](https://docs.sourcery.ai/Configuration/Project-Settings#add-custom-rules) -- Run these rules on the [command line](https://docs.sourcery.ai/Command-Line-Interface/) diff --git a/walkthrough/github_code_reviews.md b/walkthrough/github_code_reviews.md new file mode 100644 index 00000000..7d29b65f --- /dev/null +++ b/walkthrough/github_code_reviews.md @@ -0,0 +1,10 @@ +You can add Sourcery to your GitHub or GitLab repos to get immediate code review feedback on every pull/merge request. + +![Sourcery Code Reviews](Sourcery_Code_Review.gif) + +## GitHub & GitLab Code Reviews +You can add Sourcery to any [GitHub](https://app.sourcery.ai/login?connection=github) or [GitLab](https://app.sourcery.ai/login?connection=gitlab) repo to start getting code reviews on every PR. + + +## Code Reviews for Other Platforms +Sourcery is not yet available for Bitbucket or other platforms, but you can [sign up for our waitlist](https://research.typeform.com/to/GoVM9jYf) to get early access when it's available. \ No newline at end of file diff --git a/walkthrough/refactoring_scan.md b/walkthrough/refactoring_scan.md deleted file mode 100644 index 3e4142b4..00000000 --- a/walkthrough/refactoring_scan.md +++ /dev/null @@ -1,53 +0,0 @@ -## Sourcery Pro Free Trial - -### 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](Sourcery_Full_Project_Scan_VS_Code.gif) - -### 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](Sourcery_Duplicates_VS_Code.gif) - -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](Sourcery_Extract_Method_VS_Code.gif) - - -### Sourcery in Private GitHub Repos - -For Sourcery Pro users, Sourcery is able to analyze and refactor all public and -private individual repos. - -See our section on [GitHub Usage](https://docs.sourcery.ai/GitHub/Using-Sourcery-for-GitHub.md) for -more details on how to use Sourcery for GitHub. - -A [Sourcery Team plan](https://docs.sourcery.ai/Sourcery-Team.md) is required to use Sourcery for -private repos for a GitHub organization. - -### Sourcery for Students - -Students learning Python are able to get 12 months of free access to Sourcery -Pro. Please email [students@sourcery.ai](mailto:students@sourcery.ai) and we'll -help you get set up. diff --git a/walkthrough/view_welcome_file.md b/walkthrough/view_welcome_file.md index ef9fbc7f..2c95248f 100644 --- a/walkthrough/view_welcome_file.md +++ b/walkthrough/view_welcome_file.md @@ -1,11 +1,21 @@ -## Using Sourcery as Your Pair Programmer +## Sourcery - Your Code Reviewer & Coding Assistant Sourcery works in 2 ways: -1. Gives you instant suggestions for improvements and refactorings to your Python, JavaScript, and TypeScript code. +1. Gives you code reviews while you work. Anytime you want a review just ask Sourcery and it will give you feedback like you'd expect from a peer review. 2. Acts as an AI powered pair programmer allowing you to ask it questions, write new code, and interact with existing code Let's take a look at how you can interact with it in VS Code +### Code reviews + +Sourcery can review your code directly in your IDE anytime you'd like some feedback. + +Click the Sourcery logo in the sidebar to open the coding assistant and bring up the code review panel. + +Then choose the context of the code you want to review, click review, and wait for Sourcery's feedback. + +To make the most of Sourcery's reviews you can also add Sourcery to your GitHub or GitLab repos. + ### In the tutorial Once you open the tutorial you should see this example: diff --git a/welcome-to-sourcery.py b/welcome-to-sourcery.py index 99b6751d..2b8f4099 100644 --- a/welcome-to-sourcery.py +++ b/welcome-to-sourcery.py @@ -5,25 +5,30 @@ # (the hexagon) on your VS Code sidebar and click the login button, or open # the command palette (Ctrl/Cmd+Shift+P) and execute `Sourcery: Login`. -# Let's start with the coding assistant - to get started, opt in via the Sourcery sidebar. +# Let's start looking at how you can get a code review from Sourcery. # The `Review` tab allows you to get a code review straight away in your IDE. # You can choose between reviewing your current uncommitted changes and reviewing # the changes between your current branch and the default branch. # (Note that this will only work on code that is under Git version control). -def days_between_dates(date1, date2): - d1 = datetime.datetime.strptime(date1, '%Y-%m-%d').date() - d2 = datetime.datetime.strptime(date2, '%Y-%m-%d').date() - delta = d2 - d1 - return delta.days +# If you want reviews when you open a PR, you can add Sourcery to your GitHub or GitLab repos. + # Now let's move on to the `Chat` tab. + # Above each function you'll see a few commands - these are Code Lenses that # you can use to interact with Sourcery. Try clicking on "Ask Sourcery" and # asking it to update the code to use `dateutil`. The answer will appear in # the Sourcery sidebar chat. +def days_between_dates(date1, date2): + d1 = datetime.datetime.strptime(date1, '%Y-%m-%d').date() + d2 = datetime.datetime.strptime(date2, '%Y-%m-%d').date() + delta = d2 - d1 + return delta.days + + # With the Ask Sourcery command or the chat in the sidebar you can ask Sourcery # questions, have it write new code for you, or update existing code.