public static SyntaxTriviaList FixSingleLineCommentSpacing(SyntaxTriviaList list) { var parts = list.SplitBy(SyntaxKind.SingleLineCommentTrivia, true); if (parts.Count == 1) { return(parts[0]); } var result = SyntaxFactory.TriviaList(); return(result .AddRange(parts[0]) .AddRange(parts.Skip(1).SelectMany(x => x.RemoveConsecutiveEndLines()).ToList())); }