From 6740f0b02b1fb525d07a190df81fd6f1bc19d93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Wed, 22 Feb 2023 19:07:13 -0500 Subject: [PATCH] Add support for static lambda and delegate --- .../CodeActions/SwitchToAnsiConsoleAction.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Spectre.Console.Analyzer/Fixes/CodeActions/SwitchToAnsiConsoleAction.cs b/src/Spectre.Console.Analyzer/Fixes/CodeActions/SwitchToAnsiConsoleAction.cs index d6802bcbc..6a0833d19 100644 --- a/src/Spectre.Console.Analyzer/Fixes/CodeActions/SwitchToAnsiConsoleAction.cs +++ b/src/Spectre.Console.Analyzer/Fixes/CodeActions/SwitchToAnsiConsoleAction.cs @@ -75,7 +75,9 @@ protected override async Task GetChangedDocumentAsync(CancellationToke // int local = 0; // static void LocalFunction() => local; <-- local is invalid here but LookupSymbols suggests it // } - if (symbol.Kind is SymbolKind.Local) + // + // Parameters from the ancestor methods or local functions are also returned even if the operation is in a static local function. + if (symbol.Kind is SymbolKind.Local or SymbolKind.Parameter) { var localPosition = symbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax(cancellationToken).GetLocation().SourceSpan.Start; @@ -167,6 +169,24 @@ private static bool IsInStaticContext(IOperation operation, CancellationToken ca return true; } } + else if (member is LambdaExpressionSyntax lambdaExpression) + { + var symbol = operation.SemanticModel!.GetSymbolInfo(lambdaExpression, cancellationToken).Symbol; + if (symbol != null && symbol.IsStatic) + { + parentStaticMemberStartPosition = lambdaExpression.GetLocation().SourceSpan.Start; + return true; + } + } + else if (member is AnonymousMethodExpressionSyntax anonymousMethod) + { + var symbol = operation.SemanticModel!.GetSymbolInfo(anonymousMethod, cancellationToken).Symbol; + if (symbol != null && symbol.IsStatic) + { + parentStaticMemberStartPosition = anonymousMethod.GetLocation().SourceSpan.Start; + return true; + } + } else if (member is MethodDeclarationSyntax methodDeclaration) { parentStaticMemberStartPosition = methodDeclaration.GetLocation().SourceSpan.Start;