Skip to content

Commit a91d4a3

Browse files
authored
Improve formatting of assignment operators with comments (#2617)
* Improve formatting of assignment operators with comments Avoids outputting the assignment operator on the right of a long comment and removes the inconsistent 1-indent of the operator in some cases: let _ = - r (* _________________________________________________________________ *) := - 1 + r + (* _________________________________________________________________ *) + := 1 let _ = r - (* _________________________________________________________________ *) - (* _________________________________________________________________ *) - := 1 + (* _________________________________________________________________ *) + (* _________________________________________________________________ *) + := 1 Also fix an unstable comment in this case: let _ = aaaaaaa (* __________________________________________________________________________________ *) := bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Which now formats the same way as other infix operators: let _ = aaaaaaa (* __________________________________________________________________________________ *) <> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb * Update CHANGES
1 parent fb9b70f commit a91d4a3

File tree

7 files changed

+41
-18
lines changed

7 files changed

+41
-18
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ profile. This started with version 0.26.0.
8686

8787
### Fixed
8888

89-
- Fix placement of comments in some cases (#2471, #2503, #2506, #2540, #2541, #2592, @gpetiot, @Julow)
89+
- Fix placement of comments in some cases (#2471, #2503, #2506, #2540, #2541, #2592, #2617, @gpetiot, @Julow)
9090
Some comments were being moved or causing OCamlformat to crash.
9191
OCamlformat refuses to format if a comment would be missing in its output, to avoid loosing code.
9292

lib/Fmt_ast.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,15 +2014,16 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
20142014
| Pexp_infix ({txt= ":="; loc}, r, v)
20152015
when is_simple c.conf (expression_width c) (sub_exp ~ctx r) ->
20162016
let bol_indent = Params.Indent.assignment_operator_bol c.conf in
2017+
let has_cmts_before = Cmts.has_before c.cmts loc in
20172018
let cmts_before =
2018-
let indent, adj =
2019+
let indent =
20192020
(* Use the same break for comment and operator. Comments are placed
20202021
according to indentation. *)
20212022
match c.conf.fmt_opts.assignment_operator.v with
2022-
| `Begin_line -> (bol_indent, noop)
2023-
| `End_line -> (2, cut_break)
2023+
| `Begin_line -> bol_indent
2024+
| `End_line -> 0
20242025
in
2025-
Cmts.fmt_before c loc ~pro:(break 1 indent) ~epi:adj ~adj
2026+
Cmts.fmt_before c loc ~pro:(break 1 indent) ~epi:noop ~adj:noop
20262027
in
20272028
let cmts_after = Cmts.fmt_after c loc ~pro:noop ~epi:noop in
20282029
pro
@@ -2034,9 +2035,12 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
20342035
$ break 1 bol_indent $ str ":= " $ cmts_after
20352036
$ hvbox 2 (fmt_expression c (sub_exp ~ctx v))
20362037
| `End_line ->
2038+
let break_infix =
2039+
if has_cmts_before then break 1 0 else str " "
2040+
in
20372041
hvbox 0
2038-
( hvbox 0 (fmt_expression c (sub_exp ~ctx r) $ cmts_before)
2039-
$ str " :=" )
2042+
( fmt_expression c (sub_exp ~ctx r)
2043+
$ cmts_before $ break_infix $ str ":=" )
20402044
$ break 1 2 $ cmts_after
20412045
$ hvbox 2 (fmt_expression c (sub_exp ~ctx v)) ) )
20422046
| Pexp_prefix ({txt= ("~-" | "~-." | "~+" | "~+.") as op; loc}, e1) ->

test/passing/tests/assignment_operator-op_begin_line.ml.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Warning: tests/assignment_operator.ml:47 exceeds the margin
77
Warning: tests/assignment_operator.ml:52 exceeds the margin
88
Warning: tests/assignment_operator.ml:59 exceeds the margin
99
Warning: tests/assignment_operator.ml:60 exceeds the margin
10+
Warning: tests/assignment_operator.ml:65 exceeds the margin

test/passing/tests/assignment_operator-op_begin_line.ml.ref

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ let _ =
6060
:= (* _________________________________________________________________ *)
6161
(* _________________________________________________________________ *)
6262
1
63+
64+
let _ =
65+
aaaaaaa
66+
(* __________________________________________________________________________________ *)
67+
:= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

test/passing/tests/assignment_operator.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ let _ =
4848
let _ =
4949
r (* _________________________________________________________________ *) (* _________________________________________________________________ *) := (* _________________________________________________________________ *) (* _________________________________________________________________ *) 1
5050
;;
51+
52+
let _ =
53+
aaaaaaa
54+
(* __________________________________________________________________________________ *)
55+
:= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
56+
;;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Warning: tests/assignment_operator.ml:30 exceeds the margin
2-
Warning: tests/assignment_operator.ml:50 exceeds the margin
1+
Warning: tests/assignment_operator.ml:67 exceeds the margin

test/passing/tests/assignment_operator.ml.ref

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ let foo =
2828
foo
2929

3030
let _ =
31-
r (* _________________________________________________________________ *) :=
32-
1
31+
r
32+
(* _________________________________________________________________ *)
33+
:= 1
3334

3435
let _ =
3536
r
36-
(* _________________________________________________________________ *)
37-
(* _________________________________________________________________ *)
38-
:= 1
37+
(* _________________________________________________________________ *)
38+
(* _________________________________________________________________ *)
39+
:= 1
3940

4041
let _ =
4142
r :=
@@ -48,14 +49,21 @@ let _ =
4849
1
4950

5051
let _ =
51-
r (* _________________________________________________________________ *) :=
52+
r
53+
(* _________________________________________________________________ *)
54+
:=
5255
(* _________________________________________________________________ *) 1
5356

5457
let _ =
5558
r
56-
(* _________________________________________________________________ *)
57-
(* _________________________________________________________________ *)
58-
:=
59+
(* _________________________________________________________________ *)
60+
(* _________________________________________________________________ *)
61+
:=
5962
(* _________________________________________________________________ *)
6063
(* _________________________________________________________________ *)
6164
1
65+
66+
let _ =
67+
aaaaaaa
68+
(* __________________________________________________________________________________ *)
69+
:= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

0 commit comments

Comments
 (0)