diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_EndOfFile.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_EndOfFile.test new file mode 100644 index 000000000..141b5dd25 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_EndOfFile.test @@ -0,0 +1,5 @@ +using System; + +namespace MyCompany.MyNamespace; + +// Comment block diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs index 6d82305d7..44b1d02d5 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs @@ -14,8 +14,24 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context) ); if (finalTrivia != Doc.Null) { - // even though we include the initialNewLines above, a literalLine from directives trims the hardline, so add an extra one here - docs.Add(Doc.HardLineIfNoPreviousLine, finalTrivia); + // really ugly code to prevent a comment at the end of a file from continually inserting new blank lines + if ( + finalTrivia is Concat { Contents.Count: > 1 } list + && list.Contents[1] is LeadingComment + && docs[^1] is Concat previousList + && previousList.Contents[^1] is HardLine + && previousList.Contents[^2] is HardLine + ) + { + list.Contents.RemoveAt(0); + + docs.Add(finalTrivia); + } + else + { + // even though we include the initialNewLines above, a literalLine from directives trims the hardline, so add an extra one here + docs.Add(Doc.HardLineIfNoPreviousLine, finalTrivia); + } } docs.Add(Doc.HardLineIfNoPreviousLine);