Skip to content

Commit

Permalink
More edgecases in compilation unit (#556)
Browse files Browse the repository at this point in the history
closes #551
  • Loading branch information
belav committed Jan 21, 2022
1 parent fdc71f0 commit 90d6ef5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

[assembly: SomeAttribute]

namespace Namespace { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

#if DEBUG
[assembly: SomeAttribute]

#endif

namespace Namespace { }
17 changes: 14 additions & 3 deletions Src/CSharpier/SyntaxPrinter/NamespaceLikePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,26 @@ node is CompilationUnitSyntax compilationUnitSyntax

if (usings.Any() || (!usings.Any() && externs.Any()))
{
if (members[0].GetLeadingTrivia().Any(o => o.IsDirective))
var directiveTrivia = members[0].GetLeadingTrivia().Where(o => o.IsDirective).ToArray();

if (directiveTrivia.Any())
{
docs.Add(ExtraNewLines.Print(members[0]));
if (
(
node is not CompilationUnitSyntax { AttributeLists: { Count: > 0 } }
&& directiveTrivia.All(o => o.Kind() is SyntaxKind.EndIfDirectiveTrivia)
) || !directiveTrivia.All(o => o.Kind() is SyntaxKind.EndIfDirectiveTrivia)
)
{
docs.Add(ExtraNewLines.Print(members[0]));
}
}
else
else if (node is not CompilationUnitSyntax { AttributeLists: { Count: > 0 } })
{
docs.Add(Doc.HardLine);
}
}

docs.AddRange(MembersWithForcedLines.Print(node, members));
}
}

0 comments on commit 90d6ef5

Please sign in to comment.