this repo has no description

ocamlformat: custom config attribute

ArthurW aec9bf9d 5ca449ec

+40 -9
+1
protocol/x_protocol.ml
··· 6 6 | Merlin of id * Merlin_protocol.action 7 7 | Eval of id * int * string 8 8 | Format of id * string 9 + | Format_config of string 9 10 | Setup 10 11 11 12 type output =
+5
src/x_ocaml.ml
··· 27 27 28 28 let () = Client.post worker Setup 29 29 30 + let () = 31 + match current_attribute "x-ocamlformat" with 32 + | None -> () 33 + | Some conf -> Client.post worker (Format_config (Jstr.to_string conf)) 34 + 30 35 let elt_name = 31 36 match current_attribute "elt-name" with 32 37 | None -> Jstr.of_string "x-ocaml"
+31 -7
worker/ocamlfmt.ml
··· 3 3 module Format_ = Ocamlformat_format.Format_ 4 4 module Parser_extended = Ocamlformat_parser_extended 5 5 6 - let conf = Ocamlformat_lib.Conf.default 6 + let default_conf = Ocamlformat_lib.Conf.default 7 7 8 - let conf = 8 + let default_conf = 9 9 { 10 - conf with 10 + default_conf with 11 11 opr_opts = 12 12 { 13 - conf.opr_opts with 13 + default_conf.opr_opts with 14 14 ocaml_version = Conf.Elt.make (Ocaml_version.v 5 3) `Default; 15 15 }; 16 16 } 17 17 18 - let ast source = 18 + let ghost_loc = 19 + Ocamlformat_ocaml_common.Warnings.ghost_loc_in_file ".ocamlformat" 20 + 21 + let parse_conf str = 22 + List.fold_left 23 + ~f:(fun conf line -> 24 + match Conf.parse_line conf ~from:(`File ghost_loc) line with 25 + | Ok conf -> conf 26 + | Error err -> 27 + Brr.Console.error 28 + [ "OCamlformat config error:"; line; Conf.Error.to_string err ]; 29 + conf) 30 + ~init:default_conf 31 + (String.split_on_chars ~on:[ '\n' ] str) 32 + 33 + let conf = ref (`Conf default_conf) 34 + 35 + let configure = function 36 + | "disable" -> conf := `Disable 37 + | str -> conf := `Conf (parse_conf str) 38 + 39 + let ast ~conf source = 19 40 Ocamlformat_lib.Parse_with_comments.parse 20 41 (Ocamlformat_lib.Parse_with_comments.parse_ast conf) 21 42 Structure conf ~input_name:"source" ~source 22 43 23 - let fmt source = 24 - let ast = ast source in 44 + let fmt ~conf source = 45 + let ast = ast ~conf source in 25 46 let with_buffer_formatter ~buffer_size k = 26 47 let buffer = Buffer.create buffer_size in 27 48 let fs = Format_.formatter_of_buffer buffer in ··· 42 63 conf ast.ast) 43 64 in 44 65 String.strip (print ast) 66 + 67 + let fmt source = 68 + match !conf with `Disable -> source | `Conf conf -> fmt ~conf source
+3 -2
worker/x_worker.ml
··· 5 5 let reformat ~id code = 6 6 let code' = 7 7 try Ocamlfmt.fmt code 8 - with _err -> 9 - (* Brr.Console.log [ "ocamlformat error"; Printexc.to_string _err ]; *) 8 + with err -> 9 + Brr.Console.error [ "OCamlformat error:"; Printexc.to_string err ]; 10 10 code 11 11 in 12 12 if code <> code' then respond (Formatted_source (id, code')); ··· 17 17 match X_protocol.req_of_bytes marshaled_message with 18 18 | Merlin (id, action) -> 19 19 respond (Merlin_response (id, Merlin_worker.on_message action)) 20 + | Format_config conf -> Ocamlfmt.configure conf 20 21 | Format (id, code) -> ignore (reformat ~id code : string) 21 22 | Eval (id, line_number, code) -> 22 23 let code = reformat ~id code in