Example #1
0
        private static async Task <Document> RefactorAsync(
            Document document,
            List <LocalDeclarationStatementSyntax> localDeclarations,
            List <ExpressionSyntax> expressions,
            WhileStatementSyntax whileStatement,
            CancellationToken cancellationToken)
        {
            var declaration  = default(VariableDeclarationSyntax);
            var initializers = default(SeparatedSyntaxList <ExpressionSyntax>);
            var incrementors = default(SeparatedSyntaxList <ExpressionSyntax>);

            if (localDeclarations != null)
            {
                IEnumerable <VariableDeclarationSyntax> declarations = localDeclarations
                                                                       .Select(f => f.Declaration)
                                                                       .Where(f => f != null);

                TypeSyntax type = declarations.First().Type.TrimTrivia();

                IEnumerable <VariableDeclaratorSyntax> variables = declarations
                                                                   .SelectMany(f => f.Variables)
                                                                   .Select(f => f.TrimTrivia());

                declaration = VariableDeclaration(type, SeparatedList(variables));

                StatementContainer container;
                if (StatementContainer.TryCreate(whileStatement, out container))
                {
                    SyntaxList <StatementSyntax> statements = container.Statements;

                    int index = statements.IndexOf(localDeclarations[0]);

                    ForStatementSyntax forStatement = CreateForStatement(whileStatement, declaration, initializers, incrementors);

                    forStatement = forStatement
                                   .TrimLeadingTrivia()
                                   .PrependToLeadingTrivia(localDeclarations[0].GetLeadingTrivia());

                    IEnumerable <StatementSyntax> newStatements = statements.Take(index)
                                                                  .Concat(new ForStatementSyntax[] { forStatement })
                                                                  .Concat(statements.Skip(index + localDeclarations.Count + 1));

                    return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false));
                }
            }
            else if (expressions != null)
            {
                initializers = SeparatedList(expressions);
            }

            return(await document.ReplaceNodeAsync(
                       whileStatement,
                       CreateForStatement(whileStatement, declaration, initializers, incrementors),
                       cancellationToken).ConfigureAwait(false));
        }
        private static Task <Document> RefactorAsync <TNode>(
            Document document,
            WhileStatementSyntax whileStatement,
            ForStatementSyntax forStatement,
            List <TNode> list,
            CancellationToken cancellationToken) where TNode : StatementSyntax
        {
            forStatement = forStatement
                           .TrimLeadingTrivia()
                           .PrependToLeadingTrivia(list[0].GetLeadingTrivia());

            StatementContainer container = StatementContainer.Create(whileStatement);

            SyntaxList <StatementSyntax> statements = container.Statements;

            int index = statements.IndexOf(list[0]);

            IEnumerable <StatementSyntax> newStatements = statements.Take(index)
                                                          .Concat(new ForStatementSyntax[] { forStatement })
                                                          .Concat(statements.Skip(index + list.Count + 1));

            return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken));
        }
Example #3
0
        private static Task <Document> RefactorAsync <TNode>(
            Document document,
            WhileStatementSyntax whileStatement,
            ForStatementSyntax forStatement,
            List <TNode> list,
            CancellationToken cancellationToken) where TNode : StatementSyntax
        {
            forStatement = forStatement
                           .TrimLeadingTrivia()
                           .PrependToLeadingTrivia(list[0].GetLeadingTrivia());

            StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(whileStatement);

            SyntaxList <StatementSyntax> statements = statementsInfo.Statements;

            int index = statements.IndexOf(list[0]);

            IEnumerable <StatementSyntax> newStatements = statements.Take(index)
                                                          .Concat(new ForStatementSyntax[] { forStatement })
                                                          .Concat(statements.Skip(index + list.Count + 1));

            return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken));
        }