Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of cinaps comments with strings #2349

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### Bug fixes

- Fix invalid formatting of `(::)` (#2347, @Julow)
- 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)
Expand Down
24 changes: 12 additions & 12 deletions lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. *)
Expand Down
5 changes: 5 additions & 0 deletions test/passing/tests/js_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7830,3 +7830,8 @@ class x =
foo

v}*)

(*$
{|
f|}
*)
5 changes: 5 additions & 0 deletions test/passing/tests/js_source.ml.ocp
Original file line number Diff line number Diff line change
Expand Up @@ -10072,3 +10072,8 @@ class x =
(*{v
foo
v}*)

(*$
{|
f|}
*)
5 changes: 5 additions & 0 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -10072,3 +10072,8 @@ class x =
(*{v
foo
v}*)

(*$
{|
f|}
*)