@@ -93,36 +93,6 @@ type decoded_kind =
9393
9494type decoded = {prefix : string ; suffix : string ; kind : decoded_kind }
9595
96- (* * [~content_offset] indicates at which column the body of the comment
97- starts (1-indexed). [~max_idnent] indicates the maximum amount of
98- indentation to trim. *)
99- let unindent_lines ?(max_indent = Stdlib. max_int) ~content_offset first_line
100- tl_lines =
101- let tl_indent =
102- List. fold_left ~init: max_indent
103- ~f: (fun acc s ->
104- Option. value_map ~default: acc ~f: (min acc) (String. indent_of_line s) )
105- tl_lines
106- in
107- (* The indentation of the first line must account for the location of the
108- comment opening. Don't account for the first line if it's empty.
109- [fl_trim] is the number of characters to remove from the first line. *)
110- let fl_trim, fl_indent =
111- match String. indent_of_line first_line with
112- | Some i ->
113- (max 0 (min i (tl_indent - content_offset)), i + content_offset - 1 )
114- | None -> (String. length first_line, max_indent)
115- in
116- let min_indent = min tl_indent fl_indent in
117- let first_line = String. drop_prefix first_line fl_trim in
118- first_line
119- :: List. map ~f: (fun s -> String. drop_prefix s min_indent) tl_lines
120-
121- let unindent_lines ?max_indent ~content_offset txt =
122- match String. split ~on: '\n' txt with
123- | [] -> []
124- | hd :: tl -> unindent_lines ?max_indent ~content_offset hd tl
125-
12696let is_all_whitespace s = String. for_all s ~f: Char. is_whitespace
12797
12898let split_asterisk_prefixed =
@@ -147,18 +117,19 @@ let split_asterisk_prefixed =
147117 Some (fst_line :: List. map tl ~f: drop_prefix)
148118 | _ -> None
149119
120+ let ambiguous_line line =
121+ String. contains line '"' || String. contains line '{'
122+ || String. contains line '}'
123+
150124let mk ?(prefix = " " ) ?(suffix = " " ) kind = {prefix; suffix; kind}
151125
152- let decode_comment ~parse_comments_as_doc txt loc =
126+ let decode_comment ~parse_comments_as_doc ~preserve_ambiguous_line_comments
127+ txt loc =
153128 let txt =
154129 (* Windows compatibility *)
155130 let f = function '\r' -> false | _ -> true in
156131 String. filter txt ~f
157132 in
158- let opn_offset =
159- let {Lexing. pos_cnum; pos_bol; _} = loc.Location. loc_start in
160- pos_cnum - pos_bol + 1
161- in
162133 if String. length txt > = 2 then
163134 match txt.[0 ] with
164135 | '$' when not (Char. is_whitespace txt.[1 ]) -> mk (Verbatim txt)
@@ -173,15 +144,25 @@ let decode_comment ~parse_comments_as_doc txt loc =
173144 | '=' -> mk (Verbatim txt)
174145 | _ when is_all_whitespace txt ->
175146 mk (Verbatim " " ) (* Make sure not to format to [(* *) ]. *)
176- | _ when parse_comments_as_doc -> mk (Doc txt)
177- | _ -> (
147+ | c -> (
178148 let lines =
179- let content_offset = opn_offset + 2 in
180- unindent_lines ~content_offset txt
149+ txt |> String. split ~on: '\n'
150+ |> function
151+ | [] -> [] | hd :: tl -> hd :: List. map ~f: String. lstrip tl
181152 in
182- match split_asterisk_prefixed lines with
183- | Some deprefixed_lines -> mk (Asterisk_prefixed deprefixed_lines)
184- | None -> mk (Normal txt) )
153+ match lines with
154+ | [line] when preserve_ambiguous_line_comments && ambiguous_line line
155+ ->
156+ mk (Verbatim txt)
157+ | _ -> (
158+ match split_asterisk_prefixed lines with
159+ | Some deprefixed_lines -> mk (Asterisk_prefixed deprefixed_lines)
160+ | None ->
161+ if parse_comments_as_doc then
162+ match c with
163+ | '_' -> mk ~prefix: " _" (Doc (String. subo ~pos: 1 txt))
164+ | _ -> mk (Doc txt)
165+ else mk (Normal txt) ) )
185166 else
186167 match txt with
187168 (* "(* *) " is not parsed as a docstring but as a regular comment
@@ -197,6 +178,9 @@ let decode_docstring _loc = function
197178 | txt when is_all_whitespace txt -> mk (Verbatim " " )
198179 | txt -> mk ~prefix: " *" (Doc txt)
199180
200- let decode ~parse_comments_as_doc = function
201- | Comment {txt; loc} -> decode_comment ~parse_comments_as_doc txt loc
181+ let decode ~parse_comments_as_doc ~preserve_ambiguous_line_comments =
182+ function
183+ | Comment {txt; loc} ->
184+ decode_comment ~parse_comments_as_doc ~preserve_ambiguous_line_comments
185+ txt loc
202186 | Docstring {txt; loc} -> decode_docstring loc txt
0 commit comments