Skip to content

Commit 1c4960b

Browse files
authored
Fix formatting of multiline non-wrapping comments (#1723)
1 parent 9bf5d9c commit 1c4960b

11 files changed

+624
-10
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
+ Do not consider leading star '*' when checking the diff of doc comments (#1712, @hhugo)
4242

43+
+ Fix formatting of multiline non-wrapping comments (#1723, @gpetiot)
44+
4345
#### Changes
4446

4547
+ Improve the diff of unstable docstrings displayed in error messages (#1654, @gpetiot)

lib/Cmt.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ let fmt_multiline_cmt ?epi ~opn_pos ~starts_with_sp first_line tl_lines =
9090
in
9191
fmt_if_k (not first) sep $ sp $ str (String.rstrip s)
9292
in
93-
vbox 0 (list_fl unindented fmt_line $ fmt_opt epi)
93+
vbox 0 ~name:"multiline" (list_fl unindented fmt_line $ fmt_opt epi)
9494

9595
type pos = Before | Within | After
9696

97-
let fmt cmt src ~wrap:wrap_comments ~ocp_indent_compat ~fmt_code pos =
97+
let fmt cmt ~wrap:wrap_comments ~ocp_indent_compat ~fmt_code pos =
9898
let open Fmt in
9999
let fmt_asterisk_prefixed_lines lines =
100100
vbox 1
@@ -106,7 +106,6 @@ let fmt cmt src ~wrap:wrap_comments ~ocp_indent_compat ~fmt_code pos =
106106
| _, Some _ -> str line $ fmt "@,*" ) )
107107
in
108108
let fmt_unwrapped_cmt {txt= s; loc} =
109-
let begins_line = Source.begins_line src loc ~ignore_spaces:false in
110109
let is_sp = function ' ' | '\t' -> true | _ -> false in
111110
let epi =
112111
(* Preserve position of closing but strip empty lines at the end *)
@@ -119,8 +118,7 @@ let fmt cmt src ~wrap:wrap_comments ~ocp_indent_compat ~fmt_code pos =
119118
in
120119
let stripped = String.rstrip s in
121120
match String.split_lines stripped with
122-
| first_line :: (_ :: _ as tl)
123-
when (not begins_line) && not (String.is_empty first_line) ->
121+
| first_line :: (_ :: _ as tl) when not (String.is_empty first_line) ->
124122
if ocp_indent_compat then
125123
(* Not adding artificial breaks and keeping the comment contents
126124
verbatim will not interfere with ocp-indent. *)
@@ -159,7 +157,7 @@ let fmt cmt src ~wrap:wrap_comments ~ocp_indent_compat ~fmt_code pos =
159157
match fmt_code source with
160158
| Ok formatted ->
161159
let cls : Fmt.s = if dollar_last then "$*)" else "*)" in
162-
hvbox 2
160+
hvbox 2 ~name:"code"
163161
(wrap "(*$" cls
164162
( fmt "@;" $ formatted
165163
$ fmt_if (String.length str > 2) "@;<1 -2>" ) )

lib/Cmt.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type pos = Before | Within | After
2525

2626
val fmt :
2727
t
28-
-> Source.t
2928
-> wrap:bool
3029
-> ocp_indent_compat:bool
3130
-> fmt_code:(string -> (Fmt.t, unit) Result.t)

lib/Cmts.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@ let fmt_cmts_aux t (conf : Conf.t) cmts ~fmt_code pos =
347347
let groups =
348348
List.group cmts ~break:(break_comment_group t.source conf.margin)
349349
in
350-
vbox 0
350+
vbox 0 ~name:"cmts"
351351
(list_pn groups (fun ~prev:_ group ~next ->
352352
( match group with
353353
| [] -> impossible "previous match"
354354
| [cmt] ->
355-
Cmt.fmt cmt t.source ~wrap:conf.wrap_comments
355+
Cmt.fmt cmt ~wrap:conf.wrap_comments
356356
~ocp_indent_compat:conf.ocp_indent_compat
357357
~fmt_code:(fmt_code conf) pos
358358
| group ->

test/passing/dune.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,18 @@
767767
(package ocamlformat)
768768
(action (diff tests/doc_comments-no-parse-docstrings.mli.ref doc_comments-no-parse-docstrings.mli.output)))
769769

770+
(rule
771+
(deps tests/.ocamlformat )
772+
(package ocamlformat)
773+
(action
774+
(with-outputs-to doc_comments-no-wrap.mli.output
775+
(run %{bin:ocamlformat} --no-wrap-comments %{dep:tests/doc_comments.mli}))))
776+
777+
(rule
778+
(alias runtest)
779+
(package ocamlformat)
780+
(action (diff tests/doc_comments-no-wrap.mli.ref doc_comments-no-wrap.mli.output)))
781+
770782
(rule
771783
(deps tests/.ocamlformat )
772784
(package ocamlformat)

test/passing/tests/cinaps.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let foo = foo
4747
crash and the second become a mess *)
4848

4949
(*$(**)"
50-
"*)
50+
"*)
5151

5252
(*$
5353
print_newline () ;

test/passing/tests/doc_comments-no-parse-docstrings.mli.ref

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,13 @@ val k : int
434434
Phasellus et nisi id neque ultrices vestibulum vitae non tortor. Mauris
435435
aliquet at risus sed rhoncus. Ut condimentum rhoncus orci, sit amet
436436
eleifend erat tempus quis. *)
437+
438+
(** {[(* a b *)]} *)
439+
440+
val a :
441+
fooooooooooooooooooooooooooo (** {[(* a b *)]} *)
442+
-> fooooooooooooooooooooooooo
443+
444+
type x = {a: t (** {[(* a b *)]} *); b: [`A (** {[(* a b *)]} *)]}
445+
446+
type x = A of a (** {[(* a b *)]} *) | B of b (** {[(* a b *)]} *)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--no-wrap-comments

0 commit comments

Comments
 (0)