private static Task <Document> DuplicateStatementAsync( Document document, StatementSyntax statement, CancellationToken cancellationToken = default(CancellationToken)) { StatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { int index = container.Statements.IndexOf(statement); if (index == 0 && container.IsBlock && container.Block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine()) { statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine()); } SyntaxList <StatementSyntax> newStatements = container.Statements.Insert(index + 1, statement); SyntaxNode newNode = container.NodeWithStatements(newStatements); return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken)); } else { SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement }); BlockSyntax block = SyntaxFactory.Block(statements); return(document.ReplaceNodeAsync(statement, block, cancellationToken)); } }
private static Task <Document> DuplicateStatementAsync( Document document, StatementSyntax statement, CancellationToken cancellationToken = default(CancellationToken)) { StatementsInfo statementsInfo = SyntaxInfo.StatementsInfo(statement); if (statementsInfo.Success) { int index = statementsInfo.Statements.IndexOf(statement); if (index == 0 && statementsInfo.IsInBlock && statementsInfo.Block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine()) { statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine()); } SyntaxList <StatementSyntax> newStatements = statementsInfo.Statements.Insert(index + 1, statement.WithNavigationAnnotation()); StatementsInfo newInfo = statementsInfo.WithStatements(newStatements); return(document.ReplaceNodeAsync(statementsInfo.Node, newInfo.Node, cancellationToken)); } else { SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement.WithNavigationAnnotation() }); BlockSyntax block = SyntaxFactory.Block(statements); return(document.ReplaceNodeAsync(statement, block, cancellationToken)); } }
private static async Task <Document> DuplicateStatementAsync( Document document, StatementSyntax statement, CancellationToken cancellationToken = default(CancellationToken)) { var block = (BlockSyntax)statement.Parent; int index = block.Statements.IndexOf(statement); if (index == 0 && block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine()) { statement = statement.WithLeadingTrivia(statement.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia())); } BlockSyntax newBlock = block.WithStatements(block.Statements.Insert(index + 1, statement)); return(await document.ReplaceNodeAsync(block, newBlock, cancellationToken).ConfigureAwait(false)); }