public static InitializerExpressionSyntax ToMultiLine(InitializerExpressionSyntax initializer, CancellationToken cancellationToken) { SyntaxNode parent = initializer.Parent; if (parent.IsKind(SyntaxKind.ObjectCreationExpression) && !initializer.IsKind(SyntaxKind.CollectionInitializerExpression)) { return(initializer .WithExpressions( SeparatedList( initializer.Expressions.Select(expression => expression.WithLeadingTrivia(NewLine()))))); } else { SyntaxTrivia trivia = initializer.GetIndentation(cancellationToken); SyntaxTriviaList braceTrivia = TriviaList(NewLine(), trivia); SyntaxTriviaList expressionTrivia = TriviaList(NewLine(), trivia, SingleIndentation(trivia)); return(initializer .WithExpressions( SeparatedList( initializer.Expressions.Select(expression => expression.WithLeadingTrivia(expressionTrivia)))) .WithOpenBraceToken(initializer.OpenBraceToken.WithLeadingTrivia(braceTrivia)) .WithCloseBraceToken(initializer.CloseBraceToken.WithLeadingTrivia(braceTrivia))); } }
public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node) { if (!node.DescendantTrivia().All(SyntaxUtils.IsWhitespace)) { return(node); // nothing to do here } if (node.IsKind(SyntaxKind.ComplexElementInitializerExpression)) { return(VisitElementInitializer(node)); } if (!node.IsKind(SyntaxKind.CollectionInitializerExpression) && !node.IsKind(SyntaxKind.ArrayInitializerExpression)) { return(node); } var indent = node.GetIndentation(); var exprList = node.Expressions; SyntaxNodeOrToken prevNode = node.OpenBraceToken; foreach (var nodeOrToken in exprList.GetWithSeparators()) { if (nodeOrToken.IsNode && prevNode.GetTrailingTrivia().Any(SyntaxKind.EndOfLineTrivia)) { var tok = nodeOrToken.AsNode().GetFirstToken(); AddChange(tok, tok.WithLeadingWhitespace(indent + '\t')); } prevNode = nodeOrToken; } if (prevNode.GetTrailingTrivia().Any(SyntaxKind.EndOfLineTrivia)) { AddChange(node.CloseBraceToken, node.CloseBraceToken.WithLeadingWhitespace(indent)); } return(base.VisitInitializerExpression(node)); }