Skip to content

Commit a992813

Browse files
WIP rewrite of fmt_function from scratch.
1 parent bbac04d commit a992813

File tree

189 files changed

+3026
-2748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+3026
-2748
lines changed

bench/bench.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ let tests =
3636
(fun {name; input_name; kind; source; conf; action} ->
3737
Test.make
3838
~name:(Format.sprintf "%s (%s)" name input_name)
39-
( Staged.stage
40-
@@ fun () ->
41-
match action with
42-
| `Format ->
43-
ignore
44-
(Translation_unit.parse_and_format kind ~input_name ~source
45-
conf ) ) )
39+
( Staged.stage @@ fun () ->
40+
match action with
41+
| `Format ->
42+
ignore
43+
(Translation_unit.parse_and_format kind ~input_name ~source
44+
conf ) ) )
4645
inputs
4746

4847
let benchmark () =
@@ -102,11 +101,13 @@ let json_of_ols_results ?name (results : Bechamel.Analyze.OLS.t results) :
102101
|> Seq.map (fun (metric_name, ols) ->
103102
(metric_name, json_of_ols ols) )
104103
|> List.of_seq
105-
|> fun bindings -> `Assoc bindings
104+
|> fun bindings
105+
-> `Assoc bindings
106106
in
107107
`Assoc [("name", `String test_name); ("metrics", metrics)] )
108108
|> List.of_seq
109-
|> fun items -> `List items
109+
|> fun items
110+
-> `List items
110111
in
111112
let bindings = [("results", results)] in
112113
let bindings =

lib-rpc-server/ocamlformat_rpc.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ let run_format conf x =
9595

9696
let run_format_with_args {Rpc.path; config} conf x =
9797
let open Result in
98-
Option.value_map path ~default:(Ok conf) ~f:run_path
99-
>>= fun conf ->
98+
Option.value_map path ~default:(Ok conf) ~f:run_path >>= fun conf ->
10099
Option.value_map config ~default:(Ok conf) ~f:(fun c -> run_config conf c)
101-
>>= fun conf -> run_format conf x
100+
>>= fun conf
101+
-> run_format conf x
102102

103103
let handle_format_error e output = output stdout (`Error e)
104104

lib-rpc/ocamlformat_rpc_lib.ml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ module Make (IO : IO) = struct
3333

3434
let query command t =
3535
let open IO in
36-
Protocol.V1.output t.output command
37-
>>= fun () -> Protocol.V1.read_input t.input
36+
Protocol.V1.output t.output command >>= fun () ->
37+
Protocol.V1.read_input t.input
3838

3939
let halt t =
4040
let open IO in
@@ -45,8 +45,7 @@ module Make (IO : IO) = struct
4545

4646
let config c t =
4747
let open IO in
48-
query (`Config c) t
49-
>>= function
48+
query (`Config c) t >>= function
5049
| `Config _ -> return (Ok ())
5150
| `Error msg -> return (Error (`Msg msg))
5251
| _ ->
@@ -55,8 +54,7 @@ module Make (IO : IO) = struct
5554

5655
let format x t =
5756
let open IO in
58-
query (`Format x) t
59-
>>= function
57+
query (`Format x) t >>= function
6058
| `Format x -> return (Ok x)
6159
| `Error msg -> return (Error (`Msg msg))
6260
| _ -> return (Error (`Msg "failing to format input: unknown error"))
@@ -73,8 +71,8 @@ module Make (IO : IO) = struct
7371

7472
let query command t =
7573
let open IO in
76-
Protocol.V2.output t.output command
77-
>>= fun () -> Protocol.V2.read_input t.input
74+
Protocol.V2.output t.output command >>= fun () ->
75+
Protocol.V2.read_input t.input
7876

7977
let halt t =
8078
let open IO in
@@ -85,8 +83,7 @@ module Make (IO : IO) = struct
8583

8684
let format ~format_args x t =
8785
let open IO in
88-
query (`Format (x, format_args)) t
89-
>>= function
86+
query (`Format (x, format_args)) t >>= function
9087
| `Format (x, _args) -> return (Ok x)
9188
| `Error msg -> return (Error (`Msg msg))
9289
| _ -> return (Error (`Msg "failing to format input: unknown error"))
@@ -106,10 +103,8 @@ module Make (IO : IO) = struct
106103
let rec aux = function
107104
| [] -> return (Error (`Msg "Version negociation failed"))
108105
| latest :: others -> (
109-
Protocol.Init.output oc (`Version latest)
110-
>>= fun () ->
111-
Protocol.Init.read_input ic
112-
>>= function
106+
Protocol.Init.output oc (`Version latest) >>= fun () ->
107+
Protocol.Init.read_input ic >>= function
113108
| `Version v when v = latest -> return (get_client ~pid ic oc v)
114109
| `Version v -> (
115110
match others with

lib-rpc/protocol.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ module Make (IO : IO.S) = struct
3939

4040
let read_input ic =
4141
let open IO in
42-
read ic
43-
>>= function
42+
read ic >>= function
4443
| None -> return `Halt
4544
| Some (Atom "Halt") -> return `Halt
4645
| Some (List [Atom "Version"; Atom v]) -> return (`Version v)
@@ -66,8 +65,7 @@ module Make (IO : IO.S) = struct
6665
let read_input ic =
6766
let open Csexp in
6867
let open IO in
69-
read ic
70-
>>= function
68+
read ic >>= function
7169
| None -> return `Halt
7270
| Some (List [Atom "Format"; Atom x]) -> return (`Format x)
7371
| Some (List [Atom "Config"; List l]) ->
@@ -113,8 +111,7 @@ module Make (IO : IO.S) = struct
113111
| List [Atom name; Atom value] -> Some (name, value) | _ -> None )
114112
csexpl
115113
in
116-
read ic
117-
>>= function
114+
read ic >>= function
118115
| None -> return `Halt
119116
| Some (List (Atom "Format" :: Atom x :: l)) ->
120117
let extract args csexp =

lib/Chunk.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ let is_attr (type a) (fg : a list item) (x : a) =
3939

4040
let is_state_attr fg ~state x =
4141
let open Option.Monad_infix in
42-
is_attr fg x
43-
>>= fun (attr, loc) ->
44-
Conf.parse_state_attr attr
45-
>>= fun new_state ->
42+
is_attr fg x >>= fun (attr, loc) ->
43+
Conf.parse_state_attr attr >>= fun new_state ->
4644
match (state, new_state) with
4745
| `Enable, `Disable -> Some (`Disable, loc)
4846
| `Disable, `Enable -> Some (`Enable, loc)
@@ -54,8 +52,7 @@ let last_loc (type a) (fg : a list item) (l : a list) =
5452
| Structure -> List.last l >>| fun x -> x.pstr_loc
5553
| Signature -> List.last l >>| fun x -> x.psig_loc
5654
| Use_file -> (
57-
List.last l
58-
>>= function
55+
List.last l >>= function
5956
| Ptop_def x -> List.last x >>| fun x -> x.pstr_loc
6057
| Ptop_dir x -> Some x.pdir_loc )
6158

lib/Conf.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ let conventional_profile from =
8181
; indicate_nested_or_patterns= elt `Unsafe_no
8282
; infix_precedence= elt `Indent
8383
; leading_nested_match_parens= elt false
84+
; legacy_function_decl= elt true
8485
; let_and= elt `Compact
8586
; let_binding_indent= elt 2
8687
; let_binding_deindent_fun= elt true
@@ -150,6 +151,7 @@ let ocamlformat_profile from =
150151
; indicate_nested_or_patterns= elt `Space
151152
; infix_precedence= elt `Indent
152153
; leading_nested_match_parens= elt false
154+
; legacy_function_decl= elt true
153155
; let_and= elt `Compact
154156
; let_binding_indent= elt 2
155157
; let_binding_deindent_fun= elt true
@@ -218,6 +220,7 @@ let janestreet_profile from =
218220
; indicate_nested_or_patterns= elt `Unsafe_no
219221
; infix_precedence= elt `Parens
220222
; leading_nested_match_parens= elt true
223+
; legacy_function_decl= elt true
221224
; let_and= elt `Sparse
222225
; let_binding_indent= elt 2
223226
; let_binding_deindent_fun= elt false
@@ -922,6 +925,14 @@ module Formatting = struct
922925
update conf ~f:(fun f -> {f with leading_nested_match_parens= elt}) )
923926
(fun conf -> conf.fmt_opts.leading_nested_match_parens)
924927

928+
let legacy_function_decl =
929+
let doc = "Use legacy formatting for function declarations." in
930+
let names = ["legacy-function-decl"] in
931+
Decl.flag ~names ~default ~doc ~kind ~allow_inline:true
932+
(fun conf elt ->
933+
update conf ~f:(fun f -> {f with legacy_function_decl= elt}) )
934+
(fun conf -> conf.fmt_opts.legacy_function_decl)
935+
925936
let let_and =
926937
let doc = "Style of let_and." in
927938
let names = ["let-and"] in
@@ -1334,6 +1345,7 @@ module Formatting = struct
13341345
; elt indicate_nested_or_patterns
13351346
; elt infix_precedence
13361347
; elt leading_nested_match_parens
1348+
; elt legacy_function_decl
13371349
; elt let_and
13381350
; elt let_binding_indent
13391351
; elt let_binding_deindent_fun

lib/Conf_t.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type fmt_opts =
8989
; indicate_nested_or_patterns: [`Space | `Unsafe_no] elt
9090
; infix_precedence: [`Indent | `Parens] elt
9191
; leading_nested_match_parens: bool elt
92+
; legacy_function_decl: bool elt
9293
; let_and: [`Compact | `Sparse] elt
9394
; let_binding_indent: int elt
9495
; let_binding_deindent_fun: bool elt

lib/Conf_t.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type fmt_opts =
8686
; indicate_nested_or_patterns: [`Space | `Unsafe_no] elt
8787
; infix_precedence: [`Indent | `Parens] elt
8888
; leading_nested_match_parens: bool elt
89+
; legacy_function_decl: bool elt
8990
; let_and: [`Compact | `Sparse] elt
9091
; let_binding_indent: int elt
9192
; let_binding_deindent_fun: bool elt

0 commit comments

Comments
 (0)