Skip to content
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
62 changes: 62 additions & 0 deletions src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,68 @@ class C
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/10526")]
public Task LambdaListWithComma()
=> AssertNoFormattingChangesAsync("""
using System;

class Test
{
void M()
{
Action a = () => { },
b = () => { };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add tests with non-block lambdas:

                    Action a = () => x,
                           b = () => y;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 23bc2683.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add tests with non-block lambdas:

                    Action a = () => x,
                           b = () => y;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 4893298.

}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/10526")]
public Task LambdaListWithCommaMultipleVariables()
=> AssertNoFormattingChangesAsync("""
using System;

class Test
{
void M()
{
Action a = () => { },
b = () => { },
c = () => { };
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/10526")]
public Task AnonymousMethodListWithComma()
=> AssertNoFormattingChangesAsync("""
using System;

class Test
{
void M()
{
Action a = delegate { },
b = delegate { };
}
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/10526")]
public Task ExpressionLambdaListWithComma()
=> AssertNoFormattingChangesAsync("""
using System;

class Test
{
void M()
{
int x = 1, y = 2;
Func<int> a = () => x,
b = () => y;
}
}
""");

[Fact]
public Task Scen1()
=> AssertFormatAsync("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public static bool IsCommaInAnyArgumentsList(this SyntaxToken token)
token.Parent.IsAnyArgumentList();
}

public static bool IsCommaInVariableDeclaration(this SyntaxToken token)
{
return token.Kind() == SyntaxKind.CommaToken &&
token.Parent.IsKind(SyntaxKind.VariableDeclaration);
}

public static bool IsOpenParenOfParenthesizedExpression(this SyntaxToken token)
=> token.Parent is ParenthesizedExpressionSyntax parenthesizedExpression && parenthesizedExpression.OpenParenToken.Equals(token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public override AbstractFormattingRule WithOptions(SyntaxFormattingOptions optio
!currentToken.IsCloseParenOfParenthesizedExpression() && // Place ) after } in `(() => {})`
!currentToken.IsCommaInInitializerExpression() &&
!currentToken.IsCommaInAnyArgumentsList() &&
!currentToken.IsCommaInVariableDeclaration() &&
!currentToken.IsCommaInTupleExpression() &&
!currentToken.IsCommaInCollectionExpression() &&
!currentToken.IsParenInArgumentList() &&
Expand Down
Loading