From 9d9affa7a934a70510ad34b7218230d9e1b8a0a4 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 10 Jan 2025 10:26:16 -0600 Subject: [PATCH] fixing trailing comma + trailing comment issue on enums closes #1429 --- ...ailingComma_TrailingComments.expected.test | 12 +++++ .../cs/TrailingComma_TrailingComments.test | 14 +++++- .../SyntaxPrinter/MembersWithForcedLines.cs | 50 ++++++++++++++----- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.expected.test index 2ab64a1d9..2b907a398 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.expected.test @@ -17,3 +17,15 @@ var someObject = new SomeObject() [ /* this formatting isn't ideal, but this probably won't happen in the real world */ ], // Trailing Comment }; + +enum MyEnum +{ + First, + Second, // the second value +} + +enum MyEnum +{ + First, + Second, // the second value +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.test index 9be2b77eb..08a85e374 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.test @@ -14,4 +14,16 @@ var someObject = new SomeObject() { Property1 = 1, Property2 = [/* this formatting isn't ideal, but this probably won't happen in the real world */ ] // Trailing Comment -}; \ No newline at end of file +}; + +enum MyEnum +{ + First, + Second // the second value +} + +enum MyEnum +{ + First, + Second, // the second value +} diff --git a/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs b/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs index ff0b644b6..27b9f536e 100644 --- a/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs +++ b/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs @@ -50,24 +50,45 @@ public static List Print( continue; } - void AddSeparatorIfNeeded() + Doc GetSeparatorIfNeeded() { - if (members is SeparatedSyntaxList list) + if (members is not SeparatedSyntaxList list) { - if (memberIndex < list.SeparatorCount) + return Doc.Null; + } + + if (memberIndex < list.SeparatorCount) + { + return Token.Print(list.GetSeparator(memberIndex), context); + } + + if ( + node is EnumDeclarationSyntax enumDeclarationSyntax + && member is EnumMemberDeclarationSyntax + ) + { + var firstTrailingComment = list[memberIndex] + .GetTrailingTrivia() + .FirstOrDefault(o => o.IsComment()); + + if (firstTrailingComment != default) { - result.Add(Token.Print(list.GetSeparator(memberIndex), context)); + context.WithTrailingComma( + firstTrailingComment, + TrailingComma.Print( + enumDeclarationSyntax.CloseBraceToken, + context, + true + ) + ); } - else if ( - node is EnumDeclarationSyntax enumDeclarationSyntax - && member is EnumMemberDeclarationSyntax - ) + else { - result.Add( - TrailingComma.Print(enumDeclarationSyntax.CloseBraceToken, context) - ); + return TrailingComma.Print(enumDeclarationSyntax.CloseBraceToken, context); } } + + return Doc.Null; } var blankLineIsForced = ( @@ -104,7 +125,8 @@ member is MethodDeclarationSyntax methodDeclaration { lastMemberForcedBlankLine = blankLineIsForced; result.Add(Node.Print(member, context)); - AddSeparatorIfNeeded(); + result.AddIfNotNull(GetSeparatorIfNeeded()); + continue; } @@ -193,8 +215,10 @@ or SyntaxKind.EndRegionDirectiveTrivia context.State.NextTriviaNeedsLine = true; } + // this has a side effect (yuck) that fixes the trailing comma + trailing comment issue so we have to call it first + var separator = GetSeparatorIfNeeded(); result.Add(Doc.HardLine, Node.Print(member, context)); - AddSeparatorIfNeeded(); + result.AddIfNotNull(separator); lastMemberForcedBlankLine = blankLineIsForced; }