Skip to content

Commit 4b757ee

Browse files
authored
Improving the hack that ensures we don't keep adding new lines before a file ending comment (#1427)
closes #1426
1 parent e5b5565 commit 4b757ee

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System;
2+
3+
namespace MyCompany.MyNamespace;
4+
5+
// Comment block
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace MyCompany.MyNamespace;
4+
5+
6+
7+
8+
// Comment block

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context)
88

99
NamespaceLikePrinter.Print(node, docs, context);
1010

11+
// this is what ends up adding multiple HardLineSkipBreakIfFirstInGroup, but it is needed for some cases
12+
// and trying to change the logic in there to not print multiple lines was taking me down a rabbit hole
1113
var finalTrivia = Token.PrintLeadingTriviaWithNewLines(
1214
node.EndOfFileToken.LeadingTrivia,
1315
context
@@ -17,14 +19,16 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context)
1719
// really ugly code to prevent a comment at the end of a file from continually inserting new blank lines
1820
if (
1921
finalTrivia is Concat { Contents.Count: > 1 } list
20-
&& list.Contents[1] is LeadingComment
2122
&& docs.Count > 0
2223
&& docs[^1] is Concat { Contents.Count: > 1 } previousList
2324
&& previousList.Contents[^1] is HardLine
2425
&& previousList.Contents[^2] is HardLine
2526
)
2627
{
27-
list.Contents.RemoveAt(0);
28+
while (list.Contents[0] is HardLine { SkipBreakIfFirstInGroup: true })
29+
{
30+
list.Contents.RemoveAt(0);
31+
}
2832

2933
docs.Add(finalTrivia);
3034
}

0 commit comments

Comments
 (0)