public static Task <Document> RefactorAsync( Document document, ForStatementSyntax forStatement, CancellationToken cancellationToken) { var statements = new List <StatementSyntax>(); VariableDeclarationSyntax declaration = forStatement.Declaration; if (declaration != null) { statements.Add(LocalDeclarationStatement(declaration)); } else { foreach (ExpressionSyntax initializer in forStatement.Initializers) { statements.Add(ExpressionStatement(initializer)); } } StatementSyntax statement = forStatement.Statement ?? Block(); SeparatedSyntaxList <ExpressionSyntax> incrementors = forStatement.Incrementors; if (incrementors.Any()) { if (!statement.IsKind(SyntaxKind.Block)) { statement = Block(statement); } ExpressionStatementSyntax[] incrementorStatements = incrementors .Select(f => ExpressionStatement(f).WithFormatterAnnotation()) .ToArray(); var rewriter = new InsertIncrementorsBeforeContinueRewriter(incrementorStatements); statement = (StatementSyntax)rewriter.Visit(statement); statement = ((BlockSyntax)statement).AddStatements(incrementorStatements); } statements.Add(WhileStatement(forStatement.Condition ?? TrueLiteralExpression(), statement)); statements[0] = statements[0].WithLeadingTrivia(forStatement.GetLeadingTrivia()); if (forStatement.IsEmbedded()) { return(document.ReplaceNodeAsync(forStatement, Block(statements), cancellationToken)); } else { return(document.ReplaceNodeAsync(forStatement, statements, cancellationToken)); } }