Skip to content

Commit 5ec6735

Browse files
Copilotdavidwengier
andcommitted
Address PR feedback: simplify provider, reorder edits, use correct types, remove unnecessary test files
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent 4790978 commit 5ec6735

File tree

3 files changed

+29
-90
lines changed

3 files changed

+29
-90
lines changed

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
5353
}
5454

5555
// Check whether the element represents a fully qualified component
56-
if (!IsFullyQualifiedComponent(markupElementSyntax, out var fullyQualifiedName, out var componentName))
57-
{
58-
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
59-
}
60-
61-
// Extract the namespace from the fully qualified name
62-
var @namespace = GetNamespaceFromFullyQualifiedName(fullyQualifiedName);
63-
if (string.IsNullOrEmpty(@namespace))
56+
if (!IsFullyQualifiedComponent(markupElementSyntax, out var @namespace, out var componentName))
6457
{
6558
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
6659
}
@@ -117,9 +110,9 @@ private static bool HasDiagnosticsOnStartTag(MarkupTagHelperElementSyntax elemen
117110
return false;
118111
}
119112

120-
private static bool IsFullyQualifiedComponent(MarkupTagHelperElementSyntax element, out string fullyQualifiedName, out string componentName)
113+
private static bool IsFullyQualifiedComponent(MarkupTagHelperElementSyntax element, out string @namespace, out string componentName)
121114
{
122-
fullyQualifiedName = string.Empty;
115+
@namespace = string.Empty;
123116
componentName = string.Empty;
124117

125118
if (element.TagHelperInfo?.BindingResult?.Descriptors is not [.. var descriptors])
@@ -139,27 +132,17 @@ private static bool IsFullyQualifiedComponent(MarkupTagHelperElementSyntax eleme
139132
return false;
140133
}
141134

142-
fullyQualifiedName = boundTagHelper.Name;
135+
var fullyQualifiedName = boundTagHelper.Name;
143136

144-
// Extract the component name (last part after the last dot)
137+
// Extract the namespace and component name
145138
var lastDotIndex = fullyQualifiedName.LastIndexOf('.');
146139
if (lastDotIndex < 0)
147140
{
148141
return false;
149142
}
150143

144+
@namespace = fullyQualifiedName.Substring(0, lastDotIndex);
151145
componentName = fullyQualifiedName.Substring(lastDotIndex + 1);
152146
return true;
153147
}
154-
155-
private static string GetNamespaceFromFullyQualifiedName(string fullyQualifiedName)
156-
{
157-
var lastDotIndex = fullyQualifiedName.LastIndexOf('.');
158-
if (lastDotIndex < 0)
159-
{
160-
return string.Empty;
161-
}
162-
163-
return fullyQualifiedName.Substring(0, lastDotIndex);
164-
}
165148
}

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ internal class SimplifyFullyQualifiedComponentCodeActionResolver : IRazorCodeAct
3737

3838
var codeDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier() { DocumentUri = new(documentContext.Uri) };
3939

40-
using var documentChanges = new PooledArrayBuilder<TextDocumentEdit>();
41-
4240
// Check if the using directive already exists
4341
var syntaxTree = codeDocument.GetSyntaxTree();
4442
if (syntaxTree is null)
@@ -60,18 +58,11 @@ internal class SimplifyFullyQualifiedComponentCodeActionResolver : IRazorCodeAct
6058
return false;
6159
});
6260

63-
// First, add the tag simplification edits (at the original positions in the document)
64-
using var tagEdits = new PooledArrayBuilder<TextEdit>();
61+
// Build the tag simplification edits (at the original positions in the document)
62+
using var tagEdits = new PooledArrayBuilder<SumType<TextEdit, AnnotatedTextEdit>>();
6563

66-
// Replace the fully qualified name with the simple component name in start tag
67-
var startTagRange = text.GetRange(actionParams.StartTagSpanStart, actionParams.StartTagSpanEnd);
68-
tagEdits.Add(new TextEdit
69-
{
70-
NewText = actionParams.ComponentName,
71-
Range = startTagRange,
72-
});
73-
74-
// Replace the fully qualified name with the simple component name in end tag (if it exists)
64+
// Replace the fully qualified name with the simple component name in end tag first (if it exists)
65+
// The end tag edit must come before the start tag edit, as clients may not re-order them
7566
if (actionParams.EndTagSpanStart >= 0 && actionParams.EndTagSpanEnd >= 0)
7667
{
7768
var endTagRange = text.GetRange(actionParams.EndTagSpanStart, actionParams.EndTagSpanEnd);
@@ -82,25 +73,30 @@ internal class SimplifyFullyQualifiedComponentCodeActionResolver : IRazorCodeAct
8273
});
8374
}
8475

85-
documentChanges.Add(new TextDocumentEdit()
76+
// Replace the fully qualified name with the simple component name in start tag
77+
var startTagRange = text.GetRange(actionParams.StartTagSpanStart, actionParams.StartTagSpanEnd);
78+
tagEdits.Add(new TextEdit
8679
{
87-
TextDocument = codeDocumentIdentifier,
88-
Edits = tagEdits.ToArray().Select(e => (SumType<TextEdit, AnnotatedTextEdit>)e).ToArray()
80+
NewText = actionParams.ComponentName,
81+
Range = startTagRange,
8982
});
9083

91-
// Then, add using directive if it doesn't already exist (at the top of the file)
84+
// Add using directive if it doesn't already exist (at the top of the file)
9285
// This must come after the tag edits because the using directive will be inserted at the top,
9386
// which would change line numbers for subsequent edits
9487
if (!namespaceAlreadyExists)
9588
{
9689
var addUsingEdit = AddUsingsHelper.CreateAddUsingTextEdit(actionParams.Namespace, codeDocument);
97-
documentChanges.Add(new TextDocumentEdit()
98-
{
99-
TextDocument = codeDocumentIdentifier,
100-
Edits = [addUsingEdit]
101-
});
90+
tagEdits.Add(addUsingEdit);
10291
}
10392

93+
using var documentChanges = new PooledArrayBuilder<TextDocumentEdit>();
94+
documentChanges.Add(new TextDocumentEdit()
95+
{
96+
TextDocument = codeDocumentIdentifier,
97+
Edits = tagEdits.ToArray()
98+
});
99+
104100
return new WorkspaceEdit
105101
{
106102
DocumentChanges = documentChanges.ToArray(),

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ @using Microsoft.AspNetCore.Components.Authorization
2929
3030
<div></div>
3131
""",
32-
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent,
33-
additionalFiles: [
34-
(FilePath("Microsoft/AspNetCore/Components/Authorization/AuthorizeRouteView.razor"), """
35-
<div>
36-
Authorize Route View
37-
</div>
38-
""")]);
32+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
3933
}
4034

4135
[Fact]
@@ -60,13 +54,7 @@ @using Microsoft.AspNetCore.Components.Authorization
6054
6155
<div></div>
6256
""",
63-
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent,
64-
additionalFiles: [
65-
(FilePath("Microsoft/AspNetCore/Components/Authorization/AuthorizeRouteView.razor"), """
66-
<div>
67-
Authorize Route View
68-
</div>
69-
""")]);
57+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
7058
}
7159

7260
[Fact]
@@ -92,18 +80,7 @@ @using Microsoft.AspNetCore.Components.Authorization
9280
9381
<div></div>
9482
""",
95-
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent,
96-
additionalFiles: [
97-
(FilePath("Microsoft/AspNetCore/Components/Authorization/AuthorizeRouteView.razor"), """
98-
<div>
99-
Authorize Route View
100-
</div>
101-
102-
@code {
103-
[Parameter]
104-
public RenderFragment ChildContent { get; set; } = null!;
105-
}
106-
""")]);
83+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
10784
}
10885

10986
[Fact]
@@ -125,18 +102,7 @@ @using Microsoft.AspNetCore.Components.Authorization
125102
126103
<div></div>
127104
""",
128-
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent,
129-
additionalFiles: [
130-
(FilePath("Microsoft/AspNetCore/Components/Authorization/AuthorizeRouteView.razor"), """
131-
<div>
132-
Authorize Route View
133-
</div>
134-
135-
@code {
136-
[Parameter]
137-
public string Resource { get; set; } = null!;
138-
}
139-
""")]);
105+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
140106
}
141107

142108
[Fact]
@@ -244,12 +210,6 @@ @using Microsoft.AspNetCore.Components.Authorization
244210
245211
<div></div>
246212
""",
247-
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent,
248-
additionalFiles: [
249-
(FilePath("Microsoft/AspNetCore/Components/Authorization/AuthorizeRouteView.razor"), """
250-
<div>
251-
Authorize Route View
252-
</div>
253-
""")]);
213+
codeActionName: LanguageServerConstants.CodeActions.SimplifyFullyQualifiedComponent);
254214
}
255215
}

0 commit comments

Comments
 (0)