Skip to content

Commit 099d6e1

Browse files
authored
Fix doc-comments '*)' exceeding the margin (ocaml-ppx#2550)
This change applies to doc-comments and regular comments parsed as doc-comments.
1 parent 356ec71 commit 099d6e1

20 files changed

+105
-76
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ profile. This started with version 0.26.0.
5050
- Fix the formatting of the `in` keyword when `[@ocamlformat disable]` is attached to a let-binding (#2242, @EmileTrotignon)
5151
- Fix comments around underscore in record patterns (#2540, @Julow)
5252
- Fix dropped comments before `begin .. end` in a match case (#2541, @Julow)
53+
- Fix closing `*)` in doc-comments exceeding the margin (#2550, @Julow)
5354

5455
### Changes
5556
- The location of attributes for structure items is now tracked and preserved. (#2247, @EmileTrotignon)

lib/Cmts.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,23 @@ module Doc = struct
554554
let l = List.last_exn lines in
555555
(is_only_whitespaces h, is_only_whitespaces l)
556556
in
557+
let force_trailing_space =
558+
String.length txt > 1 && String.ends_with_whitespace txt
559+
in
557560
let txt = if pre_nl then String.lstrip txt else txt in
558561
let txt = if trail_nl then String.rstrip txt else txt in
559562
let parsed = Docstring.parse ~loc txt in
560563
(* Disable warnings when parsing of code blocks fails. *)
561564
let quiet = Conf_t.Elt.make true `Default in
562565
let conf = {conf with Conf.opr_opts= {conf.Conf.opr_opts with quiet}} in
563-
let doc = Fmt_odoc.fmt_parsed conf ~fmt_code ~input:txt ~offset parsed in
564566
let open Fmt in
567+
let trailing_space =
568+
if (not trail_nl) && force_trailing_space then str " " else noop
569+
in
570+
let doc =
571+
Fmt_odoc.fmt_parsed conf ~trailing_space ~fmt_code ~input:txt ~offset
572+
parsed
573+
in
565574
hvbox 2
566575
( pro
567576
$ fmt_if pre_nl (break 1000 1)

lib/Fmt_ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ let fmt_parsed_docstring c ~loc ?pro ~epi input parsed =
413413
let doc = Fmt_odoc.fmt_parsed c.conf ~fmt_code ~offset ~input parsed in
414414
Cmts.fmt c loc
415415
@@ vbox_if (Option.is_none pro) 0
416-
(fmt_opt pro $ wrap (str "(**") (str "*)") doc $ epi)
416+
(fmt_opt pro $ hovbox 0 (str "(**" $ doc $ str "*)") $ epi)
417417

418418
let docstring_epi ~standalone ~next ~epi ~floating =
419419
let epi = if Option.is_some next then force_newline else fmt_opt epi in

lib/Fmt_odoc.ml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,27 @@ let beginning_offset (conf : Conf.t) input =
372372
whitespace_count
373373
else min whitespace_count 1
374374

375-
let fmt_parsed (conf : Conf.t) ~fmt_code ~input ~offset parsed =
375+
let fmt_parsed (conf : Conf.t) ?trailing_space ~fmt_code ~input ~offset
376+
parsed =
376377
let open Fmt in
377378
let begin_offset = beginning_offset conf input in
378379
(* The offset is used to adjust the margin when formatting code blocks. *)
379380
let offset = offset + begin_offset in
380381
let fmt_code conf ~offset:offset' ~set_margin input =
381382
fmt_code conf ~offset:(offset + offset') ~set_margin input
382383
in
384+
let trailing_space =
385+
match trailing_space with
386+
| Some sp -> sp
387+
| None ->
388+
fmt_if
389+
(String.length input > 1 && String.ends_with_whitespace input)
390+
space_break
391+
in
383392
let fmt_parsed parsed =
384393
str (String.make begin_offset ' ')
385394
$ fmt_ast conf ~fmt_code parsed
386-
$ fmt_if
387-
(String.length input > 1 && String.ends_with_whitespace input)
388-
(str " ")
395+
$ trailing_space
389396
in
390397
match parsed with
391398
| _ when not conf.fmt_opts.parse_docstrings.v -> str input

lib/Fmt_odoc.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ val fmt_ast :
2323

2424
val fmt_parsed :
2525
Conf.t
26+
-> ?trailing_space:Fmt.t
2627
-> fmt_code:fmt_code
2728
-> input:string
2829
-> offset:int
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
Warning: tests/break_separators.ml:289 exceeds the margin

test/passing/tests/break_separators-after.ml.ref

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ let foooooooooooo =
287287

288288
let foooooooooooo =
289289
{ foooooooooooooo with
290-
(* foooooooooooooooo fooooooooooooooooooooooooo foooooooooooooooooooooo *)
290+
(* foooooooooooooooo fooooooooooooooooooooooooo foooooooooooooooooooooo
291+
*)
291292
fooooooooooooooooooooooooooooo= fooooooooooooo;
292293
fooooooooooooo= foooooooooooooo }
293294

@@ -381,6 +382,7 @@ let () =
381382
match x with
382383
| ( _,
383384
(* verrrrrrrrrrrrrrrrrrrrrrrry looooooooooooooooong line 1
384-
veeeeeeeeeeeeeeeeeeeeeryyyy loooooooooooooooooong line 2 *)
385+
veeeeeeeeeeeeeeeeeeeeeryyyy loooooooooooooooooong line 2
386+
*)
385387
Some _ ) ->
386388
x
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Warning: tests/break_separators.ml:324 exceeds the margin
2-
Warning: tests/break_separators.ml:334 exceeds the margin
1+
Warning: tests/break_separators.ml:335 exceeds the margin

test/passing/tests/break_separators-after_docked.ml.ref

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ let foooooooooooo =
322322
let foooooooooooo =
323323
{
324324
foooooooooooooo with
325-
(* foooooooooooooooo fooooooooooooooooooooooooo foooooooooooooooooooooo *)
325+
(* foooooooooooooooo fooooooooooooooooooooooooo foooooooooooooooooooooo
326+
*)
326327
fooooooooooooooooooooooooooooo= fooooooooooooo;
327328
fooooooooooooo= foooooooooooooo;
328329
}
@@ -430,6 +431,7 @@ let () =
430431
match x with
431432
| ( _,
432433
(* verrrrrrrrrrrrrrrrrrrrrrrry looooooooooooooooong line 1
433-
veeeeeeeeeeeeeeeeeeeeeryyyy loooooooooooooooooong line 2 *)
434+
veeeeeeeeeeeeeeeeeeeeeryyyy loooooooooooooooooong line 2
435+
*)
434436
Some _ ) ->
435437
x
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
Warning: tests/break_separators.ml:324 exceeds the margin

0 commit comments

Comments
 (0)