public static Task <Document> RefactorAsync( Document document, ConditionalExpressionSyntax conditionalExpression, CancellationToken cancellationToken) { ExpressionSyntax condition = conditionalExpression.Condition; ExpressionSyntax whenTrue = conditionalExpression.WhenTrue; ExpressionSyntax whenFalse = conditionalExpression.WhenFalse; SyntaxToken questionToken = conditionalExpression.QuestionToken; SyntaxToken colonToken = conditionalExpression.ColonToken; var writer = new NodeWriter(conditionalExpression); writer.WriteLeadingTrivia(); writer.WriteSpan(condition); Write(condition, whenTrue, questionToken, "? ", writer); Write(whenTrue, whenFalse, colonToken, ": ", writer); writer.WriteTrailingTrivia(); ExpressionSyntax newNode = SyntaxFactory.ParseExpression(writer.ToString()); return(document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken)); }
private static void Write( ExpressionSyntax expression, ExpressionSyntax nextExpression, SyntaxToken token, string newText, NodeWriter writer) { if (IsFixable(expression, token)) { if (expression.GetTrailingTrivia().Any(f => !f.IsWhitespaceOrEndOfLineTrivia()) || token.LeadingTrivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { writer.WriteTrailingTrivia(expression); writer.WriteLeadingTrivia(token); } writer.WriteTrailingTrivia(token); writer.WriteLeadingTrivia(nextExpression); writer.Write(newText); writer.WriteSpan(nextExpression); } else { writer.WriteTrailingTrivia(expression); writer.WriteFullSpan(token); writer.WriteLeadingTriviaAndSpan(nextExpression); } }