-
Notifications
You must be signed in to change notification settings - Fork 151
Destruct custom request #1583
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
Merged
Merged
Destruct custom request #1583
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
308a358
Add test that illustrate `Incomplete Error Message`
xvw 444054e
Add `ocamllsp/destruct` custom request
xvw 7c54c29
Add test for `destruct` custom request
xvw 4f7e3c8
Add CHANGE entry
xvw eea610f
Add missing documentation
xvw 39c7e51
Update ocaml-lsp-server/docs/ocamllsp/destruct-spec.md
xvw b88a23e
Update ocaml-lsp-server/docs/ocamllsp/destruct-spec.md
xvw 096d82b
Update ocaml-lsp-server/src/custom_requests/req_destruct.ml
xvw 1f62a6e
Remove residual test
xvw 391d7a3
Fix Changelog
xvw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Construct Request | ||
|
|
||
| ## Description | ||
|
|
||
| Provides a query to performs `case analysis` on pattern matching | ||
| clauses. | ||
xvw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Client Capability | ||
|
|
||
| There is no client capability relative to this request. | ||
|
|
||
| ## Server capability | ||
|
|
||
| - property name: `handleDestruct` | ||
| - property type: `boolean` | ||
|
|
||
| ## Request | ||
|
|
||
| - method: `ocamllsp/destruct` | ||
| - params: | ||
|
|
||
| ```json | ||
| { | ||
| "uri": TextDocumentIdentifier, | ||
| "range": Range | ||
| } | ||
| ``` | ||
|
|
||
| ## Response | ||
|
|
||
| ```json | ||
| { | ||
| "range": Range, | ||
| "content": string | ||
| } | ||
| ``` | ||
|
|
||
| Contain the `range` to be substitute and the `content`. | ||
xvw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| open Import | ||
|
|
||
| let capability = "handleDestruct", `Bool true | ||
| let meth = "ocamllsp/destruct" | ||
|
|
||
| module Request_params = struct | ||
| type t = | ||
| { text_document : TextDocumentIdentifier.t | ||
| ; range : Range.t | ||
| } | ||
|
|
||
| let create ~text_document ~range () = { text_document; range } | ||
|
|
||
| let yojson_of_t { text_document; range } = | ||
| match TextDocumentIdentifier.yojson_of_t text_document with | ||
| | `Assoc assoc -> `Assoc (("range", Range.yojson_of_t range) :: assoc) | ||
| | _ -> (* unreachable *) assert false | ||
| ;; | ||
|
|
||
| let t_of_yojson json = | ||
| let open Yojson.Safe.Util in | ||
| let text_document = json |> TextDocumentIdentifier.t_of_yojson in | ||
| let range = json |> member "range" |> Range.t_of_yojson in | ||
| create ~text_document ~range () | ||
| ;; | ||
| end | ||
|
|
||
| type t = | ||
| { range : Range.t | ||
| ; content : string | ||
| } | ||
|
|
||
| let t_of_yojson json = | ||
| let open Yojson.Safe.Util in | ||
| let range = json |> member "range" |> Range.t_of_yojson | ||
| and content = json |> member "content" |> to_string in | ||
| { range; content } | ||
| ;; | ||
|
|
||
| let yojson_of_t { range; content } = | ||
| `Assoc [ "range", Range.yojson_of_t range; "content", `String content ] | ||
| ;; | ||
|
|
||
| let with_pipeline state uri f = | ||
| let doc = Document_store.get state.State.store uri in | ||
| match Document.kind doc with | ||
| | `Other -> Fiber.return `Null | ||
| | `Merlin merlin -> | ||
| (match Document.Merlin.kind merlin with | ||
| | Document.Kind.Intf -> | ||
| (* Destruct makes no sense if its called from an interface. *) | ||
xvw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Fiber.return `Null | ||
| | Document.Kind.Impl -> Document.Merlin.with_pipeline_exn merlin f) | ||
| ;; | ||
|
|
||
| let make_destruct_command start stop = Query_protocol.Case_analysis (start, stop) | ||
|
|
||
| let dispatch_destruct range pipeline = | ||
| let start = range.Range.start |> Position.logical | ||
| and stop = range.Range.end_ |> Position.logical in | ||
| let command = make_destruct_command start stop in | ||
| let loc, content = Query_commands.dispatch pipeline command in | ||
| yojson_of_t { content; range = Range.of_loc loc } | ||
| ;; | ||
|
|
||
| let on_request ~params state = | ||
| Fiber.of_thunk (fun () -> | ||
| let params = (Option.value ~default:(`Assoc []) params :> Json.t) in | ||
| let Request_params.{ text_document; range } = Request_params.t_of_yojson params in | ||
| let uri = text_document.uri in | ||
| with_pipeline state uri @@ dispatch_destruct range) | ||
| ;; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| open Import | ||
|
|
||
| module Request_params : sig | ||
| type t | ||
|
|
||
| val create : text_document:Import.TextDocumentIdentifier.t -> range:Range.t -> unit -> t | ||
| val yojson_of_t : t -> Json.t | ||
| end | ||
|
|
||
| type t | ||
|
|
||
| val t_of_yojson : Json.t -> t | ||
| val capability : string * Json.t | ||
| val meth : string | ||
| val on_request : params:Jsonrpc.Structured.t option -> State.t -> Json.t Fiber.t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| open Test.Import | ||
| module Req = Ocaml_lsp_server.Custom_request.Destruct | ||
|
|
||
| module Util = struct | ||
| let call_destruct client range = | ||
| let uri = DocumentUri.of_path "test.ml" in | ||
| let text_document = TextDocumentIdentifier.create ~uri in | ||
| let params = | ||
| Req.Request_params.create ~text_document ~range () | ||
| |> Req.Request_params.yojson_of_t | ||
| |> Jsonrpc.Structured.t_of_yojson | ||
| |> Option.some | ||
| in | ||
| let req = Lsp.Client_request.UnknownRequest { meth = Req.meth; params } in | ||
| Client.request client req | ||
| ;; | ||
|
|
||
| let test pos source = | ||
| let range = | ||
| match pos with | ||
| | `Pos start -> | ||
| Range.create ~start ~end_:Position.{ start with character = start.character + 1 } | ||
| | `Range range -> range | ||
| in | ||
| let request client = | ||
| let open Fiber.O in | ||
| let+ response = call_destruct client range in | ||
| Test.print_result response | ||
| in | ||
| Helpers.test source request | ||
| ;; | ||
| end | ||
|
|
||
| let%expect_test "Perform `destruct` as custom request - 1" = | ||
| let source = | ||
| {| | ||
| let _ = | ||
| match (None : unit option) with | ||
| | None -> () | ||
| | Some _ -> () | ||
| |} | ||
| in | ||
| let pos = Position.create ~line:4 ~character:11 in | ||
| Util.test (`Pos pos) source; | ||
| [%expect | ||
| {| | ||
| { | ||
| "range": { | ||
| "end": { "character": 12, "line": 4 }, | ||
| "start": { "character": 11, "line": 4 } | ||
| }, | ||
| "content": "()" | ||
| } | ||
| |}] | ||
| ;; | ||
|
|
||
| let%expect_test "Perform `destruct` as custom request - 2" = | ||
| let source = | ||
| {| | ||
| type t = | ||
| | Foo | ||
| | Bar | ||
| | Baz of int option | ||
| let f: t -> unit = function Foo -> () | ||
| |} | ||
| in | ||
| let pos = | ||
| let start = Position.create ~line:5 ~character:28 | ||
| and end_ = Position.create ~line:5 ~character:31 in | ||
| Range.create ~start ~end_ | ||
| in | ||
| Util.test (`Range pos) source; | ||
| [%expect | ||
| {| | ||
| { | ||
| "range": { | ||
| "end": { "character": 37, "line": 5 }, | ||
| "start": { "character": 37, "line": 5 } | ||
| }, | ||
| "content": "\n| Bar | Baz _ -> _" | ||
| } | ||
| |}] | ||
| ;; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ! But 1.25.0 was released already, we need a new changelog section :-)
Next time we should add the section right after the release...