public static Task <Document> RefactorAsync(
            Document document,
            UsingStatementSyntax usingStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementContainer container = StatementContainer.Create(usingStatement);

            int index = container.Statements.IndexOf(usingStatement);

            StatementContainer newContainer = container.RemoveStatementAt(index);

            var statements = new List <StatementSyntax>()
            {
                SyntaxFactory.LocalDeclarationStatement(usingStatement.Declaration)
            };

            statements.AddRange(GetStatements(usingStatement));

            if (statements.Count > 0)
            {
                statements[0] = statements[0]
                                .WithLeadingTrivia(usingStatement.GetLeadingTrivia());

                statements[statements.Count - 1] = statements[statements.Count - 1]
                                                   .WithTrailingTrivia(usingStatement.GetTrailingTrivia());
            }

            newContainer = newContainer.WithStatements(newContainer.Statements.InsertRange(index, statements));

            return(document.ReplaceNodeAsync(container.Node, newContainer.Node.WithFormatterAnnotation(), cancellationToken));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax returnExpression = ifStatement.Condition;

            if (GetBooleanLiteral(ifStatement.Statement).Kind() == SyntaxKind.FalseLiteralExpression)
            {
                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                returnExpression = CSharpUtility.LogicallyNegate(returnExpression, semanticModel, cancellationToken);
            }

            ReturnStatementSyntax newReturnStatement = ReturnStatement(
                ReturnKeyword().WithTrailingTrivia(Space),
                returnExpression,
                SemicolonToken());

            if (ifStatement.Else != null)
            {
                newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement);

                return(await document.ReplaceNodeAsync(ifStatement, newReturnStatement, cancellationToken).ConfigureAwait(false));
            }

            StatementContainer container = StatementContainer.Create(ifStatement);

            SyntaxList <StatementSyntax> statements = container.Statements;

            int index = statements.IndexOf(ifStatement);

            var returnStatement = (ReturnStatementSyntax)statements[index + 1];

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

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

            //TODO: ReplaceStatementsAsync
            return(await document.ReplaceNodeAsync(container.Node, container.WithStatements(newStatements).Node, cancellationToken).ConfigureAwait(false));
        }
Пример #3
0
        public static Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken)
        {
            StatementContainer container = StatementContainer.Create(statement);

            SyntaxList <StatementSyntax> statements = container.Statements;

            var returnStatement = (ReturnStatementSyntax)statements.Last(f => !f.IsKind(SyntaxKind.LocalFunctionStatement));

            int returnStatementIndex = statements.IndexOf(returnStatement);

            ExpressionSyntax expression = returnStatement.Expression;

            SyntaxKind kind = statement.Kind();

            if (kind == SyntaxKind.IfStatement)
            {
                var ifStatement = (IfStatementSyntax)statement;

                IfStatement chain = Syntax.IfStatement.Create(ifStatement);

                ExpressionStatementSyntax[] expressionStatements = chain
                                                                   .Nodes
                                                                   .Select(ifOrElse => (ExpressionStatementSyntax)GetLastStatementOrDefault(ifOrElse.Statement))
                                                                   .ToArray();

                IfStatementSyntax newIfStatement = ifStatement.ReplaceNodes(
                    expressionStatements,
                    (f, g) =>
                {
                    var assignment = (AssignmentExpressionSyntax)f.Expression;

                    return(ReturnStatement(assignment.Right).WithTriviaFrom(f));
                });

                SyntaxList <StatementSyntax> newStatements = statements.Replace(ifStatement, newIfStatement);

                StatementContainer newContainer = container.WithStatements(newStatements);

                SyntaxNode newNode = newContainer.Node;

                if (chain.EndsWithElse)
                {
                    newNode = newNode.RemoveStatement(newContainer.Statements[returnStatementIndex]);
                }

                return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken));
            }
            else if (kind == SyntaxKind.SwitchStatement)
            {
                var switchStatement = (SwitchStatementSyntax)statement;

                SyntaxList <SwitchSectionSyntax> newSections = switchStatement
                                                               .Sections
                                                               .Select(section => CreateNewSection(section))
                                                               .ToSyntaxList();

                SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections);

                SyntaxList <StatementSyntax> newStatements = statements.Replace(switchStatement, newSwitchStatement);

                StatementContainer newContainer = container.WithStatements(newStatements);

                SyntaxNode newNode = newContainer.Node;

                if (switchStatement.Sections.Any(f => f.ContainsDefaultLabel()))
                {
                    newNode = newNode.RemoveStatement(newContainer.Statements[returnStatementIndex]);
                }

                return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken));
            }

            Debug.Fail(statement.Kind().ToString());

            return(Task.FromResult(document));
        }