From a0c9aaeeda5386cb88b10d3ad63f4ab35abe97fc Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 7 Nov 2023 09:57:58 +0100 Subject: [PATCH] Don't trim first line of asterisk prefixed comment --- lib/Cmt.ml | 38 ++++++++++--------------- test/passing/tests/wrap_comments.ml | 12 ++++++++ test/passing/tests/wrap_comments.ml.err | 14 ++++----- test/passing/tests/wrap_comments.ml.ref | 12 ++++++++ 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lib/Cmt.ml b/lib/Cmt.ml index b35ebfb759..5798f51f09 100644 --- a/lib/Cmt.ml +++ b/lib/Cmt.ml @@ -98,22 +98,24 @@ type decoded = {prefix: string; suffix: string; kind: decoded_kind} indentation to trim. *) let unindent_lines ?(max_indent = Stdlib.max_int) ~content_offset first_line tl_lines = - (* The indentation of the first line must account for the location of the - comment opening. Don't account for the first line if it's empty. *) - let fl_spaces, fl_indent = - match String.indent_of_line first_line with - | Some i -> (i, i + content_offset - 1) - | None -> (String.length first_line, Stdlib.max_int) - in - let fl_indent = min max_indent fl_indent in - let min_indent = - List.fold_left ~init:fl_indent + let tl_indent = + List.fold_left ~init:max_indent ~f:(fun acc s -> Option.value_map ~default:acc ~f:(min acc) (String.indent_of_line s) ) tl_lines in - (* Completely trim the first line *) - String.drop_prefix first_line fl_spaces + (* The indentation of the first line must account for the location of the + comment opening. Don't account for the first line if it's empty. + [fl_trim] is the number of characters to remove from the first line. *) + let fl_trim, fl_indent = + match String.indent_of_line first_line with + | Some i -> + (max 0 (min i (tl_indent - content_offset)), i + content_offset - 1) + | None -> (String.length first_line, max_indent) + in + let min_indent = min tl_indent fl_indent in + let first_line = String.drop_prefix first_line fl_trim in + first_line :: List.map ~f:(fun s -> String.drop_prefix s min_indent) tl_lines let unindent_lines ?max_indent ~content_offset txt = @@ -174,18 +176,8 @@ let decode_comment ~parse_comments_as_doc txt loc = let content_offset = opn_offset + 2 in unindent_lines ~content_offset txt in - (* Don't add a space to the prefix if the first line was only - spaces. *) - let prefix = - if - String.starts_with_whitespace txt - && not (is_all_whitespace (List.hd_exn lines)) - then " " - else "" - in match split_asterisk_prefixed lines with - | Some deprefixed_lines -> - mk ~prefix (Asterisk_prefixed deprefixed_lines) + | Some deprefixed_lines -> mk (Asterisk_prefixed deprefixed_lines) | None -> mk (Normal txt) ) else match txt with diff --git a/test/passing/tests/wrap_comments.ml b/test/passing/tests/wrap_comments.ml index 0632cc50e5..f2002adf0d 100644 --- a/test/passing/tests/wrap_comments.ml +++ b/test/passing/tests/wrap_comments.ml @@ -70,6 +70,12 @@ let _ = *) a +(* 1 + * + 2 + * --- + * 3 + *) + [@@@ocamlformat "wrap-comments=false"] type t = @@ -245,3 +251,9 @@ let _ = (* foo *) a + +(* 1 + * + 2 + * --- + * 3 + *) diff --git a/test/passing/tests/wrap_comments.ml.err b/test/passing/tests/wrap_comments.ml.err index 22d75f70b4..ed65e4c631 100644 --- a/test/passing/tests/wrap_comments.ml.err +++ b/test/passing/tests/wrap_comments.ml.err @@ -1,19 +1,19 @@ -Warning: tests/wrap_comments.ml:53 exceeds the margin -Warning: tests/wrap_comments.ml:178 exceeds the margin -Warning: tests/wrap_comments.ml:179 exceeds the margin -Warning: tests/wrap_comments.ml:180 exceeds the margin +Warning: tests/wrap_comments.ml:59 exceeds the margin Warning: tests/wrap_comments.ml:184 exceeds the margin Warning: tests/wrap_comments.ml:185 exceeds the margin Warning: tests/wrap_comments.ml:186 exceeds the margin -Warning: tests/wrap_comments.ml:189 exceeds the margin Warning: tests/wrap_comments.ml:190 exceeds the margin Warning: tests/wrap_comments.ml:191 exceeds the margin +Warning: tests/wrap_comments.ml:192 exceeds the margin +Warning: tests/wrap_comments.ml:195 exceeds the margin Warning: tests/wrap_comments.ml:196 exceeds the margin Warning: tests/wrap_comments.ml:197 exceeds the margin -Warning: tests/wrap_comments.ml:198 exceeds the margin Warning: tests/wrap_comments.ml:202 exceeds the margin Warning: tests/wrap_comments.ml:203 exceeds the margin Warning: tests/wrap_comments.ml:204 exceeds the margin -Warning: tests/wrap_comments.ml:207 exceeds the margin Warning: tests/wrap_comments.ml:208 exceeds the margin Warning: tests/wrap_comments.ml:209 exceeds the margin +Warning: tests/wrap_comments.ml:210 exceeds the margin +Warning: tests/wrap_comments.ml:213 exceeds the margin +Warning: tests/wrap_comments.ml:214 exceeds the margin +Warning: tests/wrap_comments.ml:215 exceeds the margin diff --git a/test/passing/tests/wrap_comments.ml.ref b/test/passing/tests/wrap_comments.ml.ref index 91de5455c2..2f2be32386 100644 --- a/test/passing/tests/wrap_comments.ml.ref +++ b/test/passing/tests/wrap_comments.ml.ref @@ -47,6 +47,12 @@ let _ = let _ = f (* foo *) a +(* 1 + * + 2 + * --- + * 3 + *) + [@@@ocamlformat "wrap-comments=false"] type t = @@ -213,3 +219,9 @@ let _ = let _ = f (* foo *) a + +(* 1 + * + 2 + * --- + * 3 + *)