Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

Fix #endregion indentation when followed by labels

Plan

  • Reproduce the issue with a test case that demonstrates the bug
  • Analyze the code to understand why #endregion is incorrectly affected by following labels
  • Fix the indentation logic in CSharpTriviaFormatter to use GetMatchingDirective for #endregion
  • Verify the test passes with the fix
  • Run FormattingEngineTriviaTests to ensure no regressions (72/72 passed)
  • Run broader formatting tests to ensure no regressions (652/652 passed)
  • Security scan completed - no issues found
  • Fix WorkItem URL to reference correct issue (#endregion directive improperly indented when followed by a label #75919)
  • Simplify logic to always align #endregion with #region

Issue

When a #endregion directive is followed by a label, the formatter incorrectly indents the #endregion to the same level as the label, instead of matching the indentation of the corresponding #region directive.

Root Cause

The formatter was using "default indentation" for #region and #endregion directives. This default indentation is calculated based on the position where the directive appears, which can be influenced by the following token. When a label follows #endregion, the formatter's indentation calculation is affected by the label's reduced indentation, causing #endregion to be incorrectly indented.

Solution

Modified CSharpTriviaFormatter.GetLineColumnRuleBetween() to:

  1. For #endregion directives, find the matching #region using GetMatchingDirective()
  2. Always align #endregion with its matching #region indentation

This ensures #endregion always aligns with its matching #region, preventing labels or other following constructs from incorrectly influencing the indentation. This provides consistent and predictable behavior.

Testing

  • Added test case EndRegionFollowedByLabel demonstrating the fix ✅
  • Updated Preprocessor8 test to reflect the new consistent behavior ✅
  • All 72 FormattingEngineTriviaTests pass ✅
  • No security vulnerabilities detected ✅

Fixes #75919


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix improper indentation of #endregion directive Fix #endregion indentation when followed by labels Nov 13, 2025
Copilot AI requested a review from CyrusNajmabadi November 13, 2025 12:14
Copilot finished work on behalf of CyrusNajmabadi November 13, 2025 12:15
}
""");

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

Choose a reason for hiding this comment

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

@copilot all these urls should be #75919

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a48ed5b

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>

// Use the maximum to handle cases where #endregion is in a deeper scope than #region
var indentation = Math.Max(regionIndentation, defaultIndentation);
return LineColumnRule.PreserveLinesWithAbsoluteIndentation(lines, indentation);
Copy link
Member

Choose a reason for hiding this comment

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

@copilot i don't understand why we need to use this 'maximum' concept? i think it would suffice to always align the #endregion with the #region.

if there is a case that makes you think your logic is correct, then please add a test demonstrating why that's teh desirable behavior. Otherwise, simplify this.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (rebase) November 13, 2025 16:04
@CyrusNajmabadi CyrusNajmabadi merged commit 5ee403c into main Nov 13, 2025
25 of 26 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-region-indentation-issue branch November 13, 2025 18:06
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 13, 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.

#endregion directive improperly indented when followed by a label

3 participants