Skip to content

Commit e4bb3a3

Browse files
Copilotdavidwengier
andcommitted
Address code review feedback: fix cursor position check, add WorkItem attributes, remove duplicate test
- Fix cursor position check to include end position (nameSpan.End instead of Contains) - Add WorkItem attributes to all tests referencing issue #9747 - Add new test AddUsing_OnClick_CursorAtEnd to verify cursor at end of attribute name works - Remove duplicate NoCodeAction_WhenNamespaceAlreadyPresent test (was identical to NoCodeAction_WhenBoundAttribute) All tests pass (8/8 passing, 1 skipped with documented reason). Co-authored-by: davidwengier <[email protected]>
1 parent 47767f6 commit e4bb3a3

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
5151

5252
// Make sure the cursor is actually on the name part, since the attribute block is the whole attribute, including
5353
// value and even some whitespace
54-
if (!attributeBlock.Name.Span.Contains(context.StartAbsoluteIndex))
54+
var nameSpan = attributeBlock.Name.Span;
55+
if (context.StartAbsoluteIndex < nameSpan.Start || context.StartAbsoluteIndex > nameSpan.End)
5556
{
5657
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
5758
}

src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CodeActions/UnboundDirectiveAttributeAddUsingTests.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using Microsoft.CodeAnalysis.Razor.Protocol;
66
using Xunit;
77
using Xunit.Abstractions;
8+
using WorkItemAttribute = Roslyn.Test.Utilities.WorkItemAttribute;
89

910
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost.CodeActions;
1011

1112
public class UnboundDirectiveAttributeAddUsingTests(ITestOutputHelper testOutputHelper) : CohostCodeActionsEndpointTestBase(testOutputHelper)
1213
{
13-
[Fact]
14+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
1415
public async Task AddUsing_OnClick()
1516
{
1617
var input = """
@@ -25,7 +26,22 @@ @using Microsoft.AspNetCore.Components.Web
2526
await VerifyCodeActionAsync(input, expected, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
2627
}
2728

28-
[Fact]
29+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
30+
public async Task AddUsing_OnClick_CursorAtEnd()
31+
{
32+
var input = """
33+
<button @onclick[||]="HandleClick"></button>
34+
""";
35+
36+
var expected = """
37+
@using Microsoft.AspNetCore.Components.Web
38+
<button @onclick="HandleClick"></button>
39+
""";
40+
41+
await VerifyCodeActionAsync(input, expected, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
42+
}
43+
44+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
2945
public async Task AddUsing_OnClick_WithExisting()
3046
{
3147
var input = """
@@ -44,7 +60,7 @@ @using Microsoft.AspNetCore.Components.Web
4460
await VerifyCodeActionAsync(input, expected, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
4561
}
4662

47-
[Fact]
63+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
4864
public async Task AddUsing_OnChange()
4965
{
5066
var input = """
@@ -59,19 +75,7 @@ @using Microsoft.AspNetCore.Components.Web
5975
await VerifyCodeActionAsync(input, expected, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
6076
}
6177

62-
[Fact]
63-
public async Task NoCodeAction_WhenNamespaceAlreadyPresent()
64-
{
65-
var input = """
66-
@using Microsoft.AspNetCore.Components.Web
67-
68-
<button @on[||]click="HandleClick"></button>
69-
""";
70-
71-
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
72-
}
73-
74-
[Fact]
78+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
7579
public async Task NoCodeAction_WhenBoundAttribute()
7680
{
7781
var input = """
@@ -83,7 +87,7 @@ @using Microsoft.AspNetCore.Components.Web
8387
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
8488
}
8589

86-
[Fact]
90+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
8791
public async Task NoCodeAction_WhenNotOnDirectiveAttribute()
8892
{
8993
var input = """
@@ -93,7 +97,7 @@ public async Task NoCodeAction_WhenNotOnDirectiveAttribute()
9397
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
9498
}
9599

96-
[Fact]
100+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
97101
public async Task NoCodeAction_WhenNotOnAttributeName()
98102
{
99103
var input = """
@@ -103,7 +107,7 @@ public async Task NoCodeAction_WhenNotOnAttributeName()
103107
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
104108
}
105109

106-
[Fact]
110+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/9747")]
107111
public async Task AddUsing_Bind()
108112
{
109113
var input = """
@@ -118,7 +122,7 @@ @using Microsoft.AspNetCore.Components.Web
118122
await VerifyCodeActionAsync(input, expected, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
119123
}
120124

121-
[Fact]
125+
[Fact(Skip = "bind:after attribute matching finds System namespace instead of Web"), WorkItem("https://github.com/dotnet/razor/issues/9747")]
122126
public async Task AddUsing_BindWithParameter()
123127
{
124128
var input = """

0 commit comments

Comments
 (0)