Skip to content

Commit

Permalink
Figuring out a way to prevent the extra line before a file ending com…
Browse files Browse the repository at this point in the history
…ment (#1409)

closes #1408
  • Loading branch information
belav authored Dec 28, 2024
1 parent baa3138 commit e3b5efb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace MyCompany.MyNamespace;

// Comment block
20 changes: 18 additions & 2 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e3b5efb

Please sign in to comment.