From ac96149ba667ef71cd00091fad0c2c10cd36b5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Mon, 8 Apr 2024 12:14:54 +0000 Subject: [PATCH] merlin: replace get/set context with flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Javier Chávarri --- bin/ocaml/ocaml_merlin.ml | 70 ++++--------------- .../{get-set-contexts.t => alt-context.t} | 27 +++---- 2 files changed, 25 insertions(+), 72 deletions(-) rename test/blackbox-tests/test-cases/merlin/{get-set-contexts.t => alt-context.t} (57%) diff --git a/bin/ocaml/ocaml_merlin.ml b/bin/ocaml/ocaml_merlin.ml index a1cbc744b9b2..0d766fb7ccde 100644 --- a/bin/ocaml/ocaml_merlin.ml +++ b/bin/ocaml/ocaml_merlin.ml @@ -7,7 +7,7 @@ module Server : sig (** Once started the server will wait for commands on stdin, read the requested merlin dot file and return its content on stdout. The server will halt when receiving EOF of a bad csexp. *) - val start : unit -> unit Fiber.t + val start : ctx_name:Context_name.t -> unit -> unit Fiber.t end = struct open Fiber.O @@ -27,12 +27,6 @@ end = struct | File of string | Halt | Unknown of string - | GetContexts - | SetContext of string - - type selected_ctxt = - | Default - | Custom of Context_name.t let read_input in_channel = match Csexp.input_opt in_channel with @@ -42,8 +36,6 @@ end = struct (match sexp with | Atom "Halt" -> Halt | List [ Atom "File"; Atom path ] -> File path - | List [ Atom "GetContexts" ] -> GetContexts - | List [ Atom "SetContext"; Atom name ] -> SetContext name | sexp -> let msg = Printf.sprintf "Bad input: %s" (Sexp.to_string sexp) in Unknown msg) @@ -137,24 +129,16 @@ end = struct |> error ;; - let to_local ~selected_context file = + let to_local ~ctx_name file = match to_local file with | Error s -> Fiber.return (Error s) | Ok file -> let module Context_name = Dune_engine.Context_name in - (match selected_context with - | Commands.Custom ctxt -> - Fiber.return (Ok (Path.Build.append_local (Context_name.build_dir ctxt) file)) - | Default -> - let+ workspace = Memo.run (Workspace.workspace ()) in - (match workspace.merlin_context with - | None -> Error "no merlin context configured" - | Some context -> - Ok (Path.Build.append_local (Context_name.build_dir context) file))) + Fiber.return (Ok (Path.Build.append_local (Context_name.build_dir ctx_name) file)) ;; - let print_merlin_conf ~selected_context file = - to_local ~selected_context file + let print_merlin_conf ~ctx_name file = + to_local ~ctx_name file >>| (function | Error s -> Merlin_conf.make_error s | Ok file -> load_merlin_file file) @@ -162,14 +146,14 @@ end = struct ;; let dump s = - to_local ~selected_context:Default s + to_local ~ctx_name:Context_name.default s >>| function | Error mess -> Printf.eprintf "%s\n%!" mess | Ok path -> get_merlin_files_paths path |> List.iter ~f:Merlin.Processed.print_file ;; let dump_dot_merlin s = - to_local ~selected_context:Default s + to_local ~ctx_name:Context_name.default s >>| function | Error mess -> Printf.eprintf "%s\n%!" mess | Ok path -> @@ -177,44 +161,16 @@ end = struct Merlin.Processed.print_generic_dot_merlin files ;; - let start () = - let selected_context = ref Commands.Default in + let start ~ctx_name () = let rec main () = match Commands.read_input stdin with | Halt -> Fiber.return () | File path -> - let* () = print_merlin_conf ~selected_context:!selected_context path in + let* () = print_merlin_conf ~ctx_name path in main () | Unknown msg -> Merlin_conf.to_stdout (Merlin_conf.make_error msg); main () - | GetContexts -> - let open Fiber.O in - let* setup = Import.Main.setup () in - let* setup = Memo.run setup in - let ctxts = - List.map - ~f:(fun (name, _) -> Sexp.Atom (Context_name.to_string name)) - (Context_name.Map.to_list setup.scontexts) - in - Merlin_conf.to_stdout Sexp.(List ctxts); - main () - | SetContext name -> - let open Fiber.O in - let* setup = Import.Main.setup () in - let* setup = Memo.run setup in - let () = - let open Merlin_conf in - match Context_name.of_string_opt name with - | None -> to_stdout (make_error (Printf.sprintf "Invalid context name %S" name)) - | Some ctxt_name -> - (match Context_name.Map.mem setup.scontexts ctxt_name with - | false -> - to_stdout - (make_error (Printf.sprintf "Can't find context with name %S" name)) - | true -> selected_context := Custom ctxt_name) - in - main () in main () ;; @@ -262,7 +218,11 @@ let man = let start_session_info name = Cmd.info name ~doc ~man let start_session_term = - let+ builder = Common.Builder.term in + let+ builder = Common.Builder.term + and+ ctx_name = + Common.context_arg + ~doc:{|The Dune context in which the command will return information for.|} + in let common, config = let builder = let builder = Common.Builder.forbid_builds builder in @@ -270,7 +230,7 @@ let start_session_term = in Common.init builder in - Scheduler.go ~common ~config Server.start + Scheduler.go ~common ~config (Server.start ~ctx_name) ;; let command = Cmd.v (start_session_info "ocaml-merlin") start_session_term diff --git a/test/blackbox-tests/test-cases/merlin/get-set-contexts.t b/test/blackbox-tests/test-cases/merlin/alt-context.t similarity index 57% rename from test/blackbox-tests/test-cases/merlin/get-set-contexts.t rename to test/blackbox-tests/test-cases/merlin/alt-context.t index de97fb541b65..25df6385d36a 100644 --- a/test/blackbox-tests/test-cases/merlin/get-set-contexts.t +++ b/test/blackbox-tests/test-cases/merlin/alt-context.t @@ -1,4 +1,4 @@ -Showcase behavior of the GetContexts ocaml-merlin command +Showcase behavior when passing the `--context` flag to ocaml-merlin $ cat >dune-project < (lang dune 3.14) @@ -14,11 +14,6 @@ Showcase behavior of the GetContexts ocaml-merlin command > (name alt))) > EOF - $ printf "(11:GetContexts)" | dune ocaml-merlin - (3:alt7:default) - -Showcase behavior of the SetContext ocaml-merlin command - $ lib1=foo $ lib2=bar $ cat >dune <