Skip to content

Commit 6740f0b

Browse files
meziantouphil-scott-78
authored andcommitted
Add support for static lambda and delegate
1 parent f7f99ec commit 6740f0b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Spectre.Console.Analyzer/Fixes/CodeActions/SwitchToAnsiConsoleAction.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
7575
// int local = 0;
7676
// static void LocalFunction() => local; <-- local is invalid here but LookupSymbols suggests it
7777
// }
78-
if (symbol.Kind is SymbolKind.Local)
78+
//
79+
// Parameters from the ancestor methods or local functions are also returned even if the operation is in a static local function.
80+
if (symbol.Kind is SymbolKind.Local or SymbolKind.Parameter)
7981
{
8082
var localPosition = symbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax(cancellationToken).GetLocation().SourceSpan.Start;
8183

@@ -167,6 +169,24 @@ private static bool IsInStaticContext(IOperation operation, CancellationToken ca
167169
return true;
168170
}
169171
}
172+
else if (member is LambdaExpressionSyntax lambdaExpression)
173+
{
174+
var symbol = operation.SemanticModel!.GetSymbolInfo(lambdaExpression, cancellationToken).Symbol;
175+
if (symbol != null && symbol.IsStatic)
176+
{
177+
parentStaticMemberStartPosition = lambdaExpression.GetLocation().SourceSpan.Start;
178+
return true;
179+
}
180+
}
181+
else if (member is AnonymousMethodExpressionSyntax anonymousMethod)
182+
{
183+
var symbol = operation.SemanticModel!.GetSymbolInfo(anonymousMethod, cancellationToken).Symbol;
184+
if (symbol != null && symbol.IsStatic)
185+
{
186+
parentStaticMemberStartPosition = anonymousMethod.GetLocation().SourceSpan.Start;
187+
return true;
188+
}
189+
}
170190
else if (member is MethodDeclarationSyntax methodDeclaration)
171191
{
172192
parentStaticMemberStartPosition = methodDeclaration.GetLocation().SourceSpan.Start;

0 commit comments

Comments
 (0)