Skip to content

Fix formatting instability on long if conditions with if-then-else=fit-or-vertical#2797

Open
MisterDA wants to merge 2 commits intoocaml-ppx:mainfrom
MisterDA:fix-2770
Open

Fix formatting instability on long if conditions with if-then-else=fit-or-vertical#2797
MisterDA wants to merge 2 commits intoocaml-ppx:mainfrom
MisterDA:fix-2770

Conversation

@MisterDA
Copy link
Copy Markdown

I vibe-clauded a fix for #2770, which is an annoying blocker for me.

…then

When using if-then-else=fit-or-vertical, a comment placed between the
`then` keyword and the branch expression caused an infinite oscillation
between two formats:

  then (* comment *)    <-->    then
    expr                          (* comment *)
                                  expr

The root cause is that the hovbox's soft break (break 1 2) in branch_pro
allows the comment to fit on the same line as `then` when the hovbox
doesn't break. On re-parsing, the comment is then classified as 'after
the then keyword' instead of 'before the expression', producing a
different layout that breaks back to the other form.

Fix: in the Fit_or_vertical case, when comments exist before the branch
body and the overall expression is multi-line, force a hard line break
(break 1000 2) so the comment always stays on its own indented line.
The single-line check ensures short expressions that fit on one line
are unaffected.
Add a test case with a long condition and a comment between `then` and
the branch expression. This is the pattern that previously triggered
'formatting did not stabilize after 10 iterations' with
if-then-else=fit-or-vertical.
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