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

New value 'vertical' for option 'if-then-else' #2174

Merged
merged 3 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

- Add a `break-colon` option to decide whether to break before or after the `:` symbol in value binding declarations and type constraints. This behavior is no longer ensured by `ocp-indent-compat`. (#2149, @gpetiot)
- Format `.mld` files as odoc documentation files (#2008, @gpetiot)
- New value `vertical` for option `if-then-else` (#2174, @gpetiot)

## 0.24.1 (2022-07-18)

Expand Down
5 changes: 4 additions & 1 deletion lib/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type fmt_opts =
; field_space: [`Tight | `Loose | `Tight_decl]
; function_indent: int
; function_indent_nested: [`Always | `Auto | `Never]
; if_then_else: [`Compact | `Fit_or_vertical | `Keyword_first | `K_R]
; if_then_else:
[`Compact | `Fit_or_vertical | `Vertical | `Keyword_first | `K_R]
; indent_after_in: int
; indicate_multiline_delimiters: [`No | `Space | `Closing_on_separate_line]
; indicate_nested_or_patterns: [`Space | `Unsafe_no]
Expand Down Expand Up @@ -662,6 +663,8 @@ module Formatting = struct
; C.Value.make ~name:"fit-or-vertical" `Fit_or_vertical
"$(b,fit-or-vertical) vertically breaks branches if they do not \
fit on a single line."
; C.Value.make ~name:"vertical" `Vertical
"$(b,vertical) always vertically breaks branches."
; C.Value.make ~name:"keyword-first" `Keyword_first
"$(b,keyword-first) formats if-then-else expressions such that \
the if-then-else keywords are the first on the line."
Expand Down
3 changes: 2 additions & 1 deletion lib/Conf.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type fmt_opts =
; field_space: [`Tight | `Loose | `Tight_decl]
; function_indent: int
; function_indent_nested: [`Always | `Auto | `Never]
; if_then_else: [`Compact | `Fit_or_vertical | `Keyword_first | `K_R]
; if_then_else:
[`Compact | `Fit_or_vertical | `Vertical | `Keyword_first | `K_R]
; indent_after_in: int
; indicate_multiline_delimiters: [`No | `Space | `Closing_on_separate_line]
; indicate_nested_or_patterns: [`Space | `Unsafe_no]
Expand Down
2 changes: 1 addition & 1 deletion lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
$ p.box_keyword_and_expr
( p.branch_pro
$ p.wrap_parens
( fmt_expression c ~box:false
( fmt_expression c ?box:p.box_expr
~parens:symbol_parens ?pro:p.expr_pro
?eol:p.expr_eol xbch
$ p.break_end_branch ) ) )
Expand Down
25 changes: 25 additions & 0 deletions lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ type if_then_else =
; box_keyword_and_expr: Fmt.t -> Fmt.t
; branch_pro: Fmt.t
; wrap_parens: Fmt.t -> Fmt.t
; box_expr: bool option
; expr_pro: Fmt.t option
; expr_eol: Fmt.t option
; break_end_branch: Fmt.t
Expand Down Expand Up @@ -385,6 +386,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens ~parens_bch
(get_parens_breaks
~opn_hint:((1, 0), (0, 0))
~cls_hint:((1, 0), (1000, -2)) )
; box_expr= Some false
; expr_pro= None
; expr_eol= None
; break_end_branch= noop
Expand All @@ -395,6 +397,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens ~parens_bch
; box_keyword_and_expr= Fn.id
; branch_pro
; wrap_parens= wrap_parens ~wrap_breaks:(wrap_k (break 1000 2) noop)
; box_expr= Some false
; expr_pro= None
; expr_eol= Some (fmt "@;<1 2>")
; break_end_branch=
Expand All @@ -415,6 +418,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens ~parens_bch
(get_parens_breaks
~opn_hint:((1, 2), (0, 2))
~cls_hint:((1, 0), (1000, 0)) )
; box_expr= Some false
; expr_pro=
Some
(fmt_if_k
Expand All @@ -427,6 +431,26 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens ~parens_bch
( match imd with
| `Closing_on_separate_line when beginend || parens_bch -> " "
| _ -> "@ " ) }
| `Vertical ->
{ box_branch= Fn.id
; cond= cond ()
; box_keyword_and_expr= Fn.id
; branch_pro
; wrap_parens=
wrap_parens
~wrap_breaks:
(get_parens_breaks
~opn_hint:((1, 2), (0, 2))
~cls_hint:((1, 0), (1000, 0)) )
; box_expr= None
; expr_pro= Some (break_unless_newline 1000 2)
; expr_eol= None
; break_end_branch= noop
; space_between_branches=
fmt
( match imd with
| `Closing_on_separate_line when parens_bch -> " "
| _ -> "@ " ) }
| `Keyword_first ->
{ box_branch= Fn.id
; cond=
Expand All @@ -448,6 +472,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens ~parens_bch
(get_parens_breaks
~opn_hint:((1, 0), (0, 0))
~cls_hint:((1, 0), (1000, -2)) )
; box_expr= Some false
; expr_pro= None
; expr_eol= None
; break_end_branch= noop
Expand Down
1 change: 1 addition & 0 deletions lib/Params.mli
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type if_then_else =
; box_keyword_and_expr: Fmt.t -> Fmt.t
; branch_pro: Fmt.t
; wrap_parens: Fmt.t -> Fmt.t
; box_expr: bool option
; expr_pro: Fmt.t option
; expr_eol: Fmt.t option
; break_end_branch: Fmt.t
Expand Down
12 changes: 6 additions & 6 deletions ocamlformat-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ OPTIONS (CODE FORMATTING STYLE)
block starts a line. always always apply function-indent. auto
applies function-indent when seen fit. The default value is never.

--if-then-else={compact|fit-or-vertical|keyword-first|k-r}
--if-then-else={compact|fit-or-vertical|vertical|keyword-first|k-r}
If-then-else formatting. compact tries to format an if-then-else
expression on a single line. fit-or-vertical vertically breaks
branches if they do not fit on a single line. keyword-first
formats if-then-else expressions such that the if-then-else
keywords are the first on the line. k-r formats if-then-else
expressions with parentheses that match the K&R style. The default
value is compact.
branches if they do not fit on a single line. vertical always
vertically breaks branches. keyword-first formats if-then-else
expressions such that the if-then-else keywords are the first on
the line. k-r formats if-then-else expressions with parentheses
that match the K&R style. The default value is compact.

--indent-after-in=COLS
Indentation (COLS columns) after `let ... in`, unless followed by
Expand Down
18 changes: 18 additions & 0 deletions test/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,24 @@
(package ocamlformat)
(action (diff tests/ite-no_indicate.ml.err ite-no_indicate.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
(action
(with-stdout-to ite-vertical.ml.stdout
(with-stderr-to ite-vertical.ml.stderr
(run %{bin:ocamlformat} --margin-check --if-then-else=vertical %{dep:tests/ite.ml})))))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/ite-vertical.ml.ref ite-vertical.ml.stdout)))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/ite-vertical.ml.err ite-vertical.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
Expand Down
6 changes: 6 additions & 0 deletions test/passing/tests/ite-compact.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ let _ =
else if bar then ( * )
else if foobar then ( / )
else ( - )

let _ =
if x then fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
6 changes: 6 additions & 0 deletions test/passing/tests/ite-compact_closing.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ let _ =
else if bar then ( * )
else if foobar then ( / )
else ( - )

let _ =
if x then fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
8 changes: 8 additions & 0 deletions test/passing/tests/ite-fit_or_vertical.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,11 @@ let _ =
( / )
else
( - )

let _ =
if x then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
8 changes: 8 additions & 0 deletions test/passing/tests/ite-fit_or_vertical_closing.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,11 @@ let _ =
( / )
else
( - )

let _ =
if x then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
8 changes: 8 additions & 0 deletions test/passing/tests/ite-fit_or_vertical_no_indicate.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,11 @@ let _ =
( / )
else
( - )

let _ =
if x then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
8 changes: 8 additions & 0 deletions test/passing/tests/ite-kr.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ let _ =
( / )
else
( - )

let _ =
if x then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
8 changes: 8 additions & 0 deletions test/passing/tests/ite-kr_closing.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,11 @@ let _ =
( / )
else
( - )

let _ =
if x then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
9 changes: 9 additions & 0 deletions test/passing/tests/ite-kw_first.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ let _ =
else if foobar
then ( / )
else ( - )

let _ =
if x
then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
9 changes: 9 additions & 0 deletions test/passing/tests/ite-kw_first_closing.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,12 @@ let _ =
else if foobar
then ( / )
else ( - )

let _ =
if x
then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
9 changes: 9 additions & 0 deletions test/passing/tests/ite-kw_first_no_indicate.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,12 @@ let _ =
else if foobar
then ( / )
else ( - )

let _ =
if x
then
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else
fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
6 changes: 6 additions & 0 deletions test/passing/tests/ite-no_indicate.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@ let _ =
else if bar then ( * )
else if foobar then ( / )
else ( - )

let _ =
if x then fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
else fun xxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyy zzzzzzzzzzz ->
xxxxxxxxx yyyyyyyyyy zzzzzzzzzzzz
Empty file.
1 change: 1 addition & 0 deletions test/passing/tests/ite-vertical.ml.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--if-then-else=vertical
Loading