Skip to content

Commit

Permalink
Fixing issue with trailing commas that have a trailing comment
Browse files Browse the repository at this point in the history
closes #1300
  • Loading branch information
belav committed Jul 10, 2024
1 parent 7ab89d5 commit 3111d65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="20.0.4" />
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" />
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class ClassName
{
int[] array = { 1, 2 };

int[] array =
[
1, // some comment
];

int[] array =
{
"someLongValue_____________________________________",
Expand Down
32 changes: 30 additions & 2 deletions Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ public static Doc Print<T>(
docs.Add(printFunc(list[x], context));

var isTrailingSeparator = x == list.Count - 1;
SyntaxToken? trailingSeparatorToken = null;

if (isTrailingSeparator && x < list.SeparatorCount)
{
trailingSeparatorToken = list.GetSeparator(x);
}

if (x >= list.SeparatorCount)
{
if (isTrailingSeparator && trailingSeparator != null)
if (
(
!trailingSeparatorToken.HasValue
|| !trailingSeparatorToken.Value.TrailingTrivia.Any(o => o.IsComment())
)
&& isTrailingSeparator
&& trailingSeparator != null
)
{
docs.Add(trailingSeparator);
}
Expand All @@ -31,7 +44,22 @@ public static Doc Print<T>(

if (isTrailingSeparator)
{
docs.Add(Doc.IfBreak(Token.Print(list.GetSeparator(x), context), Doc.Null));
if (
trailingSeparatorToken.HasValue
&& trailingSeparatorToken.Value.TrailingTrivia.Any(o => o.IsComment())
)
{
docs.Add(Token.Print(list.GetSeparator(x), context));
}
// TODO I think we can ditch this parameter, and if there is no current one, just call the TrailingComma.Print ourselves
else if (trailingSeparator != null)
{
docs.Add(trailingSeparator);
}
else
{
docs.Add(Doc.IfBreak(Token.Print(list.GetSeparator(x), context), Doc.Null));
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions Src/CSharpier/SyntaxPrinter/TrailingComma.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace CSharpier.SyntaxPrinter;

// TODO review the other places with trailing comments
// https://github.com/belav/csharpier/pull/1284/files
internal static class TrailingComma
{
public static Doc Print(SyntaxToken nextSyntaxToken, FormattingContext context)
Expand Down

0 comments on commit 3111d65

Please sign in to comment.