Add test coverage and bug fix for RemoveUnusedVariable code action in Razor files#12380
Add test coverage and bug fix for RemoveUnusedVariable code action in Razor files#12380davidwengier merged 10 commits intomainfrom
Conversation
…bles and parameters Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
...t.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CodeActions/CSharpCodeActionTests.cs
Show resolved
Hide resolved
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
...t.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CodeActions/CSharpCodeActionTests.cs
Show resolved
Hide resolved
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…used variable
Added handling in GetRazorDocumentEdits for the case where the start position
maps successfully but the end position doesn't when an edit spans multiple lines.
This is the opposite of the existing logic that handles when the end maps but
not the start. The fix enables the RemoveUnusedVariable code action to work
for single-line explicit statements like @{ var message = "Hello World"; }.
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
...r/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/CSharp/CSharpCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
…riable Removed RemoveUnusedMembers from the allow list since it's not actually offered by Roslyn. Removed the three skipped tests that were testing RemoveUnusedMembers (Parameter, PrivateField, PrivateMethod). The PR now focuses on adding test coverage and a bug fix for RemoveUnusedVariable which was already in the list. Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
...rc/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/AbstractDocumentMappingService.cs
Outdated
Show resolved
Hide resolved
- Changed TryGetAbsoluteIndex call to use the overload with separate line and character parameters instead of a tuple - Removed redundant mappedStart check in inner condition since it's already guaranteed by the outer if condition Co-authored-by: ToddGrun <6785178+ToddGrun@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…dition Changed from storing the result in endSync variable and checking it separately to directly using the TryGetAbsoluteIndex result in the if condition with negation. This makes the code more concise and easier to read. Co-authored-by: ToddGrun <6785178+ToddGrun@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| mappedEnd = this.TryMapToRazorDocumentPosition(csharpDocument, endIndex, out _, out hostEndIndex); | ||
|
|
||
| if (mappedEnd) |
There was a problem hiding this comment.
@copilot rather than bothering to set mappedEnd here, just inline the method call:
| mappedEnd = this.TryMapToRazorDocumentPosition(csharpDocument, endIndex, out _, out hostEndIndex); | |
| if (mappedEnd) | |
| if (this.TryMapToRazorDocumentPosition(csharpDocument, endIndex, out _, out hostEndIndex)) |
There was a problem hiding this comment.
Done in commit 87ca65a. Inlined the TryMapToRazorDocumentPosition call directly in the if condition instead of storing the result in mappedEnd first.
Removed the intermediate mappedEnd assignment and directly used the TryMapToRazorDocumentPosition result in the if condition. This makes the code cleaner by avoiding the unnecessary variable assignment. Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Summary
Successfully added test coverage and fixed a bug for C# RemoveUnusedVariable code actions in Razor files.
Changes Made
Modified Files:
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/AbstractDocumentMappingService.cs- Fixed edit mapping for single-line explicit statementssrc/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CodeActions/CSharpCodeActionTests.cs- Added 4 comprehensive testsCode Actions Tested
@codeblocks@{ }) with multiple statementsTest Results
Bug Fix
Fixed a bug in
AbstractDocumentMappingService.GetRazorDocumentEditswhere C# edits that started within the mapped region but ended outside of it were not handled. This was preventing the RemoveUnusedVariable code action from working in single-line explicit statements with only one unused variable (e.g.,@{ var message = "Hello World"; }).The fix adds symmetric handling to the existing logic that dealt with edits starting outside but ending inside the mapped region. Now both scenarios are properly handled by taking the appropriate portion of the edit.
How It Works
The RemoveUnusedVariable code action was already in Razor's allow list. This PR adds comprehensive test coverage and fixes a bug in edit mapping that was preventing the code action from working in certain scenarios. When Roslyn's analyzers detect unused local variables (IDE0059/CA1804) in Razor @code blocks or @{ } explicit statements, the code fix provider offers a removal action which now works correctly in all tested scenarios.
Example
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.