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

fixing trailing comma + trailing comment issue on enums #1431

Merged
merged 1 commit into from
Jan 10, 2025
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
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

enum MyEnum
{
First,
Second // the second value
}

enum MyEnum
{
First,
Second, // the second value
}
50 changes: 37 additions & 13 deletions Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,45 @@ public static List<Doc> Print<T>(
continue;
}

void AddSeparatorIfNeeded()
Doc GetSeparatorIfNeeded()
{
if (members is SeparatedSyntaxList<T> list)
if (members is not SeparatedSyntaxList<T> 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 = (
Expand Down Expand Up @@ -104,7 +125,8 @@ member is MethodDeclarationSyntax methodDeclaration
{
lastMemberForcedBlankLine = blankLineIsForced;
result.Add(Node.Print(member, context));
AddSeparatorIfNeeded();
result.AddIfNotNull(GetSeparatorIfNeeded());

continue;
}

Expand Down Expand Up @@ -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;
}
Expand Down
Loading