Skip to content

Commit

Permalink
Don't trim first line of asterisk prefixed comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow committed Nov 7, 2023
1 parent a400535 commit a0c9aae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
38 changes: 15 additions & 23 deletions lib/Cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/passing/tests/wrap_comments.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ let _ =
*)
a

(* 1
* + 2
* ---
* 3
*)

[@@@ocamlformat "wrap-comments=false"]

type t =
Expand Down Expand Up @@ -245,3 +251,9 @@ let _ =
(* foo
*)
a

(* 1
* + 2
* ---
* 3
*)
14 changes: 7 additions & 7 deletions test/passing/tests/wrap_comments.ml.err
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/passing/tests/wrap_comments.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ let _ =

let _ = f (* foo *) a

(* 1
* + 2
* ---
* 3
*)

[@@@ocamlformat "wrap-comments=false"]

type t =
Expand Down Expand Up @@ -213,3 +219,9 @@ let _ =

let _ = f (* foo
*) a

(* 1
* + 2
* ---
* 3
*)

0 comments on commit a0c9aae

Please sign in to comment.