Skip to content

Commit ec2b18f

Browse files
authored
fixing trailing comma + trailing comment issue on enums (#1431)
closes #1429
1 parent 75be6ab commit ec2b18f

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.expected.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ var someObject = new SomeObject()
1717
[ /* this formatting isn't ideal, but this probably won't happen in the real world */
1818
], // Trailing Comment
1919
};
20+
21+
enum MyEnum
22+
{
23+
First,
24+
Second, // the second value
25+
}
26+
27+
enum MyEnum
28+
{
29+
First,
30+
Second, // the second value
31+
}

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TrailingComma_TrailingComments.test

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ var someObject = new SomeObject()
1414
{
1515
Property1 = 1,
1616
Property2 = [/* this formatting isn't ideal, but this probably won't happen in the real world */ ] // Trailing Comment
17-
};
17+
};
18+
19+
enum MyEnum
20+
{
21+
First,
22+
Second // the second value
23+
}
24+
25+
enum MyEnum
26+
{
27+
First,
28+
Second, // the second value
29+
}

Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,45 @@ public static List<Doc> Print<T>(
5050
continue;
5151
}
5252

53-
void AddSeparatorIfNeeded()
53+
Doc GetSeparatorIfNeeded()
5454
{
55-
if (members is SeparatedSyntaxList<T> list)
55+
if (members is not SeparatedSyntaxList<T> list)
5656
{
57-
if (memberIndex < list.SeparatorCount)
57+
return Doc.Null;
58+
}
59+
60+
if (memberIndex < list.SeparatorCount)
61+
{
62+
return Token.Print(list.GetSeparator(memberIndex), context);
63+
}
64+
65+
if (
66+
node is EnumDeclarationSyntax enumDeclarationSyntax
67+
&& member is EnumMemberDeclarationSyntax
68+
)
69+
{
70+
var firstTrailingComment = list[memberIndex]
71+
.GetTrailingTrivia()
72+
.FirstOrDefault(o => o.IsComment());
73+
74+
if (firstTrailingComment != default)
5875
{
59-
result.Add(Token.Print(list.GetSeparator(memberIndex), context));
76+
context.WithTrailingComma(
77+
firstTrailingComment,
78+
TrailingComma.Print(
79+
enumDeclarationSyntax.CloseBraceToken,
80+
context,
81+
true
82+
)
83+
);
6084
}
61-
else if (
62-
node is EnumDeclarationSyntax enumDeclarationSyntax
63-
&& member is EnumMemberDeclarationSyntax
64-
)
85+
else
6586
{
66-
result.Add(
67-
TrailingComma.Print(enumDeclarationSyntax.CloseBraceToken, context)
68-
);
87+
return TrailingComma.Print(enumDeclarationSyntax.CloseBraceToken, context);
6988
}
7089
}
90+
91+
return Doc.Null;
7192
}
7293

7394
var blankLineIsForced = (
@@ -104,7 +125,8 @@ member is MethodDeclarationSyntax methodDeclaration
104125
{
105126
lastMemberForcedBlankLine = blankLineIsForced;
106127
result.Add(Node.Print(member, context));
107-
AddSeparatorIfNeeded();
128+
result.AddIfNotNull(GetSeparatorIfNeeded());
129+
108130
continue;
109131
}
110132

@@ -193,8 +215,10 @@ or SyntaxKind.EndRegionDirectiveTrivia
193215
context.State.NextTriviaNeedsLine = true;
194216
}
195217

218+
// this has a side effect (yuck) that fixes the trailing comma + trailing comment issue so we have to call it first
219+
var separator = GetSeparatorIfNeeded();
196220
result.Add(Doc.HardLine, Node.Print(member, context));
197-
AddSeparatorIfNeeded();
221+
result.AddIfNotNull(separator);
198222

199223
lastMemberForcedBlankLine = blankLineIsForced;
200224
}

0 commit comments

Comments
 (0)