Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: max-indent for lambdas is not respected when ocp-indent-compat=false #2570

Open
chshersh opened this issue Aug 7, 2024 · 2 comments
Open

Comments

@chshersh
Copy link

chshersh commented Aug 7, 2024

Describe the bug

Actual behaviour: When I pass a fun lambda function to another function, the body of the lambda function is indented with 4 spaces when max-indent is explicitly set to two

Expected behaviour: The body is intended with two spaces

How to Reproduce

I have the followating OCaml code in the test.ml file:

let lwt_apply f res =
   Lwt.bind res (fun r ->
     f r;
     res )

The .ocamlformat file is empty.

When I format the code with max-indent=2 and ocp-indent-compat flags, ocamlformat formats everything with two spaces consistently:

$ ocamlformat --max-indent=2 --ocp-indent-compat test.ml
let lwt_apply f res =
  Lwt.bind res (fun r ->
    f r;
    res)

However, when I disable ocp-indent-compat, the fun lambda function is indented with 4 spaces (instead of expected 2) despite specifying max-indent=2:

$ ocamlformat --max-indent=2 --no-ocp-indent-compat test.ml
let lwt_apply f res =
  Lwt.bind res (fun r ->
      f r;
      res)

In this trivial example, ocp-indent-compat affects only spacing, but in a bigger projects, setting this flag to true introduces lots of other undesired changes, so it would be great to have consistent 2 spaces formatting everywhere without ocp-indent-compat=false!

My system settings:

$ ocamlformat --version
0.26.2

I'm MacOS Sonoma 14.5 (but I also see this issue on Linux).

@SGrondin
Copy link

SGrondin commented Aug 27, 2024

I can confirm the presence of this regression. It's preventing me from upgrading certain projects to OCaml 5.2.0 entirely!

Here's the .ocamlformat configuration I'm using:

break-cases = all
break-infix = fit-or-vertical
cases-exp-indent = 2
disambiguate-non-breaking-match = true
doc-comments = before
exp-grouping = preserve
field-space = tight-decl
function-indent = 0
if-then-else = keyword-first
indicate-multiline-delimiters = space
indicate-nested-or-patterns = space
let-and = sparse
margin = 106
max-indent = 2
module-item-spacing = sparse
parens-ite = true
parens-tuple = multi-line-only
single-case = sparse
type-decl = sparse

@Julow
Copy link
Collaborator

Julow commented Oct 30, 2024

The expected formatting is due to ocp-indent-compat and is the same regardless of the value of max-indent.
max-indent is a very challenging option to implement and maintain and is known to not work well in many cases. Help is welcome on fixing this issue.

For context, the reason for this 4-indentation is to avoid small change to the code causing a large indentation change:

let lwt_apply f res =
  Lwt.bind res (fun r ->
      f r;
      res)

let lwt_apply f res =
  Lwt.bind res (* Small change makes the argument list longer here *)
    (fun r ->
      f r; (* Indentation of the possibly long fun body didn't change. *)
      res)

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

No branches or pull requests

3 participants