|
| 1 | +open Import |
| 2 | +open Fiber.O |
| 3 | +open Stdune |
| 4 | + |
| 5 | +let command_name = "ocamllsp/merlin-jump-to-target" |
| 6 | + |
| 7 | +let targets = |
| 8 | + [ "fun"; "match"; "let"; "module"; "module-type"; "match-next-case"; "match-prev-case" ] |
| 9 | +;; |
| 10 | + |
| 11 | +let available (capabilities : ShowDocumentClientCapabilities.t option) = |
| 12 | + match capabilities with |
| 13 | + | Some { support } -> support |
| 14 | + | None -> false |
| 15 | +;; |
| 16 | + |
| 17 | +let error message = |
| 18 | + Jsonrpc.Response.Error.raise |
| 19 | + @@ Jsonrpc.Response.Error.make |
| 20 | + ~code:Jsonrpc.Response.Error.Code.InvalidParams |
| 21 | + ~message |
| 22 | + () |
| 23 | +;; |
| 24 | + |
| 25 | +let command_run server (params : ExecuteCommandParams.t) = |
| 26 | + let uri, range = |
| 27 | + match params.arguments with |
| 28 | + | Some [ json_uri; json_range ] -> |
| 29 | + let uri = DocumentUri.t_of_yojson json_uri in |
| 30 | + let range = Range.t_of_yojson json_range in |
| 31 | + uri, range |
| 32 | + | None | Some _ -> error "takes a URI and a range as input" |
| 33 | + in |
| 34 | + let+ { ShowDocumentResult.success } = |
| 35 | + let req = ShowDocumentParams.create ~uri ~selection:range ~takeFocus:true () in |
| 36 | + Server.request server (Server_request.ShowDocumentRequest req) |
| 37 | + in |
| 38 | + if not success |
| 39 | + then ( |
| 40 | + let uri = Uri.to_string uri in |
| 41 | + Format.eprintf "failed to open %s@." uri); |
| 42 | + `Null |
| 43 | +;; |
| 44 | + |
| 45 | +(* Dispatch the jump request to Merlin and get the result *) |
| 46 | +let process_jump_request ~merlin ~position ~target = |
| 47 | + let+ results = |
| 48 | + Document.Merlin.with_pipeline_exn merlin (fun pipeline -> |
| 49 | + let pposition = Position.logical position in |
| 50 | + let query = Query_protocol.Jump (target, pposition) in |
| 51 | + Query_commands.dispatch pipeline query) |
| 52 | + in |
| 53 | + match results with |
| 54 | + | `Error _ -> None |
| 55 | + | `Found pos -> Some pos |
| 56 | +;; |
| 57 | + |
| 58 | +let code_actions |
| 59 | + (doc : Document.t) |
| 60 | + (params : CodeActionParams.t) |
| 61 | + (capabilities : ShowDocumentClientCapabilities.t option) |
| 62 | + = |
| 63 | + match Document.kind doc with |
| 64 | + | `Merlin merlin when available capabilities -> |
| 65 | + let+ actions = |
| 66 | + (* TODO: Merlin Jump command that returns all available jump locations for a source code buffer. *) |
| 67 | + Fiber.parallel_map targets ~f:(fun target -> |
| 68 | + let+ res = process_jump_request ~merlin ~position:params.range.start ~target in |
| 69 | + let open Option.O in |
| 70 | + let* lexing_pos = res in |
| 71 | + let+ position = Position.of_lexical_position lexing_pos in |
| 72 | + let uri = Document.uri doc in |
| 73 | + let range = { Range.start = position; end_ = position } in |
| 74 | + let title = sprintf "Jump to %s" target in |
| 75 | + let command = |
| 76 | + let arguments = [ DocumentUri.yojson_of_t uri; Range.yojson_of_t range ] in |
| 77 | + Command.create ~title ~command:command_name ~arguments () |
| 78 | + in |
| 79 | + CodeAction.create ~title ~kind:(CodeActionKind.Other "merlin-jump") ~command ()) |
| 80 | + in |
| 81 | + List.filter_opt actions |
| 82 | + | _ -> Fiber.return [] |
| 83 | +;; |
0 commit comments