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

ocp-indent-compat: Don't unindent unwrapped docstrings #2392

Merged
merged 4 commits into from
Jul 6, 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 @@ -34,7 +34,7 @@
- Restore short form formatting of record field aliases (#2282, #2388, @gpetiot, @Julow)
- Tweaks the JaneStreet profile to be more consistent with ocp-indent (#2214, #2281, #2284, #2289, #2299, #2302, #2309, #2310, #2311, #2313, #2316, #2362, #2363, @gpetiot, @Julow)
- Improve formatting of class signatures (#2301, #2387, @gpetiot, @Julow)
- JaneStreet profile: treat comments as doc-comments (#2261, #2344, #2354, #2365, @gpetiot, @Julow)
- JaneStreet profile: treat comments as doc-comments (#2261, #2344, #2354, #2365, #2392, @gpetiot, @Julow)
- Don't indent attributes after a let/val/external (#2317, @Julow)
- Adjust indentation of class-expr function body (#2328, @gpetiot)

Expand Down
10 changes: 4 additions & 6 deletions lib/Cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,16 @@ end
type pos = Before | Within | After

let unindent_lines ~offset first_line tl_lines =
let indent_of_line s =
(* index of first non-whitespace is indentation, None means white line *)
String.lfindi s ~f:(fun _ c -> not (Char.is_whitespace c))
in
(* The indentation of the first line must account for the location of the
comment opening *)
let fl_spaces = Option.value ~default:0 (indent_of_line first_line) in
let fl_spaces =
Option.value ~default:0 (String.indent_of_line first_line)
in
let fl_indent = fl_spaces + offset in
let min_indent =
List.fold_left ~init:fl_indent
~f:(fun acc s ->
Option.value_map ~default:acc ~f:(min acc) (indent_of_line s) )
Option.value_map ~default:acc ~f:(min acc) (String.indent_of_line s) )
tl_lines
in
(* Completely trim the first line *)
Expand Down
17 changes: 14 additions & 3 deletions lib/Fmt_odoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,27 @@ let fmt_ast conf ~fmt_code (docs : t) =
let c = {fmt_code; conf} in
vbox 0 (list_block_elem c docs (fmt_block_element c))

let beginning_offset (conf : Conf.t) input =
let whitespace_count =
match String.indent_of_line input with Some c -> c | None -> 1
in
if conf.fmt_opts.ocp_indent_compat.v && not conf.fmt_opts.wrap_docstrings.v
then
(* Preserve offset of the first line and indent the whole comment based
on that. *)
whitespace_count
else min whitespace_count 1

let fmt_parsed (conf : Conf.t) ~fmt_code ~input ~offset parsed =
let open Fmt in
let begin_space = String.starts_with_whitespace input in
let begin_offset = beginning_offset conf input in
(* The offset is used to adjust the margin when formatting code blocks. *)
let offset = offset + if begin_space then 1 else 0 in
let offset = offset + begin_offset in
let fmt_code conf ~offset:offset' input =
fmt_code conf ~offset:(offset + offset') input
in
let fmt_parsed parsed =
fmt_if begin_space " "
str (String.make begin_offset ' ')
$ fmt_ast conf ~fmt_code parsed
$ fmt_if
(String.length input > 1 && String.ends_with_whitespace input)
Expand Down
8 changes: 8 additions & 0 deletions test/passing/tests/js_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7956,3 +7956,11 @@ let _ =

(** xxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx
xxxx] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx] *)

(* Hand-aligned comment
.
. *)

(* First line is indented more
.
. *)
14 changes: 11 additions & 3 deletions test/passing/tests/js_source.ml.ocp
Original file line number Diff line number Diff line change
Expand Up @@ -2865,8 +2865,8 @@ let rec pre_subst f = function
;;

let comp_subst f g (x : 'a fin) = pre_subst f (g x)
(* val comp_subst :
('b fin -> 'c term) -> ('a fin -> 'b term) -> 'a fin -> 'c term *)
(* val comp_subst :
('b fin -> 'c term) -> ('a fin -> 'b term) -> 'a fin -> 'c term *)

(* 4 The Occur-Check, through thick and thin *)

Expand Down Expand Up @@ -2961,7 +2961,7 @@ let rec sub' : type m. m ealist -> m fin -> m term = function
;;

let subst' d = pre_subst (sub' d)
(* val subst' : 'a ealist -> 'a term -> 'a term *)
(* val subst' : 'a ealist -> 'a term -> 'a term *)

(* 6 First-Order Unification *)

Expand Down Expand Up @@ -10190,3 +10190,11 @@ let _ =

(** xxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx
xxxx] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx] *)

(* Hand-aligned comment
.
. *)

(* First line is indented more
.
. *)
16 changes: 12 additions & 4 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -2865,8 +2865,8 @@ let rec pre_subst f = function
;;

let comp_subst f g (x : 'a fin) = pre_subst f (g x)
(* val comp_subst :
('b fin -> 'c term) -> ('a fin -> 'b term) -> 'a fin -> 'c term *)
(* val comp_subst :
('b fin -> 'c term) -> ('a fin -> 'b term) -> 'a fin -> 'c term *)

(* 4 The Occur-Check, through thick and thin *)

Expand Down Expand Up @@ -2961,7 +2961,7 @@ let rec sub' : type m. m ealist -> m fin -> m term = function
;;

let subst' d = pre_subst (sub' d)
(* val subst' : 'a ealist -> 'a term -> 'a term *)
(* val subst' : 'a ealist -> 'a term -> 'a term *)

(* 6 First-Order Unification *)

Expand Down Expand Up @@ -10041,7 +10041,7 @@ let _ =
;;

(*
*)
*)

(** xxx *)
include S1
Expand Down Expand Up @@ -10190,3 +10190,11 @@ let _ =

(** xxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx
xxxx] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [xxxxxxx] *)

(* Hand-aligned comment
.
. *)

(* First line is indented more
.
. *)
10 changes: 10 additions & 0 deletions vendor/ocamlformat-stdlib/string_ext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ let starts_with_whitespace s = (not (is_empty s)) && Char.is_whitespace s.[0]

let ends_with_whitespace s =
(not (is_empty s)) && Char.is_whitespace s.[length s - 1]

let rec _indent_of_line s i end_ =
if Int.equal i end_ then None
else
match s.[i] with
| ' ' | '\t' -> _indent_of_line s (i + 1) end_
| '\n' | '\r' -> None
| _ -> Some i

let indent_of_line s = _indent_of_line s 0 (String.length s)
4 changes: 4 additions & 0 deletions vendor/ocamlformat-stdlib/string_ext.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ val starts_with_whitespace : string -> bool
val ends_with_whitespace : string -> bool
(** [ends_with_whitespace s] holds if [s] is non empty and ends with a
whitespace character. *)

val indent_of_line : string -> int option
(** [indent_of_line s] is the indentation at the beginning of [s].
Returns [None] if the first line of the string is only whitespaces or is empty. *)