-
Notifications
You must be signed in to change notification settings - Fork 207
allow begin end on one line in safe cases #2745
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
base: main
Are you sure you want to change the base?
Conversation
Julow
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have examples where begin .. end on a single line is better than parentheses ?
I'd find it more useful if OCamlformat removed begin .. end that fit on a single line. For example, if you modify a large expression inside a begin .. end until it becomes small, you end up with code like this (on main):
g ~y:begin
f x
endIf you allow this to fit on a single line, you get (with this PR):
g ~y:begin f x endwhich is a lot harder to read than it could be:
g ~y:(f x)|
I agree that parens is usually better, but removing beginend automatically can get annoying if you were in the process of modifying. I also worked quite a bit on a automatic begin-end bracketing feature, which was more ambitious than this (it also added begin end instead of multiline parens) and it was more complicated than you would think. The last issue I had is that formatting would not converge because in some cases, Of course, since the transformation is only one way here it would converge, but in some (rare) case you would get parens on different lines instead of your begin end clause. Also, something I should have mentionned is that a big motivation of this is all the begin end shortcut syntax I introduced. begin
fun x -> y
endbecame begin fun x -> y
endwhich begs to be on a single line (or to have parens) |
|
The complexity came exclusively from turning parens into begin..end. Removing begin..end is very easy: https://github.com/ocaml-ppx/ocamlformat/blob/main/lib/Extended_ast.ml#L282 I totally agree that this could be annoying while editing. Perhaps we could make the heuristic more complex to avoid some of these cases ? For example, don't remove begin..end on a match case if an other case has them, don't remove them in |
|
This looks a bit more complicated than I intended, I think this is okay to merge as-is, its always possible to remove the begin-end by hand. I would be okay with more functionnality in a later PR |
3099ce9 to
36ac4c7
Compare
you get
The only "unsafe" case is
which has a hovbox and is difficult to get without allowing the following:
Because of this
is forced