Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

Fix Property Pattern Indentation in Positional and Parenthesized Patterns ✅

Problem

  • Add test case to reproduce the issue in FormattingTests_Patterns.cs
  • Identify root cause in IndentBlockFormattingRule.cs
  • Fix the indentation calculation for PropertyPatternClause inside SubpatternSyntax
  • Run broader formatting tests to ensure no regressions
  • Run code review and security scanning
  • Update WorkItem reference to correct issue number (Bad formatting on property pattern in a positional pattern when line wrapping #54434)
  • Add test for parenthesized patterns (single pattern without comma separation)

Summary

Fixed issue #54434 where property patterns inside positional (tuple) patterns were being incorrectly dedented during formatting. The fix also handles parenthesized patterns without comma separation.

Before: Property patterns in tuple patterns would dedent by 2 levels instead of maintaining proper indentation.
After: Property patterns are correctly indented with one additional level relative to the containing pattern.

Root Cause

When a RecursivePatternSyntax was inside a SubpatternSyntax (positional pattern context), the code attempted to use recursivePatternParent.GetFirstToken() as the alignment base. However, since the RecursivePattern could start with the property pattern's opening brace itself, this resulted in baseTokenForAlignment == propertyPatternClause.OpenBraceToken, causing the alignment operation to be skipped entirely. This led to incorrect dedentation as fallback rules took over.

Solution

Modified IndentBlockFormattingRule.AddAlignmentBlockOperation for the PropertyPatternClauseSyntax case to:

  1. Detect when a RecursivePattern is inside a SubpatternSyntax (positional pattern context)
  2. Navigate up the syntax tree: SubpatternSyntaxPositionalPatternClauseSyntax → outer RecursivePatternSyntax → use its parent as the alignment base
  3. Use FormattingOperations.CreateRelativeIndentBlockOperation directly with indentationDelta = 1 to add one indentation level
  4. Remove the IndentIfConditionOfAnchorToken flag for positional patterns (only applies to regular is patterns)

Testing Results

✅ New test FormatPropertyPatternInPositionalPattern1 passes (tuple patterns with comma separation)
✅ New test FormatPropertyPatternInParenthesizedPattern1 passes (parenthesized patterns without comma separation)
✅ All 56 pattern formatting tests pass
✅ All 597 formatting tests pass (1 skipped)
✅ CodeQL security scan: No issues found

Security Summary

No security vulnerabilities were introduced or fixed by this change.


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

Copilot AI changed the title [WIP] Fix bad formatting on property pattern in positional pattern Fix property pattern indentation in positional patterns Nov 13, 2025
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 12:08
Copilot AI requested a review from CyrusNajmabadi November 13, 2025 12:08
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/54288")]
Copy link
Member

Choose a reason for hiding this comment

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

@copilot update tehse workitems to #54434

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated WorkItem reference to #54434 in commit 2dc9d9a.

{
Identifier: { ValueText: var formerIdentifier }
}
},
Copy link
Member

Choose a reason for hiding this comment

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

@copilot test parenthesized patterns as well. so just (...) without comma separation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added test FormatPropertyPatternInParenthesizedPattern1 for parenthesized patterns without comma separation in commit f31e665.

Copilot AI requested a review from CyrusNajmabadi November 13, 2025 12:16
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 12:16
Copilot AI changed the title Fix property pattern indentation in positional patterns Fix property pattern indentation in positional and parenthesized patterns Nov 13, 2025
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 12:42
@CyrusNajmabadi CyrusNajmabadi force-pushed the copilot/fix-bad-formatting-pattern branch from f31e665 to efdb235 Compare November 14, 2025 10:16
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 14, 2025 10:16
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 14, 2025 10:16
@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-ide ptal.

public void DoThing()
{
OrderingSyntax former = orderings[i], latter = orderings[j];
if ((former, latter) is not (
Copy link
Member

Choose a reason for hiding this comment

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

This test code doesn't exactly match the case from the issue. Should we add a test where (former, latter) is not ( is on a new line?

Copy link
Member

Choose a reason for hiding this comment

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

sure. i can add in followup!

}
}
""", """
public class Goo
Copy link
Member

@akhera99 akhera99 Nov 14, 2025

Choose a reason for hiding this comment

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

confusing that the expected comes before the original

Copy link
Member

Choose a reason for hiding this comment

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

yes. i highly dislike formatter tests for this reason :'(

Copy link
Member

Choose a reason for hiding this comment

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

Can copilot add named arguments and reverse the order? =)

@CyrusNajmabadi CyrusNajmabadi merged commit 4bc2a24 into main Nov 14, 2025
26 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-bad-formatting-pattern branch November 14, 2025 17:12
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 14, 2025
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.

4 participants