diff --git a/ocaml-lsp-server/src/code_actions.ml b/ocaml-lsp-server/src/code_actions.ml index 300d04c50..4c7308e9e 100644 --- a/ocaml-lsp-server/src/code_actions.ml +++ b/ocaml-lsp-server/src/code_actions.ml @@ -41,7 +41,14 @@ let compute server (params : CodeActionParams.t) = match doc with | None -> Fiber.return (Reply.now (actions dune_actions), state) | Some doc -> ( - let open_related = Action_open_related.for_uri uri in + let open_related = + let capabilities = + let open Option.O in + let* window = (State.client_capabilities state).window in + window.showDocument + in + Action_open_related.for_uri capabilities uri + in match Document.syntax doc with | Ocamllex | Menhir | Cram | Dune -> let state : State.t = Server.state server in diff --git a/ocaml-lsp-server/src/code_actions/action_open_related.ml b/ocaml-lsp-server/src/code_actions/action_open_related.ml index 96ae7d759..501129f64 100644 --- a/ocaml-lsp-server/src/code_actions/action_open_related.ml +++ b/ocaml-lsp-server/src/code_actions/action_open_related.ml @@ -22,28 +22,31 @@ let command_run server (params : ExecuteCommandParams.t) = Format.eprintf "failed to open %s@." uri); `Null -let for_uri uri = - Document.get_impl_intf_counterparts uri - |> List.map ~f:(fun uri -> - let path = Uri.to_path uri in - let exists = Sys.file_exists path in - let title = - sprintf "%s %s" - (if exists then "Open" else "Create") - (Filename.basename path) - in - let command = - let arguments = [ DocumentUri.yojson_of_t uri ] in - Command.create ~title ~command:command_name ~arguments () - in - let edit = - match exists with - | true -> None - | false -> - let documentChanges = - [ `CreateFile (CreateFile.create ~uri ()) ] - in - Some (WorkspaceEdit.create ~documentChanges ()) - in - CodeAction.create ?edit ~title ~kind:(CodeActionKind.Other "switch") - ~command ()) +let for_uri (capabilities : ShowDocumentClientCapabilities.t option) uri = + match capabilities with + | None | Some { support = false } -> [] + | Some { support = true } -> + Document.get_impl_intf_counterparts uri + |> List.map ~f:(fun uri -> + let path = Uri.to_path uri in + let exists = Sys.file_exists path in + let title = + sprintf "%s %s" + (if exists then "Open" else "Create") + (Filename.basename path) + in + let command = + let arguments = [ DocumentUri.yojson_of_t uri ] in + Command.create ~title ~command:command_name ~arguments () + in + let edit = + match exists with + | true -> None + | false -> + let documentChanges = + [ `CreateFile (CreateFile.create ~uri ()) ] + in + Some (WorkspaceEdit.create ~documentChanges ()) + in + CodeAction.create ?edit ~title ~kind:(CodeActionKind.Other "switch") + ~command ()) diff --git a/ocaml-lsp-server/src/code_actions/action_open_related.mli b/ocaml-lsp-server/src/code_actions/action_open_related.mli index fb100ffd6..5d3461f10 100644 --- a/ocaml-lsp-server/src/code_actions/action_open_related.mli +++ b/ocaml-lsp-server/src/code_actions/action_open_related.mli @@ -4,4 +4,5 @@ val command_name : string val command_run : _ Server.t -> ExecuteCommandParams.t -> Json.t Fiber.t -val for_uri : DocumentUri.t -> CodeAction.t list +val for_uri : + ShowDocumentClientCapabilities.t option -> DocumentUri.t -> CodeAction.t list diff --git a/ocaml-lsp-server/src/import.ml b/ocaml-lsp-server/src/import.ml index d970fc746..d159c5795 100644 --- a/ocaml-lsp-server/src/import.ml +++ b/ocaml-lsp-server/src/import.ml @@ -226,6 +226,7 @@ include struct module ServerCapabilities = ServerCapabilities module Server_notification = Lsp.Server_notification module SetTraceParams = SetTraceParams + module ShowDocumentClientCapabilities = ShowDocumentClientCapabilities module ShowDocumentParams = ShowDocumentParams module ShowDocumentResult = ShowDocumentResult module ShowMessageParams = ShowMessageParams diff --git a/ocaml-lsp-server/test/e2e-new/code_actions.ml b/ocaml-lsp-server/test/e2e-new/code_actions.ml index 9810524d1..60f798f3c 100644 --- a/ocaml-lsp-server/test/e2e-new/code_actions.ml +++ b/ocaml-lsp-server/test/e2e-new/code_actions.ml @@ -73,16 +73,4 @@ let foo = 123 "isPreferred": false, "kind": "type-annotate", "title": "Type-annotate" - } - { - "command": { - "arguments": [ "file:///foo.mli" ], - "command": "ocamllsp/open-related-source", - "title": "Create foo.mli" - }, - "edit": { - "documentChanges": [ { "kind": "create", "uri": "file:///foo.mli" } ] - }, - "kind": "switch", - "title": "Create foo.mli" } |}]