Skip to content

Commit 1d6f68c

Browse files
author
N. Taylor Mullen
committed
Remove directive attribute completions from element completion list.
- This removes the excess amount of directive attribute completions that show up when completing a component. - Added a test to verify directive attributes no longer show up in element completions. dotnet/aspnetcore#9133
1 parent d8b62e1 commit 1d6f68c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DefaultTagHelperCompletionService.cs

+8
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ public override ElementCompletionResult GetElementCompletions(ElementCompletionC
243243

244244
void UpdateCompletions(string tagName, TagHelperDescriptor possibleDescriptor)
245245
{
246+
if (possibleDescriptor.BoundAttributes.Any(boundAttribute => boundAttribute.IsDirectiveAttribute()))
247+
{
248+
// This is a TagHelper that ultimately represents a DirectiveAttribute. In classic Razor TagHelper land TagHelpers with bound attribute descriptors
249+
// were valuable to show in the completion list to understand what was possible for a certain tag; however, with Blazor directive attributes stand
250+
// on their own and shouldn't be indicated at the element level completion.
251+
return;
252+
}
253+
246254
if (!elementCompletions.TryGetValue(tagName, out var existingRuleDescriptors))
247255
{
248256
existingRuleDescriptors = new HashSet<TagHelperDescriptor>();

src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/DefaultTagHelperCompletionServiceTest.cs

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.AspNetCore.Razor.Language.Components;
89
using Microsoft.CodeAnalysis.Razor;
910
using Xunit;
1011

@@ -597,6 +598,42 @@ public void GetAttributeCompletions_NoDescriptorsForTagReturnsExistingCompletion
597598
AssertCompletionsAreEquivalent(expectedCompletions, completions);
598599
}
599600

601+
[Fact]
602+
public void GetElementCompletions_IgnoresDirectiveAttributes()
603+
{
604+
// Arrange
605+
var documentDescriptors = new[]
606+
{
607+
TagHelperDescriptorBuilder.Create("BindAttribute", "TestAssembly")
608+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("input"))
609+
.BoundAttributeDescriptor(builder =>
610+
{
611+
builder.Name = "@bind";
612+
builder.AddMetadata(ComponentMetadata.Common.DirectiveAttribute, bool.TrueString);
613+
})
614+
.TagOutputHint("table")
615+
.Build(),
616+
};
617+
var expectedCompletions = ElementCompletionResult.Create(new Dictionary<string, HashSet<TagHelperDescriptor>>()
618+
{
619+
["table"] = new HashSet<TagHelperDescriptor>(),
620+
});
621+
622+
var existingCompletions = new[] { "table" };
623+
var completionContext = BuildElementCompletionContext(
624+
documentDescriptors,
625+
existingCompletions,
626+
containingTagName: "body",
627+
containingParentTagName: null);
628+
var service = CreateTagHelperCompletionFactsService();
629+
630+
// Act
631+
var completions = service.GetElementCompletions(completionContext);
632+
633+
// Assert
634+
AssertCompletionsAreEquivalent(expectedCompletions, completions);
635+
}
636+
600637
[Fact]
601638
public void GetElementCompletions_TagOutputHintDoesNotFallThroughToSchemaCheck()
602639
{

0 commit comments

Comments
 (0)