Skip to content

Fix indentCase handling (fixes #2442)#2443

Draft
stackotter wants to merge 3 commits intonicklockwood:developfrom
stackotter:stackotter/fix_2442
Draft

Fix indentCase handling (fixes #2442)#2443
stackotter wants to merge 3 commits intonicklockwood:developfrom
stackotter:stackotter/fix_2442

Conversation

@stackotter
Copy link
Copy Markdown

As described in #2442, the indentCase formatting option has some unexpected behaviour. I've updated handling of indentCase in Result/Indent.swift to match my mental model of what indentCase does.

My mental model for indentCase is that it should lead to identical behaviour to the default switch statement formatting behaviour except with the entire body of the switch statement indented an additional level.

This mental model lines up with SwiftFormat's behaviour in most cases. The only exceptions that I know of are #2442, and the testIndentSwitchCaseWhere test case which I had to update after my changes. My justification for updating the testIndentSwitchCaseWhere test case rather than changing my implementation to preserve the behaviour that it originally tested for is that if you run that same test case without indentCase you get the same where clause wrapping behaviour as my implementation; and it doesn't make sense to me that where clause wrapping should be affected by the indentCase option.

Implementation

In order to implement this new behaviour, I ripped out all existing indentCase handling (which was originally spread all through the applyIndent implementation), and replaced it with a more direct approach. Most of the new code is used to detect whether a given { token is the start of a switch statement body, and similarly whether a given } token is the end of a switch statement body. If indentCase is enabled, then I add one more level of indentation (and push it onto the indentation scope so that case's scope-ending behaviour doesn't mess things up) when a switch body begins, and remove it when a switch body ends.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.24%. Comparing base (4cf685f) to head (a499313).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2443      +/-   ##
===========================================
- Coverage    95.28%   95.24%   -0.04%     
===========================================
  Files          165      165              
  Lines        25147    25167      +20     
===========================================
+ Hits         23962    23971       +9     
- Misses        1185     1196      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stackotter stackotter marked this pull request as draft March 12, 2026 12:38
@stackotter
Copy link
Copy Markdown
Author

@bbrk24 pointed out that this wouldn't handle nested switch statements so I've converted this to a draft for now. Will come back and fix that another day (by updating my implementation to use stacks).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant