Skip to content

Commit 47767f6

Browse files
committed
Ensure action is only offered on the name portion of the attribute
1 parent e057c87 commit 47767f6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
4949
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
5050
}
5151

52+
// Make sure the cursor is actually on the name part, since the attribute block is the whole attribute, including
53+
// value and even some whitespace
54+
if (!attributeBlock.Name.Span.Contains(context.StartAbsoluteIndex))
55+
{
56+
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
57+
}
58+
5259
// Try to find the missing namespace for this directive attribute
5360
if (!TryGetMissingDirectiveAttributeNamespace(context.CodeDocument, attributeBlock, out var missingNamespace))
5461
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ public async Task NoCodeAction_WhenNotOnDirectiveAttribute()
9393
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
9494
}
9595

96+
[Fact]
97+
public async Task NoCodeAction_WhenNotOnAttributeName()
98+
{
99+
var input = """
100+
<input @onchange="HandleCha[||]nge" />
101+
""";
102+
103+
await VerifyCodeActionAsync(input, expected: null, LanguageServerConstants.CodeActions.AddUsing, addDefaultImports: false);
104+
}
105+
96106
[Fact]
97107
public async Task AddUsing_Bind()
98108
{

0 commit comments

Comments
 (0)