From 9d348689874890487734bb20b02d1daeb282d7c2 Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Fri, 3 May 2024 10:53:12 +0000 Subject: [PATCH] remove duneContexts and call describe contexts from the vscode extension --- src/custom_requests.ml | 6 ---- src/custom_requests.mli | 2 -- src/extension_commands.ml | 60 ++++++++++++++++++++++++--------------- src/extension_instance.ml | 4 --- src/ocaml_lsp.ml | 6 ---- src/ocaml_lsp.mli | 2 -- 6 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/custom_requests.ml b/src/custom_requests.ml index 3baa9aa5f..90d551077 100644 --- a/src/custom_requests.ml +++ b/src/custom_requests.ml @@ -33,9 +33,3 @@ let typedHoles = Jsonoo.Encode.(object_ [ ("uri", string @@ Uri.toString uri ()) ])) ; decode_response = Jsonoo.Decode.list Range.t_of_json } - -let getDuneContexts = - { meth = ocamllsp_prefixed "duneContexts" - ; encode_params = (fun () -> Jsonoo.Encode.null) - ; decode_response = Jsonoo.Decode.(list string) - } diff --git a/src/custom_requests.mli b/src/custom_requests.mli index 018f04dbd..8823263eb 100644 --- a/src/custom_requests.mli +++ b/src/custom_requests.mli @@ -20,5 +20,3 @@ val switchImplIntf : (string, string array) custom_request val inferIntf : (string, string) custom_request val typedHoles : (Uri.t, Range.t list) custom_request - -val getDuneContexts : (unit, string list) custom_request diff --git a/src/extension_commands.ml b/src/extension_commands.ml index efdb78acd..0239a0532 100644 --- a/src/extension_commands.ml +++ b/src/extension_commands.ml @@ -191,30 +191,44 @@ let _set_dune_context = in Window.showQuickPickItems ~choices ~options () in - let select_dune_context client = - let* candidates = - Custom_requests.send_request client Custom_requests.getDuneContexts () - in - let* context = select_context candidates in - match context with - | None (* context selection cancelled *) -> Promise.return () - | Some new_context -> - let* () = Settings.set Settings.dune_context_setting new_context in - Extension_instance.start_language_server instance + let select_dune_context () = + match Workspace.rootPath () with + | None -> + (* Assumes that Dune root matches the workspace root *) + Promise.return + (show_message + `Warn + "Project root wasn't found. Can't select Dune context without \ + project root.") + | Some root -> ( + let* result = + let sandbox = Extension_instance.sandbox instance in + let cmd = + Sandbox.get_command sandbox "dune" [ "describe"; "contexts" ] + in + let env = + Interop.Dict.of_alist [ ("DUNE_CONFIG__GLOBAL_LOCK", "disabled") ] + in + Cmd.output ~env ~cwd:(Path.of_string root) cmd + in + match result with + | Error msg -> + Promise.return + (show_message + `Warn + "Error when calling `dune describe contexts': %s" + msg) + | Ok output -> ( + let candidates = String.split output ~on:'\n' in + let* context = select_context candidates in + match context with + | None (* context selection cancelled *) -> Promise.return () + | Some new_context -> + let* () = Settings.set Settings.dune_context_setting new_context in + Extension_instance.start_language_server instance)) in - match Extension_instance.lsp_client instance with - | None -> show_message `Warn "ocamllsp is not running." - | Some (client, ocaml_lsp) -> - if Ocaml_lsp.can_handle_dune_contexts ocaml_lsp then - let (_ : unit Promise.t) = select_dune_context client in - () - else - (* if ocamllsp doesn't have the capability, recommend updating - ocamllsp*) - show_message - `Warn - "The installed version of ocamllsp does not support setting the Dune \ - context. Consider updating ocamllsp." + let (_ : unit Promise.t) = select_dune_context () in + () in command Extension_consts.Commands.select_dune_context handler diff --git a/src/extension_instance.ml b/src/extension_instance.ml index 7c8b84df9..eedd6f18c 100644 --- a/src/extension_instance.ml +++ b/src/extension_instance.ml @@ -113,10 +113,6 @@ end = struct let server_options sandbox = let args = Settings.(get server_args_setting) |> Option.value ~default:[] in let args = - (* `handleDuneContexts` capability is already checked when getting the - contexts for the `ocaml.select-dune-context` command, so the only way - to get here with a version of ocamllsp that doesn't support contexts is - if the user adds the `dune.context` setting manually *) match Settings.get Settings.dune_context_setting with | None -> args | Some context -> "--context" :: context :: args diff --git a/src/ocaml_lsp.ml b/src/ocaml_lsp.ml index f2049c3e8..2953757b7 100644 --- a/src/ocaml_lsp.ml +++ b/src/ocaml_lsp.ml @@ -44,7 +44,6 @@ module Experimental_capabilities = struct ; handleSwitchImplIntf : bool ; handleInferIntf : bool ; handleTypedHoles : bool - ; handleDuneContexts : bool } let default = @@ -52,7 +51,6 @@ module Experimental_capabilities = struct ; handleSwitchImplIntf = false ; handleInferIntf = false ; handleTypedHoles = false - ; handleDuneContexts = false } (** Creates [t] given a JSON of form [{ 'handleSwitchImplIntf' : true, .... }] *) @@ -67,12 +65,10 @@ module Experimental_capabilities = struct let handleSwitchImplIntf = has_capability "handleSwitchImplIntf" in let handleInferIntf = has_capability "handleInferIntf" in let handleTypedHoles = has_capability "handleTypedHoles" in - let handleDuneContexts = has_capability "handleDuneContexts" in { interfaceSpecificLangId ; handleSwitchImplIntf ; handleInferIntf ; handleTypedHoles - ; handleDuneContexts } with Jsonoo.Decode_error err -> show_message @@ -234,5 +230,3 @@ let can_handle_switch_impl_intf t = let can_handle_infer_intf t = t.experimental_capabilities.handleSwitchImplIntf let can_handle_typed_holes t = t.experimental_capabilities.handleTypedHoles - -let can_handle_dune_contexts t = t.experimental_capabilities.handleDuneContexts diff --git a/src/ocaml_lsp.mli b/src/ocaml_lsp.mli index 7f8263114..dffcadcc3 100644 --- a/src/ocaml_lsp.mli +++ b/src/ocaml_lsp.mli @@ -13,8 +13,6 @@ val can_handle_infer_intf : t -> bool val can_handle_typed_holes : t -> bool -val can_handle_dune_contexts : t -> bool - module OcamllspSettingEnable : sig include Ojs.T