Skip to content

Requests: Refactor implIntf and inferIntf parameters #1501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ocaml-lsp-server/docs/ocamllsp/inferIntf-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ property type: `boolean`
## Request

- method: `ocamllsp/inferIntf`
- params: `DocumentUri` (see [`DocumentUri`](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#uri) in LSP specification)
- params:

```json
{
"uri": DocumentUri,
}
```
(see [`DocumentUri`](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#uri) in LSP specification)

## Response

Expand Down
11 changes: 8 additions & 3 deletions ocaml-lsp-server/docs/ocamllsp/switchImplIntf-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ property type: `boolean`
## Request

- method: `ocamllsp/switchImplIntf`
- params: `DocumentUri` (see [`DocumentUri`](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#uri) in LSP specification)
- params:

```json
{
"uri": DocumentUri,
}
```
(see [`DocumentUri`](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#uri) in LSP specification)

## Response

- result: DocumentUri[] (non-empty)
- error: code and message set in case an exception happens during the `ocamllsp/switchImplIntf` request.


36 changes: 11 additions & 25 deletions ocaml-lsp-server/src/custom_requests/req_infer_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,18 @@ let meth = "ocamllsp/inferIntf"

let on_request ~(params : Jsonrpc.Structured.t option) (state : State.t) =
Fiber.of_thunk (fun () ->
match params with
| Some (`List [ json_uri ]) ->
let json_uri = DocumentUri.t_of_yojson json_uri in
(match Document_store.get_opt state.store json_uri with
| None ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidParams
~message:
"ocamllsp/inferIntf received a URI for an unloaded file. Load the file \
first."
())
| Some impl ->
let+ intf = Inference.infer_intf_for_impl impl in
Json.t_of_yojson (`String intf))
| Some json ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:"The input parameter for ocamllsp/inferIntf is invalid"
~data:(`Assoc [ "param", (json :> Json.t) ])
())
let uri = Request_uri_params.parse_exn params in
let doc = Document_store.get_opt state.store uri in
match doc with
| None ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:"ocamllsp/inferIntf must receive param: DocumentUri.t"
()))
~code:InvalidParams
~message:
"ocamllsp/inferIntf received a URI for an unloaded file. Load the file \
first."
())
| Some impl ->
let+ intf = Inference.infer_intf_for_impl impl in
Json.t_of_yojson (`String intf))
;;
43 changes: 14 additions & 29 deletions ocaml-lsp-server/src/custom_requests/req_switch_impl_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,18 @@ let switch merlin_doc (param : DocumentUri.t) : Json.t =
;;

let on_request ~(params : Jsonrpc.Structured.t option) (state : State.t) =
match params with
| Some (`List [ json_uri ]) ->
let uri = DocumentUri.t_of_yojson json_uri in
(match Document_store.get_opt state.store uri with
| Some doc ->
(match Document.kind doc with
| `Merlin merlin_doc -> switch (Some merlin_doc) uri
| `Other ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:
"Document with this URI is not supported by ocamllsp/switchImplIntf"
~data:(`Assoc [ "param", (json_uri :> Json.t) ])
()))
| None -> switch None uri)
| Some json ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:"The input parameter for ocamllsp/switchImplIntf is invalid"
~data:(`Assoc [ "param", (json :> Json.t) ])
())
| None ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:"ocamllsp/switchImplIntf must receive param: DocumentUri.t"
())
let uri = Request_uri_params.parse_exn params in
let doc = Document_store.get_opt state.store uri in
match doc with
| Some doc ->
(match Document.kind doc with
| `Merlin merlin_doc -> switch (Some merlin_doc) uri
| `Other ->
Jsonrpc.Response.Error.raise
(Jsonrpc.Response.Error.make
~code:InvalidRequest
~message:"Document with this URI is not supported by ocamllsp/switchImplIntf"
~data:(Uri.yojson_of_t uri)
()))
| None -> switch None uri
;;
42 changes: 1 addition & 41 deletions ocaml-lsp-server/src/custom_requests/req_typed_holes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,6 @@ open Fiber.O
let capability = "handleTypedHoles", `Bool true
let meth = "ocamllsp/typedHoles"

module Request_params = struct
type t = Uri.t

(* Request params must have the form as in the given string. *)
let expected_params = `Assoc [ "uri", `String "<DocumentUri>" ]
let create uri = uri

let t_of_structured_json params : t option =
match params with
| `Assoc [ ("uri", uri) ] ->
let uri = Uri.t_of_yojson uri in
Some uri
| _ -> None
;;

let parse_exn (params : Jsonrpc.Structured.t option) : t =
let raise_invalid_params ?data ~message () =
Jsonrpc.Response.Error.raise
@@ Jsonrpc.Response.Error.make
?data
~code:Jsonrpc.Response.Error.Code.InvalidParams
~message
()
in
match params with
| None -> raise_invalid_params ~message:"Expected params but received none" ()
| Some params ->
(match t_of_structured_json params with
| Some uri -> uri
| None ->
let error_json =
`Assoc
[ "params_expected", expected_params; "params_received", (params :> Json.t) ]
in
raise_invalid_params ~message:"Unxpected parameter format" ~data:error_json ())
;;

let yojson_of_t = Uri.yojson_of_t
end

type t = Range.t list

let yojson_of_t holes =
Expand All @@ -57,7 +17,7 @@ let t_of_yojson list =

let on_request ~(params : Jsonrpc.Structured.t option) (state : State.t) =
Fiber.of_thunk (fun () ->
let uri = Request_params.parse_exn params in
let uri = Request_uri_params.parse_exn params in
let store = state.store in
let doc = Document_store.get_opt store uri in
match doc with
Expand Down
7 changes: 0 additions & 7 deletions ocaml-lsp-server/src/custom_requests/req_typed_holes.mli
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
open Import

module Request_params : sig
type t

val create : Uri.t -> t
val yojson_of_t : t -> Json.t
end

type t

val t_of_yojson : Json.t -> t
Expand Down
38 changes: 38 additions & 0 deletions ocaml-lsp-server/src/custom_requests/request_uri_params.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
open Import

type t = Uri.t

(* Request params must have the form as in the given string. *)
let expected_params = `Assoc [ "uri", `String "<DocumentUri>" ]

let t_of_structured_json params : t option =
match params with
| `Assoc [ ("uri", uri) ] ->
let uri = Uri.t_of_yojson uri in
Some uri
| _ -> None
;;

let parse_exn (params : Jsonrpc.Structured.t option) : t =
let raise_invalid_params ?data ~message () =
Jsonrpc.Response.Error.raise
@@ Jsonrpc.Response.Error.make
?data
~code:Jsonrpc.Response.Error.Code.InvalidParams
~message
()
in
match params with
| None -> raise_invalid_params ~message:"Expected params but received none" ()
| Some params ->
(match t_of_structured_json params with
| Some uri -> uri
| None ->
let error_json =
`Assoc
[ "params_expected", expected_params; "params_received", (params :> Json.t) ]
in
raise_invalid_params ~message:"Unexpected parameter format" ~data:error_json ())
;;

let yojson_of_t = Uri.yojson_of_t
6 changes: 6 additions & 0 deletions ocaml-lsp-server/src/custom_requests/request_uri_params.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
open Import

type t = Uri.t

val yojson_of_t : t -> Json.t
val parse_exn : Jsonrpc.Structured.t option -> t