-
Notifications
You must be signed in to change notification settings - Fork 155
Jump Custom Request #1374
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
Jump Custom Request #1374
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3a62bbf
jump custom query
PizieDust 4b55404
remove unneded code
PizieDust 205d45d
tests
PizieDust b15d288
add changelog
PizieDust 5154901
rename
PizieDust 3068f87
lint
PizieDust dda8ffd
return a single position instead of a range
PizieDust a2da6e6
review changes suggested by @voodoos
PizieDust eeb01fd
move changelog to unreleased section
PizieDust 26a2150
add jump to capabilities
PizieDust 37c427e
return a list from jump custom request
PizieDust 76ee3b8
wrap result in key jumps
PizieDust 1d149c9
Merge remote-tracking branch 'origin/master' into jump_cq
voodoos 1724a87
Allows client to request a specific target. If not, all possible targ…
voodoos 5a9f4b4
Update the request documentation and the changelog entry.
voodoos 27b739a
Fix indent
voodoos 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,42 @@ | ||
| # Merlin Jump Request | ||
|
|
||
| ## Description | ||
|
|
||
| This custom request allows Merlin-type code navigation in a source buffer. | ||
|
|
||
| ## Server capability | ||
|
|
||
| - propert name: `handleMerlinJump` | ||
| - property type: `boolean` | ||
|
|
||
| ## Request | ||
|
|
||
| ```js | ||
| export interface JumpParams extends TextDocumentPositionParams | ||
| { | ||
| target: string; | ||
voodoos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| - method: `ocamllsp/merlinJump` | ||
| - params: | ||
| - `TextDocumentIdentifier`: Specifies the document for which the request is sent. It includes a uri property that points to the document. | ||
| - `Position`: Specifies the position in the document for which the documentation is requested. It includes line and character properties. | ||
| More details can be found in the [TextDocumentPositionParams - LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentPositionParams). | ||
| - `Target`: A string representing the identifier within the document to search for and jump to. | ||
PizieDust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Response | ||
|
|
||
| ```js | ||
| result: Jump | String | ||
| export interface Jump extends TextDocumentPositionParams { | ||
| } | ||
| ``` | ||
|
|
||
| - result: | ||
| - Type: Jump or string | ||
| - Description: If the jump is successful, a position and document path is returned. If no relevant jump location is found, the result will be a string "no matching target" or an error message. | ||
| - Jump: | ||
| - Type: TextDocumentPositionParams | ||
| - `Position`: The position to jump to | ||
| - `TextDocumentIdentifier`: the document path which contains this position (ideally the same document as the request) | ||
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,80 @@ | ||
| open Import | ||
| module TextDocumentPositionParams = Lsp.Types.TextDocumentPositionParams | ||
|
|
||
| let meth = "ocamllsp/jump" | ||
| let capability = "handleJump", `Bool true | ||
|
|
||
| module JumpParams = struct | ||
| type t = | ||
| { text_document : TextDocumentIdentifier.t | ||
| ; position : Position.t | ||
| ; target : string | ||
| } | ||
|
|
||
| let t_of_yojson json = | ||
| let open Yojson.Safe.Util in | ||
| let textDocumentPosition = TextDocumentPositionParams.t_of_yojson json in | ||
| let target = json |> member "target" |> to_string in | ||
| { position = textDocumentPosition.position | ||
| ; text_document = textDocumentPosition.textDocument | ||
| ; target | ||
| } | ||
| ;; | ||
|
|
||
| let yojson_of_t { text_document; position; target } = | ||
| `Assoc | ||
| [ "textDocument", TextDocumentIdentifier.yojson_of_t text_document | ||
| ; "position", Position.yojson_of_t position | ||
| ; "target", `String target | ||
| ] | ||
| ;; | ||
| end | ||
|
|
||
| module Jump = struct | ||
| type t = Lsp.Types.TextDocumentPositionParams.t | ||
|
|
||
| let yojson_of_t t = TextDocumentPositionParams.yojson_of_t t | ||
| let t_of_yojson json = Lsp.Types.TextDocumentPositionParams.t_of_yojson json | ||
| end | ||
|
|
||
| type t = Jump.t | ||
|
|
||
| let t_of_yojson json = Jump.t_of_yojson json | ||
|
|
||
| module Request_params = struct | ||
| type t = JumpParams.t | ||
|
|
||
| let yojson_of_t t = JumpParams.yojson_of_t t | ||
| let create ~text_document ~position ~target () : t = { text_document; position; target } | ||
| end | ||
|
|
||
| let dispatch ~merlin ~position ~target = | ||
| Document.Merlin.with_pipeline_exn merlin (fun pipeline -> | ||
| let pposition = Position.logical position in | ||
| let query = Query_protocol.Jump (target, pposition) in | ||
| Query_commands.dispatch pipeline query) | ||
| ;; | ||
|
|
||
| let on_request ~params state = | ||
| Fiber.of_thunk (fun () -> | ||
| let params = (Option.value ~default:(`Assoc []) params :> Yojson.Safe.t) in | ||
| let JumpParams.{ text_document; position; target } = JumpParams.t_of_yojson params in | ||
| let uri = text_document.uri in | ||
| let doc = Document_store.get state.State.store uri in | ||
| match Document.kind doc with | ||
| | `Other -> Fiber.return `Null | ||
| | `Merlin merlin -> | ||
| Fiber.bind (dispatch ~merlin ~position ~target) ~f:(fun res -> | ||
PizieDust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match res with | ||
| | `Error err -> Fiber.return (`String err) | ||
| | `Found pos -> | ||
| (match Position.of_lexical_position pos with | ||
| | None -> Fiber.return `Null | ||
| | Some position -> | ||
| let loc = | ||
| Lsp.Types.TextDocumentPositionParams.create | ||
| ~position | ||
| ~textDocument:(TextDocumentIdentifier.create ~uri) | ||
| in | ||
| Fiber.return (Jump.yojson_of_t loc)))) | ||
| ;; | ||
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,21 @@ | ||
| open Import | ||
|
|
||
| module Request_params : sig | ||
| type t | ||
|
|
||
| val yojson_of_t : t -> Json.t | ||
|
|
||
| val create | ||
| : text_document:TextDocumentIdentifier.t | ||
| -> position:Position.t | ||
| -> target:string | ||
| -> unit | ||
| -> t | ||
| end | ||
|
|
||
| type t | ||
|
|
||
| val t_of_yojson : Json.t -> t | ||
PizieDust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val meth : string | ||
| val capability : string * [> `Bool of bool ] | ||
| 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 |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ | |
| test | ||
| type_enclosing | ||
| documentation | ||
| merlin_jump | ||
| with_pp | ||
| with_ppx | ||
| workspace_change_config)))) | ||
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,94 @@ | ||
| open Test.Import | ||
| module Req = Ocaml_lsp_server.Custom_request.Merlin_jump | ||
|
|
||
| module Util = struct | ||
| let call_jump position target client = | ||
| let uri = DocumentUri.of_path "test.ml" in | ||
| let text_document = TextDocumentIdentifier.create ~uri in | ||
| let params = | ||
| Req.Request_params.create ~text_document ~position ~target () | ||
| |> Req.Request_params.yojson_of_t | ||
| |> Jsonrpc.Structured.t_of_yojson | ||
| |> Option.some | ||
| in | ||
| let req = Lsp.Client_request.UnknownRequest { meth = "ocamllsp/jump"; params } in | ||
| Client.request client req | ||
| ;; | ||
|
|
||
| let test ~line ~character ~target ~source = | ||
| let position = Position.create ~character ~line in | ||
| let request client = | ||
| let open Fiber.O in | ||
| let+ response = call_jump position target client in | ||
| Test.print_result response | ||
| in | ||
| Helpers.test source request | ||
| ;; | ||
| end | ||
|
|
||
| let%expect_test "Get location of the next match case" = | ||
| let source = | ||
| {| | ||
| let find_vowel x = | ||
| match x with | ||
| | 'A' -> true | ||
| | 'E' -> true | ||
| | 'I' -> true | ||
| | 'O' -> true | ||
| | 'U' -> true | ||
| | _ -> false | ||
| |} | ||
| in | ||
| let line = 3 in | ||
| let character = 2 in | ||
| let target = "match-next-case" in | ||
| Util.test ~line ~character ~target ~source; | ||
| [%expect | ||
| {| | ||
| { | ||
| "position": { "character": 2, "line": 4 }, | ||
| "textDocument": { "uri": "file:///test.ml" } | ||
| } |}] | ||
| ;; | ||
|
|
||
| let%expect_test "Get location of a the module" = | ||
| let source = | ||
| {|type a = Foo | Bar | ||
|
|
||
| module A = struct | ||
| let f () = 10 | ||
| let g = Bar | ||
| let h x = x | ||
|
|
||
| module B = struct | ||
| type b = Baz | ||
|
|
||
| let x = (Baz, 10) | ||
| let y = (Bar, Foo) | ||
| end | ||
|
|
||
| type t = { a : string; b : float } | ||
|
|
||
| let z = { a = "Hello"; b = 1.0 } | ||
| end|} | ||
| in | ||
| let line = 10 in | ||
| let character = 3 in | ||
| let target = "module" in | ||
| Util.test ~line ~character ~target ~source; | ||
| [%expect | ||
| {| | ||
| { | ||
| "position": { "character": 2, "line": 7 }, | ||
| "textDocument": { "uri": "file:///test.ml" } | ||
| } |}] | ||
| ;; | ||
|
|
||
| let%expect_test "Same line should output no locations" = | ||
| let source = {|let x = 5 |} in | ||
| let line = 1 in | ||
| let character = 5 in | ||
| let target = "let" in | ||
| Util.test ~line ~character ~target ~source; | ||
| [%expect {| "No matching target" |}] | ||
| ;; |
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.
Uh oh!
There was an error while loading. Please reload this page.