diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore_TopLevel.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore_TopLevel.expected.test index 8ca05435c..4b2a724f1 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore_TopLevel.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore_TopLevel.expected.test @@ -13,7 +13,6 @@ string formatMe; string keepNewLine; // csharpier-ignore-end - // csharpier-ignore-start string removeNewLine; // csharpier-ignore-end diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.expected.test new file mode 100644 index 000000000..a94402680 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.expected.test @@ -0,0 +1,5 @@ +var x = 1; + +// condense down to just a single extra line + +var y = 2; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.test new file mode 100644 index 000000000..2875a48d8 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_TrailingNewLines.test @@ -0,0 +1,8 @@ +var x = 1; + +// condense down to just a single extra line + + + + +var y = 2; \ No newline at end of file diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index f21becad8..6b5ce17c7 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -237,6 +237,10 @@ private static Doc PrivatePrintLeadingTrivia( if (printNewLines && kind == SyntaxKind.EndOfLineTrivia) { + if (docs.Count > 0 && docs[^1] == Doc.HardLineSkipBreakIfFirstInGroup) + { + printNewLines = false; + } docs.Add(Doc.HardLineSkipBreakIfFirstInGroup); } if (kind is not (SyntaxKind.EndOfLineTrivia or SyntaxKind.WhitespaceTrivia)) @@ -317,7 +321,7 @@ void AddLeadingComment(CommentType commentType) } } - while (skipLastHardline && docs.Any() && docs.Last() is HardLine or NullDoc) + while (skipLastHardline && docs.Count != 0 && docs.Last() is HardLine or NullDoc) { docs.RemoveAt(docs.Count - 1); }