public static async Task <Document> SimplifyLambdaExpressionAsync( Document document, LambdaExpressionSyntax lambda, CancellationToken cancellationToken) { SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken); LambdaExpressionSyntax newLambda = SimplifyLambdaExpressionRefactoring.SimplifyLambdaExpression(lambda) .WithTriviaFrom(lambda) .WithAdditionalAnnotations(Formatter.Annotation); SyntaxNode newRoot = oldRoot.ReplaceNode(lambda, newLambda); return(document.WithSyntaxRoot(newRoot)); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out BlockSyntax block)) { return; } CodeAction codeAction = CodeAction.Create( "Simplify lambda expression", cancellationToken => SimplifyLambdaExpressionRefactoring.RefactorAsync(context.Document, (LambdaExpressionSyntax)block.Parent, cancellationToken), GetEquivalenceKey(DiagnosticIdentifiers.SimplifyLambdaExpression)); context.RegisterCodeFix(codeAction, context.Diagnostics); }
private void AnalyzeLambdaExpression(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var lambda = (LambdaExpressionSyntax)context.Node; if (SimplifyLambdaExpressionRefactoring.CanRefactor(lambda)) { context.ReportDiagnostic( DiagnosticDescriptors.SimplifyLambdaExpression, lambda.Body.GetLocation()); FadeOut(context, (BlockSyntax)lambda.Body); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); BlockSyntax block = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <BlockSyntax>(); if (block == null) { return; } CodeAction codeAction = CodeAction.Create( "Simplify lambda expression", cancellationToken => SimplifyLambdaExpressionRefactoring.RefactorAsync(context.Document, (LambdaExpressionSyntax)block.Parent, cancellationToken), DiagnosticIdentifiers.SimplifyLambdaExpression + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); }
private static async Task <Document> ConvertAnonymousMethodToLambdaExpressionAsync( Document document, AnonymousMethodExpressionSyntax anonymousMethod, CancellationToken cancellationToken) { SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken); LambdaExpressionSyntax lambda = ParenthesizedLambdaExpression( anonymousMethod.AsyncKeyword, anonymousMethod.ParameterList, Token(SyntaxKind.EqualsGreaterThanToken), anonymousMethod.Block); lambda = SimplifyLambdaExpressionRefactoring.SimplifyLambdaExpression(lambda) .WithTriviaFrom(anonymousMethod) .WithAdditionalAnnotations(Formatter.Annotation); SyntaxNode newRoot = oldRoot.ReplaceNode(anonymousMethod, lambda); return(document.WithSyntaxRoot(newRoot)); }
private void AnalyzeLambdaExpression(SyntaxNodeAnalysisContext context) { var lambda = (LambdaExpressionSyntax)context.Node; if (SimplifyLambdaExpressionRefactoring.CanRefactor(lambda)) { CSharpSyntaxNode body = lambda.Body; context.ReportDiagnostic(DiagnosticDescriptors.SimplifyLambdaExpression, body.GetLocation()); var block = (BlockSyntax)body; context.FadeOutBraces(DiagnosticDescriptors.SimplifyLambdaExpressionFadeOut, block); StatementSyntax statement = block.Statements[0]; if (statement.IsKind(SyntaxKind.ReturnStatement)) { context.FadeOutToken(DiagnosticDescriptors.SimplifyLambdaExpressionFadeOut, ((ReturnStatementSyntax)statement).ReturnKeyword); } } }