ocamlformat
ocamlformat copied to clipboard
Reduced indentation / more sensible "max indentation" behavior.
What is the reason for this new style? I'm not sure whether there already exists a setting I can toggle to achieve the effect I want, so if there is please point me in that direction. The code in question looks like this:
let foo =
my_list
|> List.map ~f:(function
| PATTERN -> body
| PATTERN -> body)
;;
This indentation is a lot more aggressive than I would like.
Describe the solution you'd like
I think the behavior I would want is for the | symbols to align with the L in List, or if not the L then at least with the s.
I tried setting max-indent to 2, which produced the following:
let foo =
my_list
|> List.map ~f:(function
| PATTERN -> body
| PATTERN -> body)
That's definitely not what I want, and I imagine that's not what anyone who sets max-indent would want. I think the desirable indentation of successive lines should be re-evaluated given where max-indent allows the previous ones to be placed. It seems like the second pattern is trying to achieve that original aggressive indentation.
So 2 problems / questions. (1) how can I achieve the desired effect, and (2) can the behavior of max-indent be fixed?
Ocamlformat version: 0.11.0
Dune project has (using fmt 1.2)
My .ocamlformat file just has profile = janestreet (and I added a line for max-indent when I tried using the max-indent feature as described).
Hi, there is already #925 opened, I was working on a fix #997 (not a lot of feedback so far) but I'm not a fan of implementing that in our vendored Format library, so I will try thinking of another solution.
Okay. I’m still interested in the other part of the question, which is what setting I need to toggle to change this aggressive indentation without just constraining the global max.
The situation should have improved now that #1118 is merged, but that can't be perfect because we don't directly manipulate the real indentation as decided by Format.
Unfortunately you can't have more control over the indentation as a user with ocamlformat, because we only manipulate the level of indentation for each box level.
Some cases like:
|> List.map ~f:(function
| PATTERN -> body
| PATTERN -> body)
require a special treatment because the output has been modified to please the eye, if we kept the "natural" box structure it would have been:
my_list
|>
List.map
~f:
(function
| PATTERN -> body
| PATTERN -> body)
here you can see the indentation of each box is consistent with the corresponding level of the ast, explaining the weird indentation in your case.
Let us know if we can close now that #1118 is merged.
I agree that the first is an improvement over the second, but I'm still unclear why the first isn't a configurable setting. A similar (simpler?) example I found:
let families =
List.filter_map ifaddrs ~f:(fun { Ifaddr.name; address; family; _ } ->
Option.some_if (Option.is_some address && String.equal ifaddr_name name) family)
in
...
If there is already special treatment here can't we allow a setting for "normal" indentation (in this case to the 's' of List) for these cases instead of hard-coding a setting that gives them extra indentation?