|
| 1 | +open Import |
| 2 | + |
| 3 | +let capability = "handleJumpToTypedHole", `Bool true |
| 4 | +let meth = "ocamllsp/jumpToTypedHole" |
| 5 | + |
| 6 | +module Request_params = struct |
| 7 | + type t = |
| 8 | + { text_document : TextDocumentIdentifier.t |
| 9 | + ; position : Position.t |
| 10 | + ; range : Range.t option |
| 11 | + ; direction : [ `Next | `Prev ] |
| 12 | + } |
| 13 | + |
| 14 | + let create ?(direction = `Next) ?range ~text_document ~position () = |
| 15 | + { text_document; position; direction; range } |
| 16 | + ;; |
| 17 | + |
| 18 | + let yojson_of_direction = function |
| 19 | + | `Next -> `String "next" |
| 20 | + | `Prev -> `String "prev" |
| 21 | + ;; |
| 22 | + |
| 23 | + let yojson_of_t { text_document; position; direction; range } = |
| 24 | + match TextDocumentIdentifier.yojson_of_t text_document with |
| 25 | + | `Assoc assoc -> |
| 26 | + let position = "position", Position.yojson_of_t position in |
| 27 | + let range = |
| 28 | + ( "range" |
| 29 | + , match range with |
| 30 | + | None -> `Null |
| 31 | + | Some r -> Range.yojson_of_t r ) |
| 32 | + in |
| 33 | + let direction = "direction", yojson_of_direction direction in |
| 34 | + `Assoc (direction :: position :: range :: assoc) |
| 35 | + | _ -> (* unreachable *) assert false |
| 36 | + ;; |
| 37 | + |
| 38 | + let direction_of_yojson json = |
| 39 | + let open Yojson.Safe.Util in |
| 40 | + let dir = json |> member "direction" |> to_string in |
| 41 | + match String.lowercase_ascii dir with |
| 42 | + | "prev" -> `Prev |
| 43 | + | _ -> `Next |
| 44 | + ;; |
| 45 | + |
| 46 | + let t_of_yojson json = |
| 47 | + let open Yojson.Safe.Util in |
| 48 | + let text_document = TextDocumentIdentifier.t_of_yojson json in |
| 49 | + let position = json |> member "position" |> Position.t_of_yojson in |
| 50 | + let direction = direction_of_yojson json in |
| 51 | + let range = json |> member "range" |> to_option Range.t_of_yojson in |
| 52 | + { text_document; position; direction; range } |
| 53 | + ;; |
| 54 | +end |
| 55 | + |
| 56 | +type t = Range.t option |
| 57 | + |
| 58 | +let t_of_yojson opt = |
| 59 | + let open Yojson.Safe.Util in |
| 60 | + to_option Range.t_of_yojson opt |
| 61 | +;; |
| 62 | + |
| 63 | +let yojson_of_t = function |
| 64 | + | None -> `Null |
| 65 | + | Some range -> Range.yojson_of_t range |
| 66 | +;; |
| 67 | + |
| 68 | +let on_request ~(params : Jsonrpc.Structured.t option) (state : State.t) = |
| 69 | + Fiber.of_thunk (fun () -> |
| 70 | + let params = (Option.value ~default:(`Assoc []) params :> Json.t) in |
| 71 | + let Request_params.{ text_document = { uri }; position; direction; range } = |
| 72 | + Request_params.t_of_yojson params |
| 73 | + in |
| 74 | + match Document_store.get_opt state.store uri with |
| 75 | + | Some doc -> |
| 76 | + let open Fiber.O in |
| 77 | + let merlin = Document.merlin_exn doc in |
| 78 | + let+ holes = Typed_hole.all ~pipeline_name:"jump-to-typed-hole" merlin in |
| 79 | + holes |> Typed_hole.find ~position ~range ~direction |> yojson_of_t |
| 80 | + | None -> |
| 81 | + Jsonrpc.Response.Error.raise |
| 82 | + @@ Jsonrpc.Response.Error.make |
| 83 | + ~code:Jsonrpc.Response.Error.Code.InvalidParams |
| 84 | + ~message: |
| 85 | + (Printf.sprintf |
| 86 | + "Document %s wasn't found in the document store" |
| 87 | + (Uri.to_string uri)) |
| 88 | + ()) |
| 89 | +;; |
0 commit comments