示例#1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            StatementContainer container,
            IfStatementSyntax ifStatement,
            ReturnStatementSyntax returnStatement,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax expression = ReplaceIfWithStatementRefactoring.GetExpression(
                ifStatement.Condition,
                ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement),
                returnStatement.Expression);

            ReturnStatementSyntax newReturnStatement = ReturnStatement(expression);

            SyntaxList <StatementSyntax> statements = container.Statements;

            int index = statements.IndexOf(ifStatement);

            newReturnStatement = newReturnStatement
                                 .WithLeadingTrivia(ifStatement.GetLeadingTrivia())
                                 .WithTrailingTrivia(returnStatement.GetTrailingTrivia())
                                 .WithFormatterAnnotation();

            SyntaxList <StatementSyntax> newStatements = statements
                                                         .RemoveAt(index)
                                                         .ReplaceAt(index, newReturnStatement);

            return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false));
        }
示例#2
0
        public static void ComputeRefactoring(RefactoringContext context, SelectedStatementCollection selectedStatements)
        {
            if (selectedStatements.Count == 2)
            {
                StatementSyntax[] statements = selectedStatements.ToArray();

                if (statements[0].IsKind(SyntaxKind.IfStatement) &&
                    statements[1].IsKind(SyntaxKind.ReturnStatement))
                {
                    var ifStatement = (IfStatementSyntax)statements[0];

                    if (!IfElseChain.IsPartOfChain(ifStatement))
                    {
                        ExpressionSyntax returnExpression = ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement);

                        if (returnExpression != null)
                        {
                            var returnStatement = (ReturnStatementSyntax)statements[1];
                            ExpressionSyntax returnExpression2 = returnStatement.Expression;

                            if (returnExpression2 != null)
                            {
                                const string title = "Replace if-return with return";

                                if (returnExpression.IsBooleanLiteralExpression() ||
                                    returnExpression2.IsBooleanLiteralExpression())
                                {
                                    context.RegisterRefactoring(
                                        title,
                                        cancellationToken => RefactorAsync(context.Document, selectedStatements.Container, ifStatement, returnStatement, cancellationToken));
                                }
                                else
                                {
                                    context.RegisterRefactoring(
                                        title,
                                        cancellationToken =>
                                    {
                                        return(RefactorAsync(
                                                   context.Document,
                                                   selectedStatements.Container,
                                                   ifStatement,
                                                   returnStatement,
                                                   returnExpression,
                                                   returnExpression2,
                                                   cancellationToken));
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }