Skip to content

Commit

Permalink
Abstract whether to set margin.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelvecchio-jsc committed Oct 6, 2023
1 parent 89877fb commit c96715a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ let fmt_cmt (conf : Conf.t) cmt ~fmt_code pos =
let len = String.length str - if dollar_suf then 2 else 1 in
let offset = offset + 1 in
let source = String.sub ~pos:1 ~len str in
match fmt_code conf ~offset source with
match fmt_code conf ~offset source ~set_margin:false with
| Ok formatted -> `Code (formatted, cls)
| Error (`Msg _) -> `Unwrapped (str, None) )
| txt when Char.equal txt.[0] '=' -> `Verbatim txt
Expand Down
16 changes: 11 additions & 5 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4593,14 +4593,18 @@ let fmt_file (type a) ~ctx ~fmt_code ~debug (fragment : a Extended_ast.t)
formatting doc. *)
Fmt_odoc.fmt_ast c.conf ~fmt_code:c.fmt_code d

let fmt_parse_result conf ~debug ast_kind ast source comments ~fmt_code =
let fmt_parse_result conf ~debug ast_kind ast source comments
~set_margin:set_margin_p ~fmt_code =
let cmts = Cmts.init ast_kind ~debug source ast comments in
let ctx = Top in
let code = fmt_file ~ctx ~debug ast_kind source cmts conf ast ~fmt_code in
let code =
(if set_margin_p then set_margin conf.Conf.fmt_opts.margin.v else noop)
$ fmt_file ~ctx ~debug ast_kind source cmts conf ast ~fmt_code
in
Ok code

let fmt_code ~debug =
let rec fmt_code (conf : Conf.t) ~offset s =
let rec fmt_code (conf : Conf.t) ~offset s ~set_margin =
let {Conf.fmt_opts; _} = conf in
let conf =
(* Adjust margin according to [offset]. *)
Expand All @@ -4614,9 +4618,11 @@ let fmt_code ~debug =
~input_name ~source:s
with
| Either.First {ast; comments; source; prefix= _} ->
fmt_parse_result conf ~debug Use_file ast source comments ~fmt_code
fmt_parse_result conf ~debug Use_file ast source comments ~set_margin
~fmt_code
| Second {ast; comments; source; prefix= _} ->
fmt_parse_result conf ~debug Repl_file ast source comments ~fmt_code
fmt_parse_result conf ~debug Repl_file ast source comments
~set_margin ~fmt_code
| exception Syntaxerr.Error (Expecting (_, x)) when warn ->
Error (`Msg (Format.asprintf "expecting: %s" x))
| exception Syntaxerr.Error (Not_expecting (_, x)) when warn ->
Expand Down
14 changes: 7 additions & 7 deletions lib/Fmt_odoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ open Odoc_parser.Ast
module Loc = Odoc_parser.Loc

type fmt_code =
Conf.t -> offset:int -> string -> (Fmt.t, [`Msg of string]) Result.t
Conf.t
-> offset:int
-> string
-> set_margin:bool
-> (Fmt.t, [`Msg of string]) Result.t

type c = {fmt_code: fmt_code; conf: Conf.t}

Expand Down Expand Up @@ -119,12 +123,8 @@ let fmt_code_block c s1 s2 =
match s1 with
| Some ({value= "ocaml"; _}, _) | None -> (
(* [offset] doesn't take into account code blocks nested into lists. *)
match c.fmt_code c.conf ~offset:2 original with
| Ok formatted ->
set_margin c.conf.fmt_opts.margin.v
$ formatted
|> Format_.asprintf "%a" Fmt.eval
|> fmt_code
match c.fmt_code c.conf ~offset:2 original ~set_margin:true with
| Ok formatted -> formatted |> Format_.asprintf "%a" Fmt.eval |> fmt_code
| Error (`Msg message) ->
( match message with
| "" -> ()
Expand Down
6 changes: 5 additions & 1 deletion lib/Fmt_odoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
(** [offset] is the column at which the content of the comment begins. It is
used to adjust the margin. *)
type fmt_code =
Conf.t -> offset:int -> string -> (Fmt.t, [`Msg of string]) Result.t
Conf.t
-> offset:int
-> string
-> set_margin:bool
-> (Fmt.t, [`Msg of string]) Result.t

val fmt_ast : Conf.t -> fmt_code:fmt_code -> Odoc_parser.Ast.t -> Fmt.t

Expand Down
16 changes: 7 additions & 9 deletions test/passing/tests/doc_comments-no-wrap.mli.err
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ Warning: tests/doc_comments.mli:124 exceeds the margin
Warning: tests/doc_comments.mli:328 exceeds the margin
Warning: tests/doc_comments.mli:377 exceeds the margin
Warning: tests/doc_comments.mli:384 exceeds the margin
Warning: tests/doc_comments.mli:403 exceeds the margin
Warning: tests/doc_comments.mli:408 exceeds the margin
Warning: tests/doc_comments.mli:449 exceeds the margin
Warning: tests/doc_comments.mli:463 exceeds the margin
Warning: tests/doc_comments.mli:520 exceeds the margin
Warning: tests/doc_comments.mli:550 exceeds the margin
Warning: tests/doc_comments.mli:620 exceeds the margin
Warning: tests/doc_comments.mli:451 exceeds the margin
Warning: tests/doc_comments.mli:465 exceeds the margin
Warning: tests/doc_comments.mli:522 exceeds the margin
Warning: tests/doc_comments.mli:552 exceeds the margin
Warning: tests/doc_comments.mli:622 exceeds the margin
Warning: tests/doc_comments.mli:643 exceeds the margin
Warning: tests/doc_comments.mli:656 exceeds the margin
Warning: tests/doc_comments.mli:624 exceeds the margin
Warning: tests/doc_comments.mli:645 exceeds the margin
Warning: tests/doc_comments.mli:658 exceeds the margin
6 changes: 4 additions & 2 deletions test/passing/tests/doc_comments-no-wrap.mli.ref
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,14 @@ end
#use "import.cinaps" ;;

List.iter all_fields ~f:(fun (name, type_) ->
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_ name )
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_
name )
]} *)

(** {[
List.iter all_fields ~f:(fun (name, type_) ->
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_ name )
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_
name )
]} *)

(** {[
Expand Down
16 changes: 7 additions & 9 deletions test/passing/tests/doc_comments.mli.err
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ Warning: tests/doc_comments.mli:124 exceeds the margin
Warning: tests/doc_comments.mli:328 exceeds the margin
Warning: tests/doc_comments.mli:377 exceeds the margin
Warning: tests/doc_comments.mli:384 exceeds the margin
Warning: tests/doc_comments.mli:403 exceeds the margin
Warning: tests/doc_comments.mli:408 exceeds the margin
Warning: tests/doc_comments.mli:449 exceeds the margin
Warning: tests/doc_comments.mli:463 exceeds the margin
Warning: tests/doc_comments.mli:520 exceeds the margin
Warning: tests/doc_comments.mli:550 exceeds the margin
Warning: tests/doc_comments.mli:614 exceeds the margin
Warning: tests/doc_comments.mli:451 exceeds the margin
Warning: tests/doc_comments.mli:465 exceeds the margin
Warning: tests/doc_comments.mli:522 exceeds the margin
Warning: tests/doc_comments.mli:552 exceeds the margin
Warning: tests/doc_comments.mli:616 exceeds the margin
Warning: tests/doc_comments.mli:637 exceeds the margin
Warning: tests/doc_comments.mli:650 exceeds the margin
Warning: tests/doc_comments.mli:618 exceeds the margin
Warning: tests/doc_comments.mli:639 exceeds the margin
Warning: tests/doc_comments.mli:652 exceeds the margin
6 changes: 4 additions & 2 deletions test/passing/tests/doc_comments.mli.ref
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,14 @@ end
#use "import.cinaps" ;;

List.iter all_fields ~f:(fun (name, type_) ->
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_ name )
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_
name )
]} *)

(** {[
List.iter all_fields ~f:(fun (name, type_) ->
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_ name )
printf "\nexternal get_%s\n: unit -> %s = \"get_%s\"" name type_
name )
]} *)

(** {[
Expand Down

0 comments on commit c96715a

Please sign in to comment.