Skip to content

Commit 959f78b

Browse files
committed
Naming fixes and simplified promoted method string generation
1 parent 2155895 commit 959f78b

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionProvider.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
4040
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
4141
}
4242

43-
var syntaxTree = context.CodeDocument.GetSyntaxTree();
44-
if (!IsSelectionValid(context, syntaxTree))
43+
if (!TryGetNamespace(context.CodeDocument, out var @namespace))
4544
{
4645
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
4746
}
4847

49-
if (!TryGetNamespace(context.CodeDocument, out var @namespace))
48+
var syntaxTree = context.CodeDocument.GetSyntaxTree();
49+
if (!IsSelectionValid(context, syntaxTree))
5050
{
5151
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
5252
}
@@ -93,6 +93,10 @@ private static bool IsSelectionValid(RazorCodeActionContext context, RazorSyntax
9393

9494
private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, SyntaxNode owner)
9595
{
96+
// The selection could start either in a MarkupElement or MarkupTagHelperElement.
97+
// Both of these have the necessary properties to do this check, but not the base MarkupSyntaxNode.
98+
// The workaround for this is to try to cast to the specific types and then do the check.
99+
96100
var tryMakeMarkupElement = owner.FirstAncestorOrSelf<MarkupElementSyntax>();
97101
var tryMakeMarkupTagHelperElement = owner.FirstAncestorOrSelf<MarkupTagHelperElementSyntax>();
98102

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal sealed record SelectionAnalysisResult
178178
public required bool Success;
179179
public int ExtractStart;
180180
public int ExtractEnd;
181-
public HashSet<string>? ComponentDependencies;
181+
public HashSet<string>? UsingDirectives;
182182
public HashSet<string>? TentativeVariableDependencies;
183183
}
184184

@@ -205,15 +205,15 @@ private static SelectionAnalysisResult TryAnalyzeSelection(RazorCodeDocument cod
205205
}
206206

207207
var dependencyScanRoot = FindNearestCommonAncestor(startElementNode, endElementNode) ?? startElementNode;
208-
var componentDependencies = AddComponentDependenciesInRange(dependencyScanRoot, extractStart, extractEnd);
208+
var componentDependencies = AddUsingDirectivesInRange(dependencyScanRoot, extractStart, extractEnd);
209209
var variableDependencies = AddVariableDependenciesInRange(dependencyScanRoot, extractStart, extractEnd);
210210

211211
return new SelectionAnalysisResult
212212
{
213213
Success = success,
214214
ExtractStart = extractStart,
215215
ExtractEnd = extractEnd,
216-
ComponentDependencies = componentDependencies,
216+
UsingDirectives = componentDependencies,
217217
TentativeVariableDependencies = variableDependencies,
218218
};
219219
}
@@ -439,21 +439,21 @@ private static bool IsValidNode(SyntaxNode node, bool isCodeBlock)
439439
return node is MarkupElementSyntax or MarkupTagHelperElementSyntax || (isCodeBlock && node is CSharpCodeBlockSyntax);
440440
}
441441

442-
private static HashSet<string> AddComponentDependenciesInRange(SyntaxNode root, int extractStart, int extractEnd)
442+
private static HashSet<string> AddUsingDirectivesInRange(SyntaxNode root, int extractStart, int extractEnd)
443443
{
444-
var dependencies = new HashSet<string>();
444+
var usings = new HashSet<string>();
445445
var extractSpan = new TextSpan(extractStart, extractEnd - extractStart);
446446

447447
// Only analyze nodes within the extract span
448448
foreach (var node in root.DescendantNodes().Where(node => extractSpan.Contains(node.Span)))
449449
{
450450
if (node is MarkupTagHelperElementSyntax { TagHelperInfo: { } tagHelperInfo })
451451
{
452-
AddDependenciesFromTagHelperInfo(tagHelperInfo, dependencies);
452+
AddUsingFromTagHelperInfo(tagHelperInfo, usings);
453453
}
454454
}
455455

456-
return dependencies;
456+
return usings;
457457
}
458458

459459
private static HashSet<string> AddVariableDependenciesInRange(SyntaxNode root, int extractStart, int extractEnd)
@@ -478,7 +478,7 @@ private static HashSet<string> AddVariableDependenciesInRange(SyntaxNode root, i
478478
return dependencies;
479479
}
480480

481-
private static void AddDependenciesFromTagHelperInfo(TagHelperInfo tagHelperInfo, HashSet<string> dependencies)
481+
private static void AddUsingFromTagHelperInfo(TagHelperInfo tagHelperInfo, HashSet<string> dependencies)
482482
{
483483
foreach (var descriptor in tagHelperInfo.BindingResult.Descriptors)
484484
{
@@ -508,9 +508,9 @@ private static void AddDependenciesFromTagHelperInfo(TagHelperInfo tagHelperInfo
508508

509509
var inst = PooledStringBuilder.GetInstance();
510510
var newFileContentBuilder = inst.Builder;
511-
if (selectionAnalysis.ComponentDependencies is not null)
511+
if (selectionAnalysis.UsingDirectives is not null)
512512
{
513-
foreach (var dependency in selectionAnalysis.ComponentDependencies)
513+
foreach (var dependency in selectionAnalysis.UsingDirectives)
514514
{
515515
newFileContentBuilder.AppendLine($"@using {dependency}");
516516
}
@@ -750,40 +750,34 @@ private static string GeneratePromotedMethods(HashSet<MethodSymbolicInfo> method
750750
builder.AppendLine($"/// Delegate for the '{method.Name}' method.");
751751
builder.AppendLine("/// </summary>");
752752
builder.AppendLine("[Parameter]");
753-
builder.Append("public ");
754753

755-
if (method.ReturnType == "void")
756-
{
757-
builder.Append("Action");
758-
}
759-
else
760-
{
761-
builder.Append("Func<");
762-
}
754+
// Start building delegate type
755+
builder.Append("public ");
756+
builder.Append(method.ReturnType == "void" ? "Action" : "Func");
763757

764-
if (method.ParameterTypes.Count() > 0)
758+
// If delegate type is Action, only add generic parameters if needed.
759+
if (method.ParameterTypes.Length > 0 || method.ReturnType != "void")
765760
{
766-
if (method.ReturnType == "void")
767-
{
768-
builder.Append('<');
769-
}
770-
761+
builder.Append("<");
771762
builder.Append(string.Join(", ", method.ParameterTypes));
763+
772764
if (method.ReturnType != "void")
773765
{
774-
builder.Append(", ");
766+
if (method.ParameterTypes.Length > 0)
767+
{
768+
// Add one last comma in the list of generic parameters for the result: "<..., TResult>"
769+
builder.Append(", ");
770+
}
771+
builder.Append(method.ReturnType);
775772
}
776-
}
777773

778-
if (method.ReturnType != "void")
779-
{
780-
builder.Append(method.ReturnType);
774+
builder.Append('>');
781775
}
782776

783-
builder.Append($"{(method.ReturnType == "void" ? (method.ParameterTypes.Count() > 0 ? ">" : "") : ">")}? " +
784-
$"Parameter{(parameterCount > 0 ? parameterCount : "")} {{ get; set; }}");
777+
builder.Append($"Parameter{(parameterCount > 0 ? parameterCount : "")} {{ get; set; }}");
785778
if (parameterCount < totalMethods - 1)
786779
{
780+
// Space between methods except for the last method.
787781
builder.AppendLine();
788782
builder.AppendLine();
789783
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ private async Task ValidateExtractComponentCodeActionAsync(
13151315
string? expected,
13161316
string codeAction,
13171317
int childActionIndex = 0,
1318+
IEnumerable<(string filePath, string contents)>? additionalRazorFiles = null,
13181319
IRazorCodeActionProvider[]? razorCodeActionProviders = null,
13191320
Func<string, RazorCodeDocument, IRazorFormattingService, IClientConnection, RazorLSPOptionsMonitor?, IRazorCodeActionResolver[]>? codeActionResolversCreator = null,
13201321
RazorLSPOptionsMonitor? optionsMonitor = null,
@@ -1327,7 +1328,7 @@ private async Task ValidateExtractComponentCodeActionAsync(
13271328
var codeDocument = CreateCodeDocument(input, filePath: razorFilePath);
13281329
var sourceText = codeDocument.GetSourceText();
13291330
var uri = new Uri(razorFilePath);
1330-
var languageServer = await CreateLanguageServerAsync(codeDocument, razorFilePath);
1331+
var languageServer = await CreateLanguageServerAsync(codeDocument, razorFilePath, additionalRazorFiles);
13311332
var documentContext = CreateDocumentContext(uri, codeDocument);
13321333
var requestContext = new RazorRequestContext(documentContext, null!, "lsp/method", uri: null);
13331334

0 commit comments

Comments
 (0)