Skip to content

Commit 4c94d48

Browse files
authored
Fix 'wrap-comments' not working with the janestreet profile (#2645)
Fix asterisk-prefixed comments and the wrap-comments option Asterisk-prefixed comments were recognized as documentation with the janestreet profile and the wrap-comments option didn't have an effect.
1 parent 5bac2e7 commit 4c94d48

17 files changed

+92
-86
lines changed

CHANGES.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Items marked with an asterisk (\*) are changes that are likely to format
44
existing code differently from the previous release when using the default
55
profile. This started with version 0.26.0.
66

7+
## unreleased
8+
9+
### Fixed
10+
11+
- Fixed `wrap-comments=true` not working with the janestreet profile (#2645, @Julow)
12+
Asterisk-prefixed comments are also now formatted the same way as with the
13+
default profile.
14+
715
## 0.27.0
816

917
### Highlight

lib/Cmt.ml

+3-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let split_asterisk_prefixed =
146146

147147
let mk ?(prefix = "") ?(suffix = "") kind = {prefix; suffix; kind}
148148

149-
let decode_comment ~parse_comments_as_doc txt loc =
149+
let decode_comment txt loc =
150150
let txt =
151151
(* Windows compatibility *)
152152
let f = function '\r' -> false | _ -> true in
@@ -170,7 +170,6 @@ let decode_comment ~parse_comments_as_doc txt loc =
170170
| '=' -> mk (Verbatim txt)
171171
| _ when is_all_whitespace txt ->
172172
mk (Verbatim " ") (* Make sure not to format to [(**)]. *)
173-
| _ when parse_comments_as_doc -> mk (Doc txt)
174173
| _ -> (
175174
let lines =
176175
let content_offset = opn_offset + 2 in
@@ -194,6 +193,6 @@ let decode_docstring _loc = function
194193
| "\n" | " " -> mk (Verbatim " ")
195194
| txt -> mk ~prefix:"*" (Doc txt)
196195

197-
let decode ~parse_comments_as_doc = function
198-
| Comment {txt; loc} -> decode_comment ~parse_comments_as_doc txt loc
196+
let decode = function
197+
| Comment {txt; loc} -> decode_comment txt loc
199198
| Docstring {txt; loc} -> decode_docstring loc txt

lib/Cmt.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ type decoded =
4848
; suffix: string (** Just before the closing. *)
4949
; kind: decoded_kind }
5050

51-
val decode : parse_comments_as_doc:bool -> t -> decoded
51+
val decode : t -> decoded

lib/Cmts.ml

+9-5
Original file line numberDiff line numberDiff line change
@@ -581,18 +581,22 @@ end
581581

582582
let fmt_cmt (conf : Conf.t) cmt ~fmt_code =
583583
let open Fmt in
584-
let parse_comments_as_doc = conf.fmt_opts.ocp_indent_compat.v in
585-
let decoded = Cmt.decode ~parse_comments_as_doc cmt in
584+
let decoded = Cmt.decode cmt in
586585
(* TODO: Offset should be computed from location. *)
587586
let offset = 2 + String.length decoded.prefix in
588587
let pro = str "(*" $ str decoded.prefix
589588
and epi = str decoded.suffix $ str "*)" in
589+
let fmt_doc txt =
590+
Doc.fmt ~pro ~epi ~fmt_code conf ~loc:(Cmt.loc cmt) txt ~offset
591+
in
590592
match decoded.kind with
591593
| Verbatim txt -> Verbatim.fmt ~pro ~epi txt
592-
| Doc txt ->
593-
Doc.fmt ~pro ~epi ~fmt_code conf ~loc:(Cmt.loc cmt) txt ~offset
594+
| Doc txt -> fmt_doc txt
594595
| Normal txt ->
595-
if conf.fmt_opts.wrap_comments.v then Wrapped.fmt ~pro ~epi txt
596+
if
597+
conf.fmt_opts.ocp_indent_compat.v && conf.fmt_opts.parse_docstrings.v
598+
then fmt_doc txt
599+
else if conf.fmt_opts.wrap_comments.v then Wrapped.fmt ~pro ~epi txt
596600
else Unwrapped.fmt ~pro ~epi txt
597601
| Code code -> Cinaps.fmt ~pro ~epi ~fmt_code conf ~offset code
598602
| Asterisk_prefixed lines -> Asterisk_prefixed.fmt ~pro ~epi lines

lib/Normalize_extended_ast.ml

+7-3
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,18 @@ let make_mapper ~ignore_doc_comments ~normalize_doc =
152152
; typ }
153153

154154
let normalize_cmt (conf : Conf.t) =
155-
let parse_comments_as_doc = conf.fmt_opts.ocp_indent_compat.v in
155+
let parse_comments_as_doc =
156+
conf.fmt_opts.ocp_indent_compat.v && conf.fmt_opts.parse_docstrings.v
157+
in
156158
object (self)
157159
method cmt c =
158-
let decoded = Cmt.decode ~parse_comments_as_doc c in
160+
let decoded = Cmt.decode c in
159161
match decoded.Cmt.kind with
160162
| Verbatim txt -> txt
161163
| Doc txt -> self#doc txt
162-
| Normal txt -> Docstring.normalize_text txt
164+
| Normal txt ->
165+
if parse_comments_as_doc then self#doc txt
166+
else Docstring.normalize_text txt
163167
| Code txt -> self#code txt
164168
| Asterisk_prefixed lines ->
165169
String.concat ~sep:" " (List.map ~f:Docstring.normalize_text lines)

test/passing/refs.janestreet/comment_header.ml.ref

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type typ = typ
4545

4646
(* TEST
4747
arguments = "???"
48-
*)
48+
*)
4949

5050
(* On Windows the runtime expand windows wildcards (asterisks and
5151
* question marks).
@@ -57,4 +57,4 @@ type typ = typ
5757
*
5858
* The source code of this test is empty: we just check the arguments
5959
* expansion.
60-
* *)
60+
*)

test/passing/refs.janestreet/comments-no-wrap.ml.ref

+3-3
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,13 @@ let _ =
483483
*)
484484
();
485485
(* indentation preserved
486-
*)
486+
*)
487487
();
488488
(* indentation preserved
489-
*)
489+
*)
490490
();
491491
(* indentation not preserved
492-
*)
492+
*)
493493
()
494494
;;
495495

test/passing/refs.janestreet/comments.ml.ref

+3-3
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,13 @@ let _ =
483483
*)
484484
();
485485
(* indentation preserved
486-
*)
486+
*)
487487
();
488488
(* indentation preserved
489-
*)
489+
*)
490490
();
491491
(* indentation not preserved
492-
*)
492+
*)
493493
()
494494
;;
495495

test/passing/refs.janestreet/doc_comments-after.ml.ref

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ module A = struct
257257
end
258258

259259
(* Same with get_pure, except that when we have both "x = t" and "y = t" where t is a primed ident,
260-
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261-
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
260+
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261+
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
262262
let _ = ()
263263

264264
(** Tags without text *)

test/passing/refs.janestreet/doc_comments-before-except-val.ml.ref

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ module A = struct
257257
end
258258

259259
(* Same with get_pure, except that when we have both "x = t" and "y = t" where t is a primed ident,
260-
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261-
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
260+
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261+
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
262262
let _ = ()
263263

264264
(** Tags without text *)

test/passing/refs.janestreet/doc_comments-before.ml.ref

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ module A = struct
257257
end
258258

259259
(* Same with get_pure, except that when we have both "x = t" and "y = t" where t is a primed ident,
260-
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261-
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
260+
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261+
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
262262
let _ = ()
263263

264264
(** Tags without text *)

test/passing/refs.janestreet/doc_comments.ml.ref

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ module A = struct
257257
end
258258

259259
(* Same with get_pure, except that when we have both "x = t" and "y = t" where t is a primed ident,
260-
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261-
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
260+
* we add "x = y" to the result. This is crucial for the normalizer, as it tend to drop "x = t" before
261+
* processing "y = t". If we don't explicitly preserve "x = y", the normalizer cannot pick it up *)
262262
let _ = ()
263263

264264
(** Tags without text *)

test/passing/refs.janestreet/source.ml.ref

+6-5
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ let smaller : type a b. (a succ, b succ) le -> (a, b) le = function
16641664
type (_, _) diff = Diff : 'c nat * ('a, 'c, 'b) plus -> ('a, 'b) diff
16651665

16661666
(*
1667-
let rec diff : type a b. (a,b) le -> a nat -> b nat -> (a,b) diff =
1667+
let rec diff : type a b. (a,b) le -> a nat -> b nat -> (a,b) diff =
16681668
fun le a b ->
16691669
match a, b, le with
16701670
| NZ, m, _ -> Diff (m, PlusZ m)
@@ -5901,7 +5901,7 @@ module C : sig module L : module type of List end = A
59015901
include D'
59025902

59035903
(*
5904-
let () =
5904+
let () =
59055905
print_endline (Int.to_string D'.M.y)
59065906
*)
59075907
open A
@@ -6528,7 +6528,7 @@ end = struct
65286528
type refer = { poly : 'a 'b 'c. (('b, 'c) #Classdef.cl2 as 'a) }
65296529
end
65306530
(*
6531-
ocamlc -c pr3918a.mli pr3918b.mli
6531+
ocamlc -c pr3918a.mli pr3918b.mli
65326532
rm -f pr3918a.cmi
65336533
ocamlc -c pr3918c.ml
65346534
*)
@@ -7410,7 +7410,7 @@ let _ =
74107410
(* Early strict evaluation *)
74117411

74127412
(*
7413-
module rec Cyclic
7413+
module rec Cyclic
74147414
: sig val x : int end
74157415
= struct let x = Cyclic.x + 1 end
74167416
;;
@@ -7507,7 +7507,7 @@ end
75077507
(* Wrong LHS signatures (PR#4336) *)
75087508

75097509
(*
7510-
module type ASig = sig type a val a:a val print:a -> unit end
7510+
module type ASig = sig type a val a:a val print:a -> unit end
75117511
module type BSig = sig type b val b:b val print:b -> unit end
75127512

75137513
module A = struct type a = int let a = 0 let print = print_int end
@@ -7519,6 +7519,7 @@ module MakeB (Empty:sig end) : BSig = B
75197519
module
75207520
rec NewA : ASig = MakeA (struct end)
75217521
and NewB : BSig with type b = NewA.a = MakeB (struct end);;
7522+
75227523
*)
75237524

75247525
(* Expressions and bindings *)
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
Warning: unicode.ml:2 exceeds the margin
2-
Warning: unicode.ml:4 exceeds the margin
3-
Warning: unicode.ml:6 exceeds the margin
4-
Warning: unicode.ml:8 exceeds the margin
1+
Warning: unicode.ml:5 exceeds the margin
2+
Warning: unicode.ml:11 exceeds the margin
+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
(* Don't edit this file with an editor that perform unicode normalization *)
22

3-
(* normal78901234567890123456789012345678901234567890123456789012345678901 a bū c d e*)
3+
(* normal78901234567890123456789012345678901234567890123456789012345678901 a bū
4+
c d e*)
45

5-
(* modifier901234567890123456789012345678901234567890123456789012345678901 a bū̃ c d e*)
6+
(* modifier901234567890123456789012345678901234567890123456789012345678901 a bū̃
7+
c d e*)
68

7-
(* 12345678901234567890123456789012345678901234567890123456789012345678901 a yo c d e*)
9+
(* 12345678901234567890123456789012345678901234567890123456789012345678901 a yo
10+
c d e*)
811

9-
(* 12345678901234567890123456789012345678901234567890123456789012345678901 a y̲o c d e*)
12+
(* 12345678901234567890123456789012345678901234567890123456789012345678901 a y̲o
13+
c d e*)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Warning: wrap_comments.ml:4 exceeds the margin
2-
Warning: wrap_comments.ml:85 exceeds the margin
1+
Warning: wrap_comments.ml:72 exceeds the margin
2+
Warning: wrap_comments.ml:213 exceeds the margin
33
Warning: wrap_comments.ml:224 exceeds the margin
4-
Warning: wrap_comments.ml:235 exceeds the margin
5-
Warning: wrap_comments.ml:254 exceeds the margin
4+
Warning: wrap_comments.ml:243 exceeds the margin

0 commit comments

Comments
 (0)