Exemple #1
0
        static IfStatementSyntax GenerateInvertedIfStatement(IfStatementSyntax ifStatement)
        {
            var condition = VBUtil.InvertCondition(ifStatement.Condition);

            return(SyntaxFactory.IfStatement(condition)
                   .WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword))
                   .WithAdditionalAnnotations(Formatter.Annotation));
        }
Exemple #2
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;

            if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
            {
                return;
            }
            var span = context.Span;

            if (!span.IsEmpty)
            {
                return;
            }
            var cancellationToken = context.CancellationToken;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (model.IsFromGeneratedCode(cancellationToken))
            {
                return;
            }
            var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax expr;
            SyntaxToken      token;

            if (!GetRelationalExpression(root, span, out expr, out token))
            {
                return;
            }
            if (expr.IsKind(SyntaxKind.NotExpression))
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        span,
                        DiagnosticSeverity.Info,
                        string.Format(GettextCatalog.GetString("Invert '{0}'"), expr),
                        t2 =>
                {
                    var uOp           = expr as UnaryExpressionSyntax;
                    var convertedExpr = VBUtil.InvertCondition(uOp.Operand.SkipParens()).WithAdditionalAnnotations(Formatter.Annotation);
                    var newRoot       = root.ReplaceNode(
                        expr,
                        VBUtil.InvertCondition(uOp.Operand.SkipParens()).WithAdditionalAnnotations(Formatter.Annotation)
                        );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
            }
            else if (expr.Parent is ParenthesizedExpressionSyntax && expr.Parent.Parent is UnaryExpressionSyntax)
            {
                var unaryOperatorExpression = expr.Parent.Parent as UnaryExpressionSyntax;
                if (unaryOperatorExpression.IsKind(SyntaxKind.NotExpression))
                {
                    context.RegisterRefactoring(
                        CodeActionFactory.Create(
                            span,
                            DiagnosticSeverity.Info,
                            string.Format(GettextCatalog.GetString("Invert '{0}'"), unaryOperatorExpression),
                            t2 =>
                    {
                        //var uOp = expr as PrefixUnaryExpressionSyntax;
                        var newRoot = root.ReplaceNode(
                            unaryOperatorExpression,
                            VBUtil.InvertCondition(expr).WithAdditionalAnnotations(Formatter.Annotation)
                            );
                        return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                    }
                            )
                        );
                }
            }
            else
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        span,
                        DiagnosticSeverity.Info,
                        string.Format(GettextCatalog.GetString("Invert '{0}'"), expr),
                        t2 =>
                {
                    var newRoot = root.ReplaceNode((SyntaxNode)
                                                   expr,
                                                   SyntaxFactory.UnaryExpression(
                                                       SyntaxKind.NotExpression,
                                                       SyntaxFactory.Token(SyntaxKind.NotKeyword),
                                                       SyntaxFactory.ParenthesizedExpression(VBUtil.InvertCondition(expr))
                                                       ).WithAdditionalAnnotations(Formatter.Annotation)
                                                   );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
            }
        }
Exemple #3
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;

            if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
            {
                return;
            }
            var span = context.Span;

            if (!span.IsEmpty)
            {
                return;
            }
            var cancellationToken = context.CancellationToken;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (model.IsFromGeneratedCode(cancellationToken))
            {
                return;
            }
            var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var complexIfBlock = GetMultiLineIfBlockComplex(root, span);

            if (complexIfBlock != null)
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        span,
                        DiagnosticSeverity.Info,
                        invertIfFixMessage,
                        t2 =>
                {
                    var newRoot = root.ReplaceNode(
                        complexIfBlock,
                        GenerateNewScript(complexIfBlock)
                        );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
                return;
            }

            var simpleIfBlock = GetMultiLineIfBlockSimple(root, span);

            if (simpleIfBlock != null)
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        span,
                        DiagnosticSeverity.Info,
                        invertIfFixMessage,
                        t2 =>
                {
                    var newRoot = root.ReplaceNode(
                        simpleIfBlock,
                        simpleIfBlock
                        .WithIfStatement(simpleIfBlock.IfStatement.WithCondition(VBUtil.InvertCondition(simpleIfBlock.IfStatement.Condition)))
                        .WithStatements(simpleIfBlock.ElseBlock.Statements)
                        .WithElseBlock(simpleIfBlock.ElseBlock.WithStatements(simpleIfBlock.Statements))
                        .WithAdditionalAnnotations(Formatter.Annotation)
                        );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
                return;
            }

            var inSubIfBlock = GetMultiLineIfBlockLastInSub(root, span);

            if (inSubIfBlock != null)
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        inSubIfBlock.Span,
                        DiagnosticSeverity.Info,
                        invertIfFixMessage,
                        t2 =>
                {
                    var ifStatement     = GenerateInvertedIfStatement(inSubIfBlock.IfStatement);
                    var invertedIfBlock = SyntaxFactory.MultiLineIfBlock(ifStatement)
                                          .WithStatements(new SyntaxList <StatementSyntax>().Add(SyntaxFactory.ReturnStatement()))
                                          .WithLeadingTrivia(inSubIfBlock.GetLeadingTrivia())
                                          .WithTrailingTrivia(inSubIfBlock.GetTrailingTrivia())
                                          .WithAdditionalAnnotations(Formatter.Annotation);
                    var newRoot = root.ReplaceNode(
                        inSubIfBlock,
                        new SyntaxNode[] { invertedIfBlock }.Concat(GetStatements(inSubIfBlock))
                        );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
            }


            var inLoopIfBlock = GetMultiLineIfBlockInLoop(root, span);

            if (inLoopIfBlock != null)
            {
                context.RegisterRefactoring(
                    CodeActionFactory.Create(
                        inLoopIfBlock.Span,
                        DiagnosticSeverity.Info,
                        invertIfFixMessage,
                        t2 =>
                {
                    var ifStatement     = GenerateInvertedIfStatement(inLoopIfBlock.IfStatement);
                    var invertedIfBlock = SyntaxFactory.MultiLineIfBlock(ifStatement)
                                          .WithStatements(new SyntaxList <StatementSyntax>().Add(GetContinueStatement(inLoopIfBlock.Parent)))
                                          .WithLeadingTrivia(inLoopIfBlock.GetLeadingTrivia())
                                          .WithTrailingTrivia(inLoopIfBlock.GetTrailingTrivia())
                                          .WithAdditionalAnnotations(Formatter.Annotation);
                    var newRoot = root.ReplaceNode(
                        inLoopIfBlock,
                        new SyntaxNode[] { invertedIfBlock }.Concat(GetStatements(inLoopIfBlock))
                        );
                    return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
                }
                        )
                    );
                return;
            }
        }