Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class CSharpCodeActionProvider(LanguageServerFeatureOptions languageSer
RazorPredefinedCodeFixProviderNames.ImplementAbstractClass,
RazorPredefinedCodeFixProviderNames.ImplementInterface,
RazorPredefinedCodeFixProviderNames.RemoveUnusedVariable,
RazorPredefinedCodeFixProviderNames.RemoveUnusedMembers,
Comment thread
davidwengier marked this conversation as resolved.
Outdated
];

// We don't support any code actions in implicit expressions at the moment, but rather than simply returning early
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ public IEnumerable<TextChange> GetRazorDocumentEdits(RazorCSharpDocument csharpD
}
}

// The opposite case of the above: for the last line of a code block, the C# formatter might
// return an edit that starts within our mapping, but ends after. In those cases, when the edit
// spans multiple lines we just take the first line and try to use that.
//
// This can happen with code actions that remove content, where the edit starts inside the mapped
// region but extends beyond it. For example, when removing an unused variable in a single-line
// explicit statement block like `@{ var x = 1; }`, Roslyn may generate edits that span beyond
// the mapped C# region.
if (mappedStart && !mappedEnd && startLine != endLine)
{
// Construct a theoretical edit that is just for the first line of the edit that the C# formatter
// gave us, and see if we can map that.
var firstNewLine = change.NewText.AssumeNotNull().IndexOfAny(['\n', '\r']);
Comment thread
ToddGrun marked this conversation as resolved.
Outdated

// Get the end of the start line
var endSync = csharpSourceText.TryGetAbsoluteIndex((startLine, csharpSourceText.Lines[startLine].Span.Length), out var endIndex);
Comment thread
ToddGrun marked this conversation as resolved.
Outdated
if (endSync is false)
Comment thread
ToddGrun marked this conversation as resolved.
Outdated
{
break;
}

mappedEnd = this.TryMapToRazorDocumentPosition(csharpDocument, endIndex, out _, out hostEndIndex);

if (mappedStart && mappedEnd)
Comment thread
ToddGrun marked this conversation as resolved.
Outdated
{
// If there's a newline in the new text, only take the part before it
var newText = firstNewLine >= 0 ? change.NewText[..firstNewLine] : change.NewText;
yield return new TextChange(TextSpan.FromBounds(hostStartIndex, hostEndIndex), newText);
continue;
}
}

// If we couldn't map either the start or the end then we still might want to do something tricky.
// When we have a block like this:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,156 @@ private string GetDebuggerDisplay()

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeRefactoringProviderNames.AddDebuggerDisplay);
}

Comment thread
davidwengier marked this conversation as resolved.
[Fact]
public async Task RemoveUnusedVariable_Local()
{
var input = """
@code
{
void M()
{
int {|IDE0059:[||]x|} = 5;
}
}
""";

var expected = """
@code
{
void M()
{
}
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedVariable);
}

[Fact]
public async Task RemoveUnusedVariable_ExplicitStatement_SingleLine()
{
var input = """
@{ var {|IDE0059:[||]x|} = 1; var y = 2; }
Comment thread
davidwengier marked this conversation as resolved.
""";

var expected = """
@{
var y = 2;
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedVariable);
}

[Fact]
public async Task RemoveUnusedVariable_ExplicitStatement_SingleLine_OnlyVariable()
{
var input = """
@{ var {|IDE0059:[||]message|} = "Hello World"; }
""";

var expected = """
@{}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedVariable);
}

[Fact]
public async Task RemoveUnusedVariable_ExplicitStatement_MultiLine()
{
var input = """
@{
var {|IDE0059:[||]x|} = 1;
var y = 2;
}
""";

var expected = """
@{
var y = 2;
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedVariable);
}

[Fact(Skip = "RemoveUnusedMembers code action is not triggered in test scenarios - requires full analyzer support")]
public async Task RemoveUnusedVariable_Parameter()
{
var input = """
@code
{
void M(int {|IDE0060:[||]unusedParameter|})
{
}
}
""";

var expected = """
@code
{
void M()
{
}
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedMembers);
}

[Fact(Skip = "RemoveUnusedMembers code action is not triggered in test scenarios - requires full analyzer support")]
public async Task RemoveUnusedVariable_PrivateField()
{
var input = """
@code
{
private int {|IDE0051:[||]_field|};

void M()
{
}
}
""";

var expected = """
@code
{
void M()
{
}
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedMembers);
}

[Fact(Skip = "RemoveUnusedMembers code action is not triggered in test scenarios - requires full analyzer support")]
public async Task RemoveUnusedVariable_PrivateMethod()
{
var input = """
@code
{
private void {|IDE0051:[||]UnusedMethod|}()
{
}

void M()
{
}
}
""";

var expected = """
@code
{
void M()
{
}
}
""";

await VerifyCodeActionAsync(input, expected, RazorPredefinedCodeFixProviderNames.RemoveUnusedMembers);
}
}