public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => f.IsKind( SyntaxKind.TrueLiteralExpression, SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression, SyntaxKind.LogicalAndExpression, SyntaxKind.LogicalOrExpression))) { return; } switch (node.Kind()) { case SyntaxKind.TrueLiteralExpression: { RegisterCodeFix( context, node.ToString(), cancellationToken => { return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync( context.Document, (ForStatementSyntax)node.Parent, cancellationToken)); }); break; } case SyntaxKind.EqualsExpression: case SyntaxKind.NotEqualsExpression: case SyntaxKind.LogicalAndExpression: case SyntaxKind.LogicalOrExpression: { var binaryExpression = (BinaryExpressionSyntax)node; TextSpan span = RemoveRedundantBooleanLiteralRefactoring.GetSpanToRemove(binaryExpression, binaryExpression.Left, binaryExpression.Right); RegisterCodeFix( context, binaryExpression.ToString(span), cancellationToken => { return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync( context.Document, binaryExpression, cancellationToken)); }); break; } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); SyntaxNode node = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf(f => f.IsKind( SyntaxKind.TrueLiteralExpression, SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression, SyntaxKind.LogicalAndExpression, SyntaxKind.LogicalOrExpression)); Debug.Assert(node != null, $"{nameof(node)} is null"); if (node == null) { return; } switch (node.Kind()) { case SyntaxKind.TrueLiteralExpression: { RegisterCodeFix( context, node.ToString(), cancellationToken => { return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync( context.Document, (ForStatementSyntax)node.Parent, cancellationToken)); }); break; } case SyntaxKind.EqualsExpression: case SyntaxKind.NotEqualsExpression: case SyntaxKind.LogicalAndExpression: case SyntaxKind.LogicalOrExpression: { var binaryExpression = (BinaryExpressionSyntax)node; TextSpan span = RemoveRedundantBooleanLiteralRefactoring.GetSpanToRemove(binaryExpression, binaryExpression.Left, binaryExpression.Right); RegisterCodeFix( context, binaryExpression.ToString(span), cancellationToken => { return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync( context.Document, binaryExpression, cancellationToken)); }); break; } } }