From c3b3d10d0fb88b735a3b4c8c7c4a09be3611189f Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Mon, 24 Apr 2023 17:31:50 +0200 Subject: [PATCH 1/3] Fix formatting of cinaps comments with strings Strings containing breaks must carefully avoid changing the content of strings while indenting the whole code. --- lib/Cmts.ml | 24 ++++++++++++------------ test/passing/tests/js_source.ml | 5 +++++ test/passing/tests/js_source.ml.ocp | 5 +++++ test/passing/tests/js_source.ml.ref | 5 +++++ 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/Cmts.ml b/lib/Cmts.ml index 9c51e0ced4..7a23640d7e 100644 --- a/lib/Cmts.ml +++ b/lib/Cmts.ml @@ -566,17 +566,17 @@ module Cinaps = struct open Fmt (** Comments enclosed in [(*$], [$*)] are formatted as code. *) - let fmt ~cls code = - let wrap k = hvbox 2 (fmt "(*$" $ k $ fmt cls) in - match String.split_lines code with - | [] | [""] -> wrap (str " ") - | [line] -> wrap (fmt "@ " $ str line $ fmt "@;<1 -2>") - | lines -> - let fmt_line = function - | "" -> fmt "\n" - | line -> fmt "@\n" $ str line - in - wrap (list lines "" fmt_line $ fmt "@;<1000 -2>") + let fmt ~opn_pos ~cls code = + let code = + match String.split_lines code with + | [] | [""] -> noop + | [line] -> fmt "@ " $ str line + | first_line :: tl_lines -> + fmt "@," + $ Unwrapped.fmt_multiline_cmt ~opn_pos ~starts_with_sp:false + first_line tl_lines + in + hvbox 2 (fmt "(*$" $ code $ fmt "@;<1 -2>" $ fmt cls) end module Ocp_indent_compat = struct @@ -648,7 +648,7 @@ let fmt_cmt (conf : Conf.t) (cmt : Cmt.t) ~fmt_code pos = let open Fmt in match mode with | `Verbatim x -> Verbatim.fmt x pos - | `Code (code, cls) -> Cinaps.fmt ~cls code + | `Code (code, cls) -> Cinaps.fmt ~opn_pos:cmt.Cmt.loc.loc_start ~cls code | `Wrapped (x, epi) -> str "(*" $ fill_text x ~epi | `Unwrapped (x, ln) when conf.fmt_opts.ocp_indent_compat.v -> (* TODO: [offset] should be computed from location. *) diff --git a/test/passing/tests/js_source.ml b/test/passing/tests/js_source.ml index 9c49d5b9a2..d33182257c 100644 --- a/test/passing/tests/js_source.ml +++ b/test/passing/tests/js_source.ml @@ -7830,3 +7830,8 @@ class x = foo v}*) + +(*$ + {| + f|} +*) diff --git a/test/passing/tests/js_source.ml.ocp b/test/passing/tests/js_source.ml.ocp index 494d8e409f..8360de5914 100644 --- a/test/passing/tests/js_source.ml.ocp +++ b/test/passing/tests/js_source.ml.ocp @@ -10072,3 +10072,8 @@ class x = (*{v foo v}*) + +(*$ + {| + f|} +*) diff --git a/test/passing/tests/js_source.ml.ref b/test/passing/tests/js_source.ml.ref index f14c9f2363..25eb067870 100644 --- a/test/passing/tests/js_source.ml.ref +++ b/test/passing/tests/js_source.ml.ref @@ -10072,3 +10072,8 @@ class x = (*{v foo v}*) + +(*$ + {| + f|} +*) From 3b830509602bf5768260e42f54e7adcba482357b Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Mon, 24 Apr 2023 17:35:40 +0200 Subject: [PATCH 2/3] Update Changes --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 8a66ce84de..656c7ef0c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,7 @@ ### Bug fixes -- Fix formatting of string literals in code blocks (#2338, @Julow) +- Fix formatting of string literals in code blocks (#2338, #2349, @Julow) - Improve formatting of module arguments (#2322, @Julow) - Consistent indentation of `@@ let+ x = ...` (#2315, @Julow) - Remove double parenthesis around tuple in a match (#2308, @Julow) From ad632eda262e63e4cfc35738a163384ac24e2500 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 25 Apr 2023 11:09:59 +0200 Subject: [PATCH 3/3] Fix changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 9708088ba9..0c63dff096 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ ### Bug fixes - Fix invalid formatting of `(::)` (#2347, @Julow) +- Fix formatting of string literals in code blocks (#2338, #2349, @Julow) - Improve formatting of module arguments (#2322, @Julow) - Consistent indentation of `@@ let+ x = ...` (#2315, @Julow) - Remove double parenthesis around tuple in a match (#2308, @Julow)