public virtual Task <Document> InlineAsync( ExpressionStatementSyntax expressionStatement, SyntaxList <StatementSyntax> statements, CancellationToken cancellationToken = default(CancellationToken)) { int count = statements.Count; StatementSyntax[] newStatements = RewriteStatements(statements); newStatements[0] = newStatements[0].WithLeadingTrivia(expressionStatement.GetLeadingTrivia()); newStatements[count - 1] = newStatements[count - 1].WithTrailingTrivia(expressionStatement.GetTrailingTrivia()); StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(expressionStatement); if (statementsInfo.Success) { StatementListInfo newInfo = statementsInfo.WithStatements(statementsInfo.Statements.ReplaceRange(expressionStatement, newStatements)); return(Document.ReplaceNodeAsync(statementsInfo.Parent, newInfo.Parent, cancellationToken)); } else { return(Document.ReplaceNodeAsync(expressionStatement, Block(newStatements), cancellationToken)); } }
private static Task <Document> DuplicateStatementAsync( Document document, StatementSyntax statement, CancellationToken cancellationToken = default) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(statement); if (statementsInfo.Success) { int index = statementsInfo.Statements.IndexOf(statement); if (index == 0 && statementsInfo.Parent is BlockSyntax block && block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine()) { statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine()); } SyntaxList <StatementSyntax> newStatements = statementsInfo.Statements.Insert(index + 1, statement.WithNavigationAnnotation()); StatementListInfo newInfo = statementsInfo.WithStatements(newStatements); return(document.ReplaceNodeAsync(statementsInfo.Parent, newInfo.Parent, cancellationToken)); } else { SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement.WithNavigationAnnotation() }); BlockSyntax block = SyntaxFactory.Block(statements); return(document.ReplaceNodeAsync(statement, block, cancellationToken)); } }
public static Task <Document> RefactorAsync( Document document, UsingStatementSyntax usingStatement, CancellationToken cancellationToken = default) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(usingStatement); int index = statementsInfo.Statements.IndexOf(usingStatement); StatementListInfo newStatementsInfo = statementsInfo.RemoveAt(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()); } newStatementsInfo = newStatementsInfo.WithStatements(newStatementsInfo.Statements.InsertRange(index, statements)); return(document.ReplaceNodeAsync(statementsInfo.Parent, newStatementsInfo.Parent.WithFormatterAnnotation(), cancellationToken)); }
internal static Task <Document> ReplaceStatementsAsync( this Document document, StatementListInfo statementsInfo, SyntaxList <StatementSyntax> newStatements, CancellationToken cancellationToken = default(CancellationToken)) { return(document.ReplaceNodeAsync(statementsInfo.Parent, statementsInfo.WithStatements(newStatements).Parent, cancellationToken)); }
private static Task <Document> RefactorAsync( Document document, StatementListInfo statementsInfo, StatementSyntax statement, InitializerExpressionSyntax initializer, ExpressionSyntax initializedExpression, CancellationToken cancellationToken) { ExpressionStatementSyntax[] expressions = CreateExpressionStatements(initializer, initializedExpression).ToArray(); expressions[expressions.Length - 1] = expressions[expressions.Length - 1] .WithTrailingTrivia(statement.GetTrailingTrivia()); var objectCreationExpression = (ObjectCreationExpressionSyntax)initializer.Parent; ObjectCreationExpressionSyntax newObjectCreationExpression = objectCreationExpression.WithInitializer(null); if (newObjectCreationExpression.ArgumentList == null) { TypeSyntax type = newObjectCreationExpression.Type; newObjectCreationExpression = newObjectCreationExpression .WithArgumentList(ArgumentList().WithTrailingTrivia(type.GetTrailingTrivia())) .WithType(type.WithoutTrailingTrivia()); } SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(statement); StatementSyntax newStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression); SyntaxKind statementKind = statement.Kind(); if (statementKind == SyntaxKind.ExpressionStatement) { var expressionStatement = (ExpressionStatementSyntax)newStatement; newStatement = expressionStatement .WithExpression(expressionStatement.Expression.WithoutTrailingTrivia()); } else if (statementKind == SyntaxKind.LocalDeclarationStatement) { var localDeclaration = (LocalDeclarationStatementSyntax)newStatement; newStatement = localDeclaration .WithDeclaration(localDeclaration.Declaration.WithoutTrailingTrivia()); } SyntaxList <StatementSyntax> newStatements = statements.Replace(statement, newStatement); SyntaxNode newNode = statementsInfo .WithStatements(newStatements.InsertRange(index + 1, expressions)) .Parent .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(statementsInfo.Parent, newNode, cancellationToken)); }
/// <summary> /// Creates a new document with the specified statements replaced with new statements. /// </summary> /// <param name="document"></param> /// <param name="statementsInfo"></param> /// <param name="newStatements"></param> /// <param name="cancellationToken"></param> public static Task <Document> ReplaceStatementsAsync( this Document document, StatementListInfo statementsInfo, SyntaxList <StatementSyntax> newStatements, CancellationToken cancellationToken = default) { if (document == null) { throw new ArgumentNullException(nameof(document)); } return(document.ReplaceNodeAsync(statementsInfo.Parent, statementsInfo.WithStatements(newStatements).Parent, cancellationToken)); }
public virtual async Task <Solution> InlineAndRemoveAsync( ExpressionStatementSyntax expressionStatement, SyntaxList <StatementSyntax> statements, CancellationToken cancellationToken = default(CancellationToken)) { if (expressionStatement.SyntaxTree == Declaration.SyntaxTree) { DocumentEditor editor = await DocumentEditor.CreateAsync(Document, cancellationToken).ConfigureAwait(false); StatementSyntax[] newStatements = RewriteStatements(statements); int count = statements.Count; newStatements[0] = newStatements[0].WithLeadingTrivia(expressionStatement.GetLeadingTrivia()); newStatements[count - 1] = newStatements[count - 1].WithTrailingTrivia(expressionStatement.GetTrailingTrivia()); StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(expressionStatement); if (statementsInfo.Success) { StatementListInfo newStatementsInfo = statementsInfo.WithStatements(statementsInfo.Statements.ReplaceRange(expressionStatement, newStatements)); editor.ReplaceNode(statementsInfo.Parent, newStatementsInfo.Parent); } else { editor.ReplaceNode(expressionStatement, Block(newStatements)); } editor.RemoveNode(Declaration); return(editor.GetChangedDocument().Solution()); } else { Document newDocument = await InlineAsync(expressionStatement, statements, cancellationToken).ConfigureAwait(false); DocumentId documentId = Document.Solution().GetDocumentId(Declaration.SyntaxTree); newDocument = await newDocument.Solution().GetDocument(documentId).RemoveMemberAsync(Declaration, cancellationToken).ConfigureAwait(false); return(newDocument.Solution()); } }
private static SyntaxNode AddNextIf( StatementListInfo statementsInfo, IfStatementSyntax ifStatement, IfStatementSyntax newIfStatement, ExpressionSyntax expression) { IfStatementSyntax nextIfStatement = ifStatement.WithCondition(expression) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(ifStatement); SyntaxList <StatementSyntax> newStatements = statements .Replace(ifStatement, newIfStatement) .Insert(index + 1, nextIfStatement); return(statementsInfo.WithStatements(newStatements).Parent); }
private SyntaxNode Rewrite(StatementListInfo statementsInfo, IfStatementSyntax ifStatement) { SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(ifStatement); ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition, _semanticModel, _cancellationToken); if (_recursive) { ifStatement = (IfStatementSyntax)VisitIfStatement(ifStatement); } var block = (BlockSyntax)ifStatement.Statement; BlockSyntax newBlock = block.WithStatements(SingletonList(_jumpStatement)); if (!block .Statements .First() .GetLeadingTrivia() .Any(f => f.IsEndOfLineTrivia())) { newBlock = newBlock.WithCloseBraceToken(newBlock.CloseBraceToken.AppendToTrailingTrivia(NewLine())); } IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition) .WithStatement(newBlock) .WithFormatterAnnotation(); if (CSharpFacts.IsJumpStatementOrYieldBreakStatement(statements.Last().Kind()) && CSharpFacts.IsJumpStatementOrYieldBreakStatement(block.Statements.Last().Kind())) { statements = statements.RemoveAt(statements.Count - 1); } SyntaxList <StatementSyntax> newStatements = statements .ReplaceAt(index, newIfStatement) .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation())); return(statementsInfo.WithStatements(newStatements).Parent); }