private static WhileStatementSyntax ExtractExpressionToNestedIf( ExpressionSyntax expression, WhileStatementSyntax whileStatement, WhileStatementSyntax newWhileStatement) { if (newWhileStatement.Statement.IsKind(SyntaxKind.Block)) { var block = (BlockSyntax)newWhileStatement.Statement; IfStatementSyntax nestedIf = IfStatement( expression.WithoutTrivia(), Block(block.Statements)); return(newWhileStatement.ReplaceNode( block, block.WithStatements(SingletonList <StatementSyntax>(nestedIf)))); } else { IfStatementSyntax nestedIf = IfStatement( expression.WithoutTrivia(), whileStatement.Statement.WithoutTrivia()); BlockSyntax block = Block(nestedIf).WithTriviaFrom(whileStatement.Statement); return(newWhileStatement.WithStatement(block)); } }
public override SyntaxNode VisitWhileStatement(WhileStatementSyntax node) { var statement = (BlockSyntax)node.Statement; var line = GetLineNumber(statement.Statements.Any() ? statement.Statements.First() : statement) + 1; var updated = Block(new[] { ThrowIfCancelled.WithTrailingTrivia(GetLineDirective(line)) }.Concat(statement.Statements)); return(base.VisitWhileStatement(node.ReplaceNode(statement, updated))); }