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)); }
private static Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { VariableDeclaratorSyntax variableDeclarator = localDeclaration .Declaration .Variables .Single(); ExpressionSyntax expression = variableDeclarator .Initializer .Value .WalkDownParentheses(); AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(expression); PrefixUnaryExpressionSyntax newCondition = LogicalNotExpression( ParenthesizedExpression( IsPatternExpression( asExpressionInfo.Expression, DeclarationPattern( asExpressionInfo.Type, SingleVariableDesignation(variableDeclarator.Identifier))))); StatementListInfo statements = SyntaxInfo.StatementListInfo(localDeclaration); int index = statements.IndexOf(localDeclaration); var ifStatement = (IfStatementSyntax)statements[index + 1]; SyntaxTriviaList leadingTrivia = localDeclaration .DescendantTrivia(TextSpan.FromBounds(localDeclaration.SpanStart, ifStatement.SpanStart)) .ToSyntaxTriviaList() .EmptyIfWhitespace(); leadingTrivia = localDeclaration.GetLeadingTrivia().AddRange(leadingTrivia); StatementSyntax newStatement = ifStatement.Statement; if (ifStatement.SingleNonBlockStatementOrDefault() is ReturnStatementSyntax returnStatement && returnStatement.Expression?.WalkDownParentheses() is IdentifierNameSyntax identifierName && string.Equals(identifierName.Identifier.ValueText, variableDeclarator.Identifier.ValueText, System.StringComparison.Ordinal)) { newStatement = newStatement.ReplaceNode(returnStatement.Expression, NullLiteralExpression().WithTriviaFrom(returnStatement.Expression)); } IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition.WithTriviaFrom(ifStatement.Condition)) .WithStatement(newStatement) .WithLeadingTrivia(leadingTrivia) .WithFormatterAnnotation(); StatementListInfo newStatements = statements.RemoveAt(index).ReplaceAt(index, newIfStatement); return(document.ReplaceStatementsAsync(statements, newStatements, cancellationToken)); }
private static Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { VariableDeclaratorSyntax variableDeclarator = localDeclaration .Declaration .Variables .Single(); ExpressionSyntax expression = variableDeclarator .Initializer .Value .WalkDownParentheses(); AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(expression); PrefixUnaryExpressionSyntax newCondition = LogicalNotExpression( ParenthesizedExpression( IsPatternExpression( asExpressionInfo.Expression, DeclarationPattern( asExpressionInfo.Type, SingleVariableDesignation(variableDeclarator.Identifier))))); StatementListInfo statements = SyntaxInfo.StatementListInfo(localDeclaration); int index = statements.IndexOf(localDeclaration); var ifStatement = (IfStatementSyntax)statements[index + 1]; SyntaxTriviaList leadingTrivia = localDeclaration .DescendantTrivia(TextSpan.FromBounds(localDeclaration.SpanStart, ifStatement.SpanStart)) .ToSyntaxTriviaList() .EmptyIfWhitespace(); leadingTrivia = localDeclaration.GetLeadingTrivia().AddRange(leadingTrivia); IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition.WithTriviaFrom(ifStatement.Condition)) .WithLeadingTrivia(leadingTrivia); StatementListInfo newStatements = statements.RemoveAt(index).ReplaceAt(index, newIfStatement); return(document.ReplaceStatementsAsync(statements, newStatements, cancellationToken)); }