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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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 *)

0 commit comments

Comments
 (0)