Skip to content

Commit 48168f8

Browse files
Copilotdavidwengier
andcommitted
Address code review feedback: allow data-enhance-nav on any element and refactor duplicate check
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent bbd20bf commit 48168f8

File tree

3 files changed

+78
-24
lines changed

3 files changed

+78
-24
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/BlazorDataAttributeCompletionItemProvider.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,23 @@ public ImmutableArray<RazorCompletionItem> GetCompletionItems(RazorCompletionCon
8686
continue;
8787
}
8888

89-
// Only show data-enhance-nav for anchor elements
90-
if (attributeName == "data-enhance-nav" &&
91-
!string.Equals(containingTagName, "a", System.StringComparison.OrdinalIgnoreCase))
92-
{
93-
continue;
94-
}
89+
// data-enhance-nav can go on any element, no filtering needed
9590

9691
// Check if the attribute already exists on the element
9792
var alreadyExists = false;
9893
foreach (var attribute in attributes)
9994
{
100-
if (attribute.Kind == AspNetCore.Razor.Language.SyntaxKind.MarkupAttributeBlock)
95+
var existingAttributeName = attribute.Kind switch
10196
{
102-
var attrBlock = (MarkupAttributeBlockSyntax)attribute;
103-
if (string.Equals(attrBlock.Name.GetContent(), attributeName, System.StringComparison.Ordinal))
104-
{
105-
alreadyExists = true;
106-
break;
107-
}
108-
}
109-
else if (attribute.Kind == AspNetCore.Razor.Language.SyntaxKind.MarkupMinimizedAttributeBlock)
97+
AspNetCore.Razor.Language.SyntaxKind.MarkupAttributeBlock => ((MarkupAttributeBlockSyntax)attribute).Name.GetContent(),
98+
AspNetCore.Razor.Language.SyntaxKind.MarkupMinimizedAttributeBlock => ((MarkupMinimizedAttributeBlockSyntax)attribute).Name.GetContent(),
99+
_ => null
100+
};
101+
102+
if (existingAttributeName != null && string.Equals(existingAttributeName, attributeName, System.StringComparison.Ordinal))
110103
{
111-
var minAttrBlock = (MarkupMinimizedAttributeBlockSyntax)attribute;
112-
if (string.Equals(minAttrBlock.Name.GetContent(), attributeName, System.StringComparison.Ordinal))
113-
{
114-
alreadyExists = true;
115-
break;
116-
}
104+
alreadyExists = true;
105+
break;
117106
}
118107
}
119108

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Completion/BlazorDataAttributeCompletionItemProviderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public void GetCompletionItems_OnNonFormElement_DoesNotReturnDataEnhance()
101101
}
102102

103103
[Fact]
104-
public void GetCompletionItems_OnNonAnchorElement_DoesNotReturnDataEnhanceNav()
104+
public void GetCompletionItems_OnDivElement_ReturnsDataEnhanceNav()
105105
{
106-
// Arrange
106+
// Arrange - data-enhance-nav can go on any element, not just anchors
107107
TestCode testCode = "<div d$$></div>";
108108
var context = CreateRazorCompletionContext(testCode);
109109

@@ -112,7 +112,7 @@ public void GetCompletionItems_OnNonAnchorElement_DoesNotReturnDataEnhanceNav()
112112

113113
// Assert
114114
var dataEnhanceNav = completions.FirstOrDefault(c => c.DisplayText == "data-enhance-nav");
115-
Assert.Null(dataEnhanceNav);
115+
Assert.NotNull(dataEnhanceNav);
116116
}
117117

118118
[Fact]

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/Shared/CohostDocumentCompletionEndpointTest.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,71 @@ The end.
746746
Assert.All(list.Items, item => Assert.DoesNotContain("=", item.CommitCharacters ?? []));
747747
}
748748

749+
[Fact]
750+
public async Task BlazorDataEnhanceAttributeCompletion_OnFormElement()
751+
{
752+
await VerifyCompletionListAsync(
753+
input: """
754+
This is a Razor document.
755+
756+
<form d$$></form>
757+
758+
The end.
759+
""",
760+
completionContext: new VSInternalCompletionContext()
761+
{
762+
InvokeKind = VSInternalCompletionInvokeKind.Typing,
763+
TriggerCharacter = "d",
764+
TriggerKind = CompletionTriggerKind.TriggerCharacter
765+
},
766+
expectedItemLabels: ["data-enhance", "data-enhance-nav", "data-permanent", "dir"],
767+
htmlItemLabels: ["dir"]);
768+
}
769+
770+
[Fact]
771+
public async Task BlazorDataEnhanceNavAttributeCompletion_OnAnyElement()
772+
{
773+
await VerifyCompletionListAsync(
774+
input: """
775+
This is a Razor document.
776+
777+
<div d$$></div>
778+
779+
The end.
780+
""",
781+
completionContext: new VSInternalCompletionContext()
782+
{
783+
InvokeKind = VSInternalCompletionInvokeKind.Typing,
784+
TriggerCharacter = "d",
785+
TriggerKind = CompletionTriggerKind.TriggerCharacter
786+
},
787+
expectedItemLabels: ["data-enhance-nav", "data-permanent", "dir"],
788+
unexpectedItemLabels: ["data-enhance"],
789+
htmlItemLabels: ["dir"]);
790+
}
791+
792+
[Fact]
793+
public async Task BlazorDataPermanentAttributeCompletion_OnAnchorElement()
794+
{
795+
await VerifyCompletionListAsync(
796+
input: """
797+
This is a Razor document.
798+
799+
<a d$$></a>
800+
801+
The end.
802+
""",
803+
completionContext: new VSInternalCompletionContext()
804+
{
805+
InvokeKind = VSInternalCompletionInvokeKind.Typing,
806+
TriggerCharacter = "d",
807+
TriggerKind = CompletionTriggerKind.TriggerCharacter
808+
},
809+
expectedItemLabels: ["data-enhance-nav", "data-permanent", "dir"],
810+
unexpectedItemLabels: ["data-enhance"],
811+
htmlItemLabels: ["dir"]);
812+
}
813+
749814
private async Task<RazorVSInternalCompletionList> VerifyCompletionListAsync(
750815
TestCode input,
751816
VSInternalCompletionContext completionContext,

0 commit comments

Comments
 (0)