Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

Fix comma placement after lambda block in variable declarations

Formatting incorrectly moved commas to a new line when multiple lambda or anonymous method variables were declared in a single statement:

// Before fix - comma forced to new line
Action a = () => { }
,
       b = () => { };

// After fix - comma stays on same line
Action a = () => { },
       b = () => { };

Root Cause

Lambda/anonymous method blocks have BlockSyntax parent (not ExpressionSyntax), so IsCloseBraceOfExpression() returns false. This triggered newline insertion logic, which checked exceptions for commas in initializers, arguments, tuples, and collections—but missed commas in variable declarations.

Changes

  • FormattingHelpers.cs: Added IsCommaInVariableDeclaration() helper
  • TokenBasedFormattingRule.cs: Added exception check for !currentToken.IsCommaInVariableDeclaration()
  • FormattingTests.cs: Added tests for lambda lists, multiple variables, anonymous methods, and expression-bodied lambdas

Testing

Fixes #10526

Original prompt

This section details on the original issue you should resolve

<issue_title>Incorrect comma placement when formatting lambda list</issue_title>
<issue_description>Version Used: VS 2015 Update 2

Steps to Reproduce:

  1. Add using System;
  2. Write the following code:
Action a = () => { }, 
       b = () => { };
  1. Format the document

Expected Behavior: Nothing changes

Actual Behavior: The comma goes on its own line:

Action a = () => { }
,
       b = () => { };

Might be related to #7704, another lambda formatting bug I reported.
</issue_description>

<agent_instructions>@copilot see if you can figure out what's causing us to force a new-line here.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect comma placement when formatting lambda list Fix comma placement after lambda block in variable declarations Nov 13, 2025
Copilot AI requested a review from CyrusNajmabadi November 13, 2025 11:44
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 11:44
}
""");

[Fact]
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 [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/10526")] to all added tests.

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 2e74af8.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
{
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.

Copilot AI requested a review from CyrusNajmabadi November 13, 2025 14:59
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 14:59
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 13, 2025 15:04
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 13, 2025 15:04
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 15:58
@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (rebase) November 13, 2025 16:01
@CyrusNajmabadi CyrusNajmabadi merged commit 3cea483 into main Nov 13, 2025
25 of 26 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 13, 2025
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-comma-placement-issue branch November 13, 2025 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect comma placement when formatting lambda list

3 participants