Skip to content

Commit

Permalink
Improving the hack that ensures we don't keep adding new lines before…
Browse files Browse the repository at this point in the history
… a file ending comment (#1427)

closes #1426
  • Loading branch information
belav authored Jan 3, 2025
1 parent e5b5565 commit 4b757ee
Show file tree
Hide file tree
Showing 3 changed files with 19 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace MyCompany.MyNamespace;




// Comment block
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context)

NamespaceLikePrinter.Print(node, docs, context);

// this is what ends up adding multiple HardLineSkipBreakIfFirstInGroup, but it is needed for some cases
// and trying to change the logic in there to not print multiple lines was taking me down a rabbit hole
var finalTrivia = Token.PrintLeadingTriviaWithNewLines(
node.EndOfFileToken.LeadingTrivia,
context
Expand All @@ -17,14 +19,16 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context)
// 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.Count > 0
&& docs[^1] is Concat { Contents.Count: > 1 } previousList
&& previousList.Contents[^1] is HardLine
&& previousList.Contents[^2] is HardLine
)
{
list.Contents.RemoveAt(0);
while (list.Contents[0] is HardLine { SkipBreakIfFirstInGroup: true })
{
list.Contents.RemoveAt(0);
}

docs.Add(finalTrivia);
}
Expand Down

0 comments on commit 4b757ee

Please sign in to comment.