Fix formatting oscillation in if-then-else branches#2800
Open
MisterDA wants to merge 1 commit into
Open
Conversation
Julow
reviewed
Apr 28, 2026
Contributor
Author
|
Thanks for the review, here's a revised version of the patch with your comments. |
Julow
reviewed
Apr 30, 2026
Julow
reviewed
May 4, 2026
| ~parens_bch ~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs | ||
| ~infix_ext_attrs ~fmt_cond ~cmts_before_kw ~cmts_after_kw = | ||
| ~infix_ext_attrs ~fmt_cond ~cmts_before_kw ~cmts_after_kw | ||
| ~cmts_after_kw_raw = |
Collaborator
There was a problem hiding this comment.
I really don't like this cmts_after_kw_raw argument. I tried to get rid of it by passing Cmts.fmt_after ~pro:noop ~epi:noop .. to ~cmts_after_kw but it's very hard due to the many different states this creates.
I won't merge a new argument to get_if_then_else.
Contributor
Author
There was a problem hiding this comment.
Claude found another workaround. Let me know if it's satisfying or not.
EDIT: not sure how to reproduce the CI failures.
When a comment sits between a keyword (then/else) and certain branch expressions (begin...end wrapping match/try/function/ifthenelse, or bare match/try/function/ifthenelse), the comment would alternate between "after keyword" and "before expression" placements across formatting iterations. This caused branch_pro to compute different indentation each time, producing "formatting did not stabilize after 10 iterations". The fix detects these oscillation-prone patterns and ensures stable formatting regardless of the initial comment placement: - In Fmt_ast, when a branch matches `needs_raw_cmts_after_kw`, extract "after keyword" comments without breaks and override branch_pro using `Params.raw_cmts_branch_pro` (which computes mode-appropriate break+indent+comment output). - In Params, `branch_pro_with_cmts` detects comments "before expression" (the alternate placement) and forces a break, producing the same layout as the "after keyword" path. - For begin...end branches, `branch_pro` checks for comments before the inner expression (not just the begin...end node) to handle the case where the comment migrates inside the begin...end scope. Affected modes: Fit_or_vertical, Compact (profile=conventional). Assisted-by: GitHub Copilot:claude-opus-4-20250514
if-then-else=fit-or-vertical and begin…end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a comment sits between a keyword (then/else) and certain branch
expressions (begin...end wrapping match/try/function/ifthenelse, or bare
match/try/function/ifthenelse), the comment would alternate between
"after keyword" and "before expression" placements across formatting
iterations. This caused branch_pro to compute different indentation each
time, producing "formatting did not stabilize after 10 iterations".
The fix detects these oscillation-prone patterns and ensures stable
formatting regardless of the initial comment placement:
In Fmt_ast, when a branch matches
needs_raw_cmts_after_kw, extract"after keyword" comments without breaks and override branch_pro using
Params.raw_cmts_branch_pro(which computes mode-appropriatebreak+indent+comment output).
In Params,
branch_pro_with_cmtsdetects comments "before expression"(the alternate placement) and forces a break, producing the same layout
as the "after keyword" path.
For begin...end branches,
branch_prochecks for comments before theinner expression (not just the begin...end node) to handle the case
where the comment migrates inside the begin...end scope.
Affected modes: Fit_or_vertical, Compact (profile=conventional).
Closes #2799.