public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            LiteralExpressionSyntax literalExpression = root
                                                        .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                        .FirstAncestorOrSelf <LiteralExpressionSyntax>();

            Debug.Assert(literalExpression != null, $"{nameof(literalExpression)} is null");

            if (literalExpression == null)
            {
                return;
            }

            Debug.Assert(literalExpression.IsBooleanLiteralExpression(), literalExpression.Kind().ToString());

            if (!literalExpression.IsBooleanLiteralExpression())
            {
                return;
            }

            SyntaxNode parent = literalExpression.Parent;

            switch (parent?.Kind())
            {
            case SyntaxKind.ForStatement:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (ForStatementSyntax)parent,
                                   cancellationToken));
                    });

                break;
            }

            case SyntaxKind.LogicalAndExpression:
            case SyntaxKind.LogicalOrExpression:
            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (BinaryExpressionSyntax)parent,
                                   cancellationToken));
                    });

                break;
            }
            }
        }
예제 #2
0
        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 = RemoveRedundantBooleanLiteralAnalysis.GetSpanToRemove(binaryExpression, binaryExpression.Left, binaryExpression.Right);

                RegisterCodeFix(
                    context,
                    binaryExpression.ToString(span),
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   binaryExpression,
                                   cancellationToken));
                    });

                break;
            }
            }
        }
예제 #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).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,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (ForStatementSyntax)node.Parent,
                                   cancellationToken));
                    });

                break;
            }

            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            case SyntaxKind.LogicalAndExpression:
            case SyntaxKind.LogicalOrExpression:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (BinaryExpressionSyntax)node,
                                   cancellationToken));
                    });

                break;
            }
            }
        }
예제 #4
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            BinaryExpressionSyntax binaryExpression = root
                                                      .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                      .FirstAncestorOrSelf <BinaryExpressionSyntax>();

            if (binaryExpression == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantBooleanLiteral:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant boolean literal",
                        cancellationToken => RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);

                    break;
                }

                case DiagnosticIdentifiers.SimplifyBooleanComparison:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Simplify boolean comparison",
                        cancellationToken => SimplifyBooleanComparisonRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);

                    break;
                }

                case DiagnosticIdentifiers.UseAnyMethodInsteadOfCountMethod:
                {
                    UseAnyMethodInsteadOfCountMethodCodeFix.Register(context, diagnostic, binaryExpression);
                    break;
                }
                }
            }
        }