Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figuring out a way to prevent the extra line before a file ending comment #1409

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading