Skip to content

Fix vertical breaks in let-in with comments before 'in'#2813

Open
yakobowski wants to merge 2 commits into
ocaml-ppx:mainfrom
yakobowski:main
Open

Fix vertical breaks in let-in with comments before 'in'#2813
yakobowski wants to merge 2 commits into
ocaml-ppx:mainfrom
yakobowski:main

Conversation

@yakobowski

@yakobowski yakobowski commented Jul 4, 2026

Copy link
Copy Markdown

I'm currently migrating a big codebase which is formatted using ocamlformat 0.22. I have noticed quite a few improvements (thanks!) but also some regressions. Some of them are fixed by #2810. The remaining ones are related to comments in a let binding.

Bug: let x = value (* comment *) in body is always broken across multiple lines, even when it fits within the margin. The force_break prefix on comments near in unconditionally forced the containing hvbox to vertical mode.

Fix: ~pro:force_break → ~pro:(break 1 0) in two places in fmt_value_binding. The comment now gets a regular break (becomes a space when the box fits horizontally, still breaks when it doesn't). Also fixes a non-idempotency where the one-liner and its pre-broken form produced different outputs. Affects all profiles.

Boris Yakobowski and others added 2 commits July 6, 2026 17:38
Add test cases to let_binding.ml covering:
- Short comment after value that fits on one line (currently broken
  across lines regardless of configuration)
- Function body with trailing comment (same issue)
- Long expression with comment that legitimately needs a break

The bug affects all profiles/configurations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
`let x = value (* comment *) in` was being broken across multiple lines
regardless of configuration because comments near the `in` keyword were
formatted with `force_break` as their prefix, which unconditionally
forced the containing hvbox to vertical mode.

Fix: change `~pro:force_break` to `~pro:(break 1 0)` in two places in
fmt_value_binding — the comment is now a regular break that becomes a
space when the hvbox fits horizontally. When the content exceeds the
margin, the box still goes vertical naturally.

This also fixes a non-idempotency: previously, `let x = v (* c *) in`
and its pre-broken equivalent produced different outputs. The bug
affects all profiles (default, ocamlformat, janestreet, ahrefs).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yakobowski

Copy link
Copy Markdown
Author

#2810 is uniformly better on the if-then-else issue, so I'm dropping those commits from the MR.

@yakobowski yakobowski changed the title Fix various regressions compared to 0.22.4 Fix vertical breaks in let-in with comments before 'in' Jul 6, 2026
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