Skip to content

Commit 8b46320

Browse files
committed
Only offer on start tag name, or end tag, and add tests
1 parent a7a4439 commit 8b46320

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
3838
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
3939
}
4040

41-
// Find the start tag at the cursor position - we only want to trigger on the start tag, not content
42-
var owner = syntaxRoot.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: true)?.FirstAncestorOrSelf<MarkupTagHelperStartTagSyntax>();
43-
if (owner is not MarkupTagHelperStartTagSyntax startTag)
41+
// Find the tag at the cursor position, if it's on the start tag (name portion) or end tag only.
42+
var owner = syntaxRoot.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: true) switch
4443
{
45-
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
46-
}
44+
MarkupTagHelperStartTagSyntax startTag when startTag.Name.Span.Contains(context.StartAbsoluteIndex) => startTag.Parent,
45+
MarkupTagHelperEndTagSyntax endTag => endTag.Parent,
46+
_ => null
47+
};
4748

48-
// Get the parent element to access tag helper binding information
49-
if (startTag.Parent is not MarkupTagHelperElementSyntax markupElementSyntax)
49+
if (owner is not MarkupTagHelperElementSyntax markupElementSyntax)
5050
{
5151
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
5252
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,39 @@ await VerifyCodeActionAsync(
362362
expected: null,
363363
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
364364
}
365+
366+
[Fact]
367+
public async Task DoNotOfferInAttribute()
368+
{
369+
await VerifyCodeActionAsync(
370+
input: """
371+
<Microsoft.AspNetCore.Components.Forms.InputText class="bo[||]ld"></Microsoft.AspNetCore.Components.Forms.InputText>
372+
""",
373+
expected: null,
374+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
375+
}
376+
377+
[Fact]
378+
public async Task SimplifyFullyQualifiedComponent_EndTag()
379+
{
380+
// Microsoft.AspNetCore.Components.Forms is automatically in scope, so no using directive should be added
381+
await VerifyCodeActionAsync(
382+
input: """
383+
<div></div>
384+
385+
<Microsoft.AspNetCore.Components.Forms.InputText>
386+
</Microsoft.AspNetCore.Components.Forms.Input[||]Text>
387+
388+
<div></div>
389+
""",
390+
expected: """
391+
<div></div>
392+
393+
<InputText>
394+
</InputText>
395+
396+
<div></div>
397+
""",
398+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
399+
}
365400
}

0 commit comments

Comments
 (0)