|
| 1 | +open Import |
| 2 | +module TextDocumentPositionParams = Lsp.Types.TextDocumentPositionParams |
| 3 | + |
| 4 | +let meth = "ocamllsp/typeSearch" |
| 5 | +let capability = "handleTypeSearch", `Bool true |
| 6 | + |
| 7 | +module TypeSearchParams = struct |
| 8 | + type t = |
| 9 | + { text_document : TextDocumentIdentifier.t |
| 10 | + ; position : Position.t |
| 11 | + ; limit : int |
| 12 | + ; query : string |
| 13 | + ; with_doc : bool |
| 14 | + ; doc_format : MarkupKind.t option |
| 15 | + } |
| 16 | + |
| 17 | + let t_of_yojson json = |
| 18 | + let open Yojson.Safe.Util in |
| 19 | + let textDocumentPosition = Lsp.Types.TextDocumentPositionParams.t_of_yojson json in |
| 20 | + let query = json |> member "query" |> to_string in |
| 21 | + let limit = json |> member "limit" |> to_int in |
| 22 | + let with_doc = json |> member "with_doc" |> to_bool in |
| 23 | + let doc_format = json |> member "doc_format" |> to_option MarkupKind.t_of_yojson in |
| 24 | + { position = textDocumentPosition.position |
| 25 | + ; text_document = textDocumentPosition.textDocument |
| 26 | + ; query |
| 27 | + ; limit |
| 28 | + ; with_doc |
| 29 | + ; doc_format |
| 30 | + } |
| 31 | + ;; |
| 32 | + |
| 33 | + let yojson_of_t { text_document; position; query; limit; with_doc; doc_format } = |
| 34 | + let doc_format = |
| 35 | + match doc_format with |
| 36 | + | Some format -> [ "doc_format", MarkupKind.yojson_of_t format ] |
| 37 | + | None -> [] |
| 38 | + in |
| 39 | + `Assoc |
| 40 | + (("textDocument", TextDocumentIdentifier.yojson_of_t text_document) |
| 41 | + :: ("position", Position.yojson_of_t position) |
| 42 | + :: ("limit", `Int limit) |
| 43 | + :: ("with_doc", `Bool with_doc) |
| 44 | + :: ("query", `String query) |
| 45 | + :: doc_format) |
| 46 | + ;; |
| 47 | +end |
| 48 | + |
| 49 | +module TypeSearch = struct |
| 50 | + type t = string Query_protocol.type_search_result list |
| 51 | + |
| 52 | + let doc_to_markupContent ~kind ~value = |
| 53 | + let v = |
| 54 | + match kind with |
| 55 | + | MarkupKind.Markdown -> |
| 56 | + (match Doc_to_md.translate value with |
| 57 | + | Raw d -> d |
| 58 | + | Markdown d -> d) |
| 59 | + | MarkupKind.PlainText -> value |
| 60 | + in |
| 61 | + MarkupContent.create ~kind ~value:v |
| 62 | + ;; |
| 63 | + |
| 64 | + let yojson_of_t (t : t) doc_format = |
| 65 | + let format = |
| 66 | + match doc_format with |
| 67 | + | Some format -> format |
| 68 | + | None -> MarkupKind.PlainText |
| 69 | + in |
| 70 | + let yojson_of_type_search_result (res : string Query_protocol.type_search_result) = |
| 71 | + `Assoc |
| 72 | + [ "name", `String res.name |
| 73 | + ; "typ", `String res.typ |
| 74 | + ; "loc", Range.yojson_of_t (Range.of_loc res.loc) |
| 75 | + ; ( "doc" |
| 76 | + , match res.doc with |
| 77 | + | Some value -> |
| 78 | + doc_to_markupContent ~kind:format ~value |> MarkupContent.yojson_of_t |
| 79 | + | None -> `Null ) |
| 80 | + ; "cost", `Int res.cost |
| 81 | + ; "constructible", `String res.constructible |
| 82 | + ] |
| 83 | + in |
| 84 | + `List (List.map ~f:yojson_of_type_search_result t) |
| 85 | + ;; |
| 86 | +end |
| 87 | + |
| 88 | +type t = TypeSearch.t |
| 89 | + |
| 90 | +module Request_params = struct |
| 91 | + type t = TypeSearchParams.t |
| 92 | + |
| 93 | + let yojson_of_t t = TypeSearchParams.yojson_of_t t |
| 94 | + |
| 95 | + let create text_document position limit query with_doc doc_format : t = |
| 96 | + { text_document; position; limit; query; with_doc; doc_format } |
| 97 | + ;; |
| 98 | +end |
| 99 | + |
| 100 | +let dispatch merlin position limit query with_doc doc_format = |
| 101 | + Document.Merlin.with_pipeline_exn merlin (fun pipeline -> |
| 102 | + let position = Position.logical position in |
| 103 | + let query = Query_protocol.Type_search (query, position, limit, with_doc) in |
| 104 | + let results = Query_commands.dispatch pipeline query in |
| 105 | + TypeSearch.yojson_of_t results doc_format) |
| 106 | +;; |
| 107 | + |
| 108 | +let on_request ~params state = |
| 109 | + Fiber.of_thunk (fun () -> |
| 110 | + let params = (Option.value ~default:(`Assoc []) params :> Yojson.Safe.t) in |
| 111 | + let TypeSearchParams.{ text_document; position; limit; query; with_doc; doc_format } = |
| 112 | + TypeSearchParams.t_of_yojson params |
| 113 | + in |
| 114 | + let uri = text_document.uri in |
| 115 | + let doc = Document_store.get state.State.store uri in |
| 116 | + match Document.kind doc with |
| 117 | + | `Other -> Fiber.return `Null |
| 118 | + | `Merlin merlin -> dispatch merlin position limit query with_doc doc_format) |
| 119 | +;; |
0 commit comments