Skip to content

Commit b0664d4

Browse files
authored
Do not consider leading star '*' when checking the diff of doc comments (#1712)
Fixup #1695
1 parent edf3c9d commit b0664d4

14 files changed

+575
-21
lines changed

CHANGES.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@
3434

3535
+ Fix parentheses around successive unary operations (#1696, @gpetiot)
3636

37-
+ Fix normalization of odoc paragraphs (#1695, @gpetiot)
38-
Paragraphs must not be considered for the normalized form. They are only structural and the shape of a docstring organized in paragraphs only depend on linebreaks, which may be changed by the formatting.
39-
4037
+ Add missing break between pattern and attribute (#1711, @gpetiot)
4138

4239
+ Add missing parentheses around expression having attributes or comments inside a shorthand let-open clause (#1708, @gpetiot)
4340

41+
+ Do not consider leading star '*' when checking the diff of doc comments (#1712, @hhugo)
42+
4443
#### Changes
4544

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

dune-project

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
base-unix
4848
cmdliner
4949
dune-build-info
50-
either
5150
fix
5251
fpath
5352
(menhir
@@ -90,7 +89,6 @@
9089
base-unix
9190
cmdliner
9291
dune-build-info
93-
either
9492
fix
9593
fpath
9694
(menhir

lib/Normalize.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ and odoc_inline_elements fmt elems =
9393
list (ign_loc odoc_inline_element) fmt elems
9494

9595
let rec odoc_nestable_block_element c fmt = function
96-
| `Paragraph elms ->
97-
(* Paragraphs must not be considered for the normalized form. They are
98-
only structural and the shape of a docstring organized in paragraphs
99-
only depend on linebreaks, which may be changed by the
100-
formatting. *)
101-
odoc_inline_elements fmt elms
96+
| `Paragraph elms -> fpf fmt "Paragraph(%a)" odoc_inline_elements elms
10297
| `Code_block (_, txt) ->
10398
let txt = Odoc_parser.Loc.value txt in
10499
let txt =

lib/Translation_unit.ml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,24 @@ let format (type a b) (fg0 : a Ast_passes.Ast0.t)
353353
( match Cmts.remaining_comments cmts_t with
354354
| [] -> ()
355355
| l -> internal_error (`Comment_dropped l) [] ) ;
356-
let is_docstring Cmt.{txt; _} =
357-
conf.parse_docstrings && Char.equal txt.[0] '*'
356+
let is_docstring (Cmt.{txt; loc} as cmt) =
357+
match txt with
358+
| "" | "*" -> Either.Second cmt
359+
| _ when Char.equal txt.[0] '*' ->
360+
(* Doc comments here (comming directly from the lexer) include
361+
their leading star *. It is not part of the docstring and
362+
should be dropped. *)
363+
let txt = String.drop_prefix txt 1 in
364+
let cmt = Cmt.create txt loc in
365+
if conf.parse_docstrings then Either.First cmt
366+
else Either.Second cmt
367+
| _ -> Either.Second cmt
358368
in
359369
let old_docstrings, old_comments =
360-
List.partition_tf t.comments ~f:is_docstring
370+
List.partition_map t.comments ~f:is_docstring
361371
in
362372
let t_newdocstrings, t_newcomments =
363-
List.partition_tf t_new.comments ~f:is_docstring
373+
List.partition_map t_new.comments ~f:is_docstring
364374
in
365375
let diff_cmts =
366376
Sequence.append

ocamlformat-rpc.opam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ depends: [
1818
"base-unix"
1919
"cmdliner"
2020
"dune-build-info"
21-
"either"
2221
"fix"
2322
"fpath"
2423
"menhir" {>= "20180528"}

ocamlformat.opam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ depends: [
1717
"base-unix"
1818
"cmdliner"
1919
"dune-build-info"
20-
"either"
2120
"fix"
2221
"fpath"
2322
"menhir" {>= "20180528"}

test/passing/dune.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,18 @@
755755
(package ocamlformat)
756756
(action (diff tests/doc_comments-before.ml.ref doc_comments-before.ml.output)))
757757

758+
(rule
759+
(deps tests/.ocamlformat )
760+
(package ocamlformat)
761+
(action
762+
(with-outputs-to doc_comments-parse-docstrings.mli.output
763+
(run %{bin:ocamlformat} --parse-docstrings %{dep:tests/doc_comments.mli}))))
764+
765+
(rule
766+
(alias runtest)
767+
(package ocamlformat)
768+
(action (diff tests/doc_comments-parse-docstrings.mli.ref doc_comments-parse-docstrings.mli.output)))
769+
758770
(rule
759771
(deps tests/.ocamlformat )
760772
(package ocamlformat)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--parse-docstrings

0 commit comments

Comments
 (0)