Skip to content
Draft
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: 2 additions & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor/**
opam-files/**
opam-files-mll/**
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(lang dune 2.8)
(lang dune 3.13)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
Expand All @@ -13,7 +13,7 @@

(name ocamlformat)

(using menhir 2.1)
(using menhir 3.0)

(cram enable)

Expand Down
62 changes: 33 additions & 29 deletions lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -454,36 +454,40 @@ let is_only_whitespaces s = String.for_all s ~f:Char.is_whitespace
module Wrapped = struct
let fmt ~pro ~epi text =
let open Fmt in
assert (not (String.is_empty text)) ;
let prefix = if String.starts_with_whitespace text then " " else ""
and suffix = if String.ends_with_whitespace text then " " else "" in
let fmt_line line =
let words =
List.filter ~f:(Fn.non String.is_empty)
(String.split_on_chars line
~on:['\t'; '\n'; '\011'; '\012'; '\r'; ' '] )
if String.is_empty text then pro $ epi
else
let prefix = if String.starts_with_whitespace text then " " else ""
and suffix = if String.ends_with_whitespace text then " " else "" in
let fmt_line line =
let words =
List.filter ~f:(Fn.non String.is_empty)
(String.split_on_chars line
~on:['\t'; '\n'; '\011'; '\012'; '\r'; ' '] )
in
list words space_break str
in
list words space_break str
in
let lines =
List.remove_consecutive_duplicates
~equal:(fun x y -> String.is_empty x && String.is_empty y)
(String.split (String.rstrip text) ~on:'\n')
in
let groups =
List.group lines ~break:(fun _ y -> is_only_whitespaces y)
in
pro $ str prefix
$ hovbox 0
(list_fl groups (fun ~first ~last:last_group group ->
let group = List.filter group ~f:(Fn.non is_only_whitespaces) in
fmt_if (not first) (str "\n" $ force_newline)
$ hovbox 0
(list_fl group (fun ~first ~last x ->
fmt_if (not first) space_break
$ fmt_line x
$ fmt_if (last_group && last) (str suffix $ epi) ) ) )
)
let lines =
List.remove_consecutive_duplicates
~equal:(fun x y -> String.is_empty x && String.is_empty y)
(String.split (String.rstrip text) ~on:'\n')
in
let groups =
List.group lines ~break:(fun _ y -> is_only_whitespaces y)
|> List.filter_map ~f:(fun group ->
match List.filter group ~f:(Fn.non is_only_whitespaces) with
| [] -> None
| group -> Some group )
in
pro $ str prefix
$ hovbox 0
(list_fl groups (fun ~first ~last:last_group group ->
fmt_if (not first) (str "\n" $ force_newline)
$ hovbox 0
(list_fl group (fun ~first ~last x ->
fmt_if (not first) space_break
$ fmt_line x
$ fmt_if (last_group && last) (str suffix $ epi) ) ) )
)
end

module Asterisk_prefixed = struct
Expand Down
20 changes: 20 additions & 0 deletions lib/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ let conventional_profile from =
; stritem_extension_indent= elt 0
; type_decl= elt `Compact
; type_decl_indent= elt 2
; reformat_mll= elt `Ocaml_block
; wrap_comments= elt false
; wrap_docstrings= elt true
; wrap_fun_args= elt true }
Expand Down Expand Up @@ -182,6 +183,7 @@ let ocamlformat_profile from =
; stritem_extension_indent= elt 0
; type_decl= elt `Compact
; type_decl_indent= elt 2
; reformat_mll= elt `Ocaml_block
; wrap_comments= elt false
; wrap_docstrings= elt true
; wrap_fun_args= elt true }
Expand Down Expand Up @@ -252,6 +254,7 @@ let janestreet_profile from =
; stritem_extension_indent= elt 2
; type_decl= elt `Sparse
; type_decl_indent= elt 2
; reformat_mll= elt `Ocaml_block
; wrap_comments= elt false
; wrap_docstrings= elt false
; wrap_fun_args= elt false }
Expand Down Expand Up @@ -1323,6 +1326,22 @@ module Formatting = struct
update conf ~f:(fun f -> {f with type_decl_indent= elt}) )
(fun conf -> conf.fmt_opts.type_decl_indent)

let reformat_mll =
let doc = "How to format .mll (ocamllex) files." in
let names = ["reformat-mll"] in
let all =
[ Decl.Value.make ~name:"ocaml-block" `Ocaml_block
"$(b,ocaml-block) formats only the embedded OCaml code blocks, \
preserving surrounding syntax and comments."
; Decl.Value.make ~name:"full" `Full
"$(b,full) reformats the entire .mll file structure."
; Decl.Value.make ~name:"no" `No
"$(b,no) disables formatting of .mll files." ]
in
Decl.choice ~names ~all ~default ~doc ~kind
(fun conf elt -> update conf ~f:(fun f -> {f with reformat_mll= elt}))
(fun conf -> conf.fmt_opts.reformat_mll)

let wrap_comments =
let doc =
"Comments are divided into paragraphs by open lines (two or more \
Expand Down Expand Up @@ -1395,6 +1414,7 @@ module Formatting = struct
; elt parens_tuple_patterns
; elt parse_docstrings
; elt parse_toplevel_phrases
; elt reformat_mll
; elt sequence_blank_line
; elt sequence_style
; elt single_case
Expand Down
1 change: 1 addition & 0 deletions lib/Conf_t.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type fmt_opts =
; stritem_extension_indent: int elt
; type_decl: [`Compact | `Sparse] elt
; type_decl_indent: int elt
; reformat_mll: [`No | `Ocaml_block | `Full] elt
; wrap_comments: bool elt
; wrap_docstrings: bool elt
; wrap_fun_args: bool elt }
Expand Down
2 changes: 2 additions & 0 deletions lib/Conf_t.mli
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ type fmt_opts =
; stritem_extension_indent: int elt
; type_decl: [`Compact | `Sparse] elt
; type_decl_indent: int elt
; reformat_mll: [`No | `Ocaml_block | `Full] elt
(** How to format .mll files. *)
; wrap_comments: bool elt (** Wrap comments at margin. *)
; wrap_docstrings: bool elt
; wrap_fun_args: bool elt }
Expand Down
Loading
Loading