···66 | Merlin of id * Merlin_protocol.action
77 | Eval of id * int * string
88 | Format of id * string
99+ | Format_config of string
910 | Setup
10111112type output =
+5
src/x_ocaml.ml
···27272828let () = Client.post worker Setup
29293030+let () =
3131+ match current_attribute "x-ocamlformat" with
3232+ | None -> ()
3333+ | Some conf -> Client.post worker (Format_config (Jstr.to_string conf))
3434+3035let elt_name =
3136 match current_attribute "elt-name" with
3237 | None -> Jstr.of_string "x-ocaml"
+31-7
worker/ocamlfmt.ml
···33module Format_ = Ocamlformat_format.Format_
44module Parser_extended = Ocamlformat_parser_extended
5566-let conf = Ocamlformat_lib.Conf.default
66+let default_conf = Ocamlformat_lib.Conf.default
7788-let conf =
88+let default_conf =
99 {
1010- conf with
1010+ default_conf with
1111 opr_opts =
1212 {
1313- conf.opr_opts with
1313+ default_conf.opr_opts with
1414 ocaml_version = Conf.Elt.make (Ocaml_version.v 5 3) `Default;
1515 };
1616 }
17171818-let ast source =
1818+let ghost_loc =
1919+ Ocamlformat_ocaml_common.Warnings.ghost_loc_in_file ".ocamlformat"
2020+2121+let parse_conf str =
2222+ List.fold_left
2323+ ~f:(fun conf line ->
2424+ match Conf.parse_line conf ~from:(`File ghost_loc) line with
2525+ | Ok conf -> conf
2626+ | Error err ->
2727+ Brr.Console.error
2828+ [ "OCamlformat config error:"; line; Conf.Error.to_string err ];
2929+ conf)
3030+ ~init:default_conf
3131+ (String.split_on_chars ~on:[ '\n' ] str)
3232+3333+let conf = ref (`Conf default_conf)
3434+3535+let configure = function
3636+ | "disable" -> conf := `Disable
3737+ | str -> conf := `Conf (parse_conf str)
3838+3939+let ast ~conf source =
1940 Ocamlformat_lib.Parse_with_comments.parse
2041 (Ocamlformat_lib.Parse_with_comments.parse_ast conf)
2142 Structure conf ~input_name:"source" ~source
22432323-let fmt source =
2424- let ast = ast source in
4444+let fmt ~conf source =
4545+ let ast = ast ~conf source in
2546 let with_buffer_formatter ~buffer_size k =
2647 let buffer = Buffer.create buffer_size in
2748 let fs = Format_.formatter_of_buffer buffer in
···4263 conf ast.ast)
4364 in
4465 String.strip (print ast)
6666+6767+let fmt source =
6868+ match !conf with `Disable -> source | `Conf conf -> fmt ~conf source
+3-2
worker/x_worker.ml
···55let reformat ~id code =
66 let code' =
77 try Ocamlfmt.fmt code
88- with _err ->
99- (* Brr.Console.log [ "ocamlformat error"; Printexc.to_string _err ]; *)
88+ with err ->
99+ Brr.Console.error [ "OCamlformat error:"; Printexc.to_string err ];
1010 code
1111 in
1212 if code <> code' then respond (Formatted_source (id, code'));
···1717 match X_protocol.req_of_bytes marshaled_message with
1818 | Merlin (id, action) ->
1919 respond (Merlin_response (id, Merlin_worker.on_message action))
2020+ | Format_config conf -> Ocamlfmt.configure conf
2021 | Format (id, code) -> ignore (reformat ~id code : string)
2122 | Eval (id, line_number, code) ->
2223 let code = reformat ~id code in