|
| 1 | +open Import |
| 2 | +module TextDocumentPositionParams = Lsp.Types.TextDocumentPositionParams |
| 3 | + |
| 4 | +let meth = "ocamllsp/jump" |
| 5 | +let capability = "handleJump", `Bool true |
| 6 | + |
| 7 | +module JumpParams = struct |
| 8 | + let targets = |
| 9 | + [ "fun" |
| 10 | + ; "match" |
| 11 | + ; "let" |
| 12 | + ; "module" |
| 13 | + ; "module-type" |
| 14 | + ; "match-next-case" |
| 15 | + ; "match-prev-case" |
| 16 | + ] |
| 17 | + ;; |
| 18 | + |
| 19 | + type t = |
| 20 | + { textDocument : TextDocumentIdentifier.t |
| 21 | + ; position : Position.t |
| 22 | + ; target : string option |
| 23 | + } |
| 24 | + |
| 25 | + let t_of_yojson json = |
| 26 | + let open Yojson.Safe.Util in |
| 27 | + { textDocument = json |> member "textDocument" |> TextDocumentIdentifier.t_of_yojson |
| 28 | + ; position = json |> member "position" |> Position.t_of_yojson |
| 29 | + ; target = json |> member "target" |> to_string_option |
| 30 | + } |
| 31 | + ;; |
| 32 | + |
| 33 | + let yojson_of_t { textDocument; position; target } = |
| 34 | + let target = |
| 35 | + Option.value_map target ~default:[] ~f:(fun v -> [ "target", `String v ]) |
| 36 | + in |
| 37 | + `Assoc |
| 38 | + (("textDocument", TextDocumentIdentifier.yojson_of_t textDocument) |
| 39 | + :: ("position", Position.yojson_of_t position) |
| 40 | + :: target) |
| 41 | + ;; |
| 42 | +end |
| 43 | + |
| 44 | +module Jump = struct |
| 45 | + type t = (string * Position.t) list |
| 46 | + |
| 47 | + let yojson_of_t (lst : t) : Yojson.Safe.t = |
| 48 | + let jumps = |
| 49 | + List.map |
| 50 | + ~f:(fun (target, position) -> |
| 51 | + `Assoc [ "target", `String target; "position", Position.yojson_of_t position ]) |
| 52 | + lst |
| 53 | + in |
| 54 | + `Assoc [ "jumps", `List jumps ] |
| 55 | + ;; |
| 56 | +end |
| 57 | + |
| 58 | +type t = Jump.t |
| 59 | + |
| 60 | +module Request_params = struct |
| 61 | + type t = JumpParams.t |
| 62 | + |
| 63 | + let yojson_of_t t = JumpParams.yojson_of_t t |
| 64 | + |
| 65 | + let create ~uri ~position ~target = |
| 66 | + { JumpParams.textDocument = TextDocumentIdentifier.create ~uri; position; target } |
| 67 | + ;; |
| 68 | +end |
| 69 | + |
| 70 | +let dispatch ~merlin ~position ~target = |
| 71 | + Document.Merlin.with_pipeline_exn merlin (fun pipeline -> |
| 72 | + let pposition = Position.logical position in |
| 73 | + let query = Query_protocol.Jump (target, pposition) in |
| 74 | + Query_commands.dispatch pipeline query) |
| 75 | +;; |
| 76 | + |
| 77 | +let on_request ~params state = |
| 78 | + let open Fiber.O in |
| 79 | + Fiber.of_thunk (fun () -> |
| 80 | + let params = (Option.value ~default:(`Assoc []) params :> Yojson.Safe.t) in |
| 81 | + let params = JumpParams.t_of_yojson params in |
| 82 | + let uri = params.textDocument.uri in |
| 83 | + let position = params.position in |
| 84 | + let doc = Document_store.get state.State.store uri in |
| 85 | + match Document.kind doc with |
| 86 | + | `Other -> Fiber.return `Null |
| 87 | + | `Merlin merlin -> |
| 88 | + let targets = |
| 89 | + match params.target with |
| 90 | + | None -> JumpParams.targets |
| 91 | + | Some target -> [ target ] |
| 92 | + in |
| 93 | + let+ results = |
| 94 | + Fiber.parallel_map targets ~f:(fun target -> |
| 95 | + dispatch ~merlin ~position ~target |
| 96 | + |> Fiber.map ~f:(function |
| 97 | + | `Error _ -> None |
| 98 | + | `Found pos -> |
| 99 | + (match Position.of_lexical_position pos with |
| 100 | + | None -> None |
| 101 | + | Some position -> Some (target, position)))) |
| 102 | + in |
| 103 | + Jump.yojson_of_t (List.filter_map results ~f:Fun.id)) |
| 104 | +;; |
0 commit comments