private static InitializerExpressionSyntax RemoveTrailingComma(InitializerExpressionSyntax initializer) { SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; SyntaxToken trailingComma = expressions.GetTrailingSeparator(); SeparatedSyntaxList <ExpressionSyntax> newExpressions = expressions.ReplaceSeparator( trailingComma, SyntaxFactory.MissingToken(SyntaxKind.CommaToken)); int lastIndex = expressions.Count - 1; SyntaxTriviaList newTrailingTrivia = expressions[lastIndex] .GetTrailingTrivia() .AddRange(trailingComma.LeadingTrivia) .AddRange(trailingComma.TrailingTrivia); ExpressionSyntax newExpression = newExpressions[lastIndex].WithTrailingTrivia(newTrailingTrivia); newExpressions = newExpressions.ReplaceAt(lastIndex, newExpression); return(initializer.WithExpressions(newExpressions)); }
public static Task <Document> ToSingleLineAsync( Document document, InitializerExpressionSyntax initializer, bool removeTrailingComma = false, CancellationToken cancellationToken = default(CancellationToken)) { InitializerExpressionSyntax newInitializer = initializer .ReplaceWhitespace(ElasticSpace, TextSpan.FromBounds(initializer.Span.Start, initializer.Span.End)) .WithFormatterAnnotation(); newInitializer = newInitializer.WithOpenBraceToken(newInitializer.OpenBraceToken.WithoutLeadingTrivia().WithTrailingTrivia(Space)); newInitializer = newInitializer.WithCloseBraceToken(newInitializer.CloseBraceToken.WithoutLeadingTrivia()); SeparatedSyntaxList <ExpressionSyntax> expressions = newInitializer.Expressions; if (expressions.Any()) { ExpressionSyntax firstExpression = expressions.First(); newInitializer = newInitializer.WithExpressions(expressions.Replace(firstExpression, firstExpression.WithoutLeadingTrivia())); expressions = newInitializer.Expressions; SyntaxToken trailingComma = expressions.GetTrailingSeparator(); if (trailingComma.IsKind(SyntaxKind.CommaToken)) { if (removeTrailingComma) { expressions = expressions.ReplaceSeparator(trailingComma, MissingToken(SyntaxKind.CommaToken)); ExpressionSyntax lastExpression = expressions.Last(); expressions = expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space)); newInitializer = newInitializer.WithExpressions(expressions); } else { newInitializer = newInitializer.WithExpressions(expressions.ReplaceSeparator(trailingComma, trailingComma.WithTrailingTrivia(Space))); } } else { ExpressionSyntax lastExpression = expressions.Last(); newInitializer = newInitializer.WithExpressions(expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space))); } } SyntaxNode parent = initializer.Parent; SyntaxNode newParent; switch (parent.Kind()) { case SyntaxKind.ObjectCreationExpression: { var expression = (ObjectCreationExpressionSyntax)parent; expression = expression.WithInitializer(newInitializer); ArgumentListSyntax argumentList = expression.ArgumentList; if (argumentList != null) { newParent = expression.WithArgumentList(argumentList.WithTrailingTrivia(Space)); } else { newParent = expression.WithType(expression.Type.WithTrailingTrivia(Space)); } break; } case SyntaxKind.ArrayCreationExpression: { var expression = (ArrayCreationExpressionSyntax)parent; newParent = expression .WithInitializer(newInitializer) .WithType(expression.Type.WithTrailingTrivia(Space)); break; } case SyntaxKind.ImplicitArrayCreationExpression: { var expression = (ImplicitArrayCreationExpressionSyntax)parent; newParent = expression .WithInitializer(newInitializer) .WithCloseBracketToken(expression.CloseBracketToken.WithTrailingTrivia(Space)); break; } case SyntaxKind.EqualsValueClause: { var equalsValueClause = (EqualsValueClauseSyntax)parent; newParent = equalsValueClause .WithValue(newInitializer) .WithEqualsToken(equalsValueClause.EqualsToken.WithTrailingTrivia(Space)); break; } case SyntaxKind.SimpleAssignmentExpression: { var simpleAssignment = (AssignmentExpressionSyntax)parent; newParent = simpleAssignment .WithRight(newInitializer) .WithOperatorToken(simpleAssignment.OperatorToken.WithTrailingTrivia(Space)); break; } default: { Debug.Fail(parent.Kind().ToString()); return(document.ReplaceNodeAsync(initializer, newInitializer, cancellationToken)); } } return(document.ReplaceNodeAsync(parent, newParent, cancellationToken)); }