@@ -178,7 +178,7 @@ internal sealed record SelectionAnalysisResult
178
178
public required bool Success ;
179
179
public int ExtractStart ;
180
180
public int ExtractEnd ;
181
- public HashSet < string > ? ComponentDependencies ;
181
+ public HashSet < string > ? UsingDirectives ;
182
182
public HashSet < string > ? TentativeVariableDependencies ;
183
183
}
184
184
@@ -205,15 +205,15 @@ private static SelectionAnalysisResult TryAnalyzeSelection(RazorCodeDocument cod
205
205
}
206
206
207
207
var dependencyScanRoot = FindNearestCommonAncestor ( startElementNode , endElementNode ) ?? startElementNode ;
208
- var componentDependencies = AddComponentDependenciesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
208
+ var componentDependencies = AddUsingDirectivesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
209
209
var variableDependencies = AddVariableDependenciesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
210
210
211
211
return new SelectionAnalysisResult
212
212
{
213
213
Success = success ,
214
214
ExtractStart = extractStart ,
215
215
ExtractEnd = extractEnd ,
216
- ComponentDependencies = componentDependencies ,
216
+ UsingDirectives = componentDependencies ,
217
217
TentativeVariableDependencies = variableDependencies ,
218
218
} ;
219
219
}
@@ -439,21 +439,21 @@ private static bool IsValidNode(SyntaxNode node, bool isCodeBlock)
439
439
return node is MarkupElementSyntax or MarkupTagHelperElementSyntax || ( isCodeBlock && node is CSharpCodeBlockSyntax ) ;
440
440
}
441
441
442
- private static HashSet < string > AddComponentDependenciesInRange ( SyntaxNode root , int extractStart , int extractEnd )
442
+ private static HashSet < string > AddUsingDirectivesInRange ( SyntaxNode root , int extractStart , int extractEnd )
443
443
{
444
- var dependencies = new HashSet < string > ( ) ;
444
+ var usings = new HashSet < string > ( ) ;
445
445
var extractSpan = new TextSpan ( extractStart , extractEnd - extractStart ) ;
446
446
447
447
// Only analyze nodes within the extract span
448
448
foreach ( var node in root . DescendantNodes ( ) . Where ( node => extractSpan . Contains ( node . Span ) ) )
449
449
{
450
450
if ( node is MarkupTagHelperElementSyntax { TagHelperInfo : { } tagHelperInfo } )
451
451
{
452
- AddDependenciesFromTagHelperInfo ( tagHelperInfo , dependencies ) ;
452
+ AddUsingFromTagHelperInfo ( tagHelperInfo , usings ) ;
453
453
}
454
454
}
455
455
456
- return dependencies ;
456
+ return usings ;
457
457
}
458
458
459
459
private static HashSet < string > AddVariableDependenciesInRange ( SyntaxNode root , int extractStart , int extractEnd )
@@ -478,7 +478,7 @@ private static HashSet<string> AddVariableDependenciesInRange(SyntaxNode root, i
478
478
return dependencies ;
479
479
}
480
480
481
- private static void AddDependenciesFromTagHelperInfo ( TagHelperInfo tagHelperInfo , HashSet < string > dependencies )
481
+ private static void AddUsingFromTagHelperInfo ( TagHelperInfo tagHelperInfo , HashSet < string > dependencies )
482
482
{
483
483
foreach ( var descriptor in tagHelperInfo . BindingResult . Descriptors )
484
484
{
@@ -508,9 +508,9 @@ private static void AddDependenciesFromTagHelperInfo(TagHelperInfo tagHelperInfo
508
508
509
509
var inst = PooledStringBuilder . GetInstance ( ) ;
510
510
var newFileContentBuilder = inst . Builder ;
511
- if ( selectionAnalysis . ComponentDependencies is not null )
511
+ if ( selectionAnalysis . UsingDirectives is not null )
512
512
{
513
- foreach ( var dependency in selectionAnalysis . ComponentDependencies )
513
+ foreach ( var dependency in selectionAnalysis . UsingDirectives )
514
514
{
515
515
newFileContentBuilder . AppendLine ( $ "@using { dependency } ") ;
516
516
}
@@ -750,40 +750,34 @@ private static string GeneratePromotedMethods(HashSet<MethodSymbolicInfo> method
750
750
builder . AppendLine ( $ "/// Delegate for the '{ method . Name } ' method.") ;
751
751
builder . AppendLine ( "/// </summary>" ) ;
752
752
builder . AppendLine ( "[Parameter]" ) ;
753
- builder . Append ( "public " ) ;
754
753
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" ) ;
763
757
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" )
765
760
{
766
- if ( method . ReturnType == "void" )
767
- {
768
- builder . Append ( '<' ) ;
769
- }
770
-
761
+ builder . Append ( "<" ) ;
771
762
builder . Append ( string . Join ( ", " , method . ParameterTypes ) ) ;
763
+
772
764
if ( method . ReturnType != "void" )
773
765
{
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 ) ;
775
772
}
776
- }
777
773
778
- if ( method . ReturnType != "void" )
779
- {
780
- builder . Append ( method . ReturnType ) ;
774
+ builder . Append ( '>' ) ;
781
775
}
782
776
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; }}") ;
785
778
if ( parameterCount < totalMethods - 1 )
786
779
{
780
+ // Space between methods except for the last method.
787
781
builder . AppendLine ( ) ;
788
782
builder . AppendLine ( ) ;
789
783
}
0 commit comments