|
| 1 | +open Import |
| 2 | +module TextDocumentPositionParams = Lsp.Types.TextDocumentPositionParams |
| 3 | + |
| 4 | +let capability = ("handleTypeEnclosing", `Bool true) |
| 5 | + |
| 6 | +let meth = "ocamllsp/typeEnclosing" |
| 7 | + |
| 8 | +module Request_params = struct |
| 9 | + type t = |
| 10 | + { text_document : TextDocumentIdentifier.t |
| 11 | + ; at : [ `Range of Range.t | `Position of Position.t ] |
| 12 | + ; index : int |
| 13 | + ; verbosity : int |
| 14 | + } |
| 15 | + |
| 16 | + let yojson_of_at = function |
| 17 | + | `Range r -> Range.yojson_of_t r |
| 18 | + | `Position p -> Position.yojson_of_t p |
| 19 | + |
| 20 | + let yojson_of_t { text_document; index; at; verbosity } = |
| 21 | + match TextDocumentIdentifier.yojson_of_t text_document with |
| 22 | + | `Assoc assoc -> |
| 23 | + let index = ("index", `Int index) in |
| 24 | + let range_end = ("at", yojson_of_at at) in |
| 25 | + let verbosity = ("verbosity", `Int verbosity) in |
| 26 | + `Assoc (index :: range_end :: verbosity :: assoc) |
| 27 | + | _ -> (* unreachable *) assert false |
| 28 | + |
| 29 | + let create ?(verbosity = 0) ~text_document ~at ~index () = |
| 30 | + { text_document; index; at; verbosity } |
| 31 | + |
| 32 | + let json_error json = |
| 33 | + Json.error "invalid Req_type_enclosing.Request_params" json |
| 34 | + |
| 35 | + let index_of_yojson json params = |
| 36 | + match List.assoc_opt "index" params with |
| 37 | + | Some (`Int index) -> index |
| 38 | + | _ -> |
| 39 | + (* If the parameter is incorrectly formatted or missing, we refuse to build |
| 40 | + the parameter, [index] is mandatory. *) |
| 41 | + json_error json |
| 42 | + |
| 43 | + let verbosity_of_yojson params = |
| 44 | + match List.assoc_opt "verbosity" params with |
| 45 | + | Some (`Int verbosity) -> verbosity |
| 46 | + | _ -> |
| 47 | + (* If the parameter is incorrectly formatted or missing, it is assumed that |
| 48 | + the we ask for a verbosity level set to 0. *) |
| 49 | + 0 |
| 50 | + |
| 51 | + let at_of_yojson json params = |
| 52 | + match List.assoc_opt "at" params with |
| 53 | + | Some at -> ( |
| 54 | + try `Position (Position.t_of_yojson at) |
| 55 | + with _ -> `Range (Range.t_of_yojson at)) |
| 56 | + | _ -> |
| 57 | + (* If the parameter is incorrectly formatted or missing, we refuse to build |
| 58 | + the parameter, [at] is mandatory. *) |
| 59 | + json_error json |
| 60 | + |
| 61 | + let t_of_yojson = function |
| 62 | + | `Assoc params as json -> |
| 63 | + let verbosity = verbosity_of_yojson params in |
| 64 | + let at = at_of_yojson json params in |
| 65 | + let index = index_of_yojson json params in |
| 66 | + let text_document = TextDocumentIdentifier.t_of_yojson json in |
| 67 | + { index; at; verbosity; text_document } |
| 68 | + | json -> json_error json |
| 69 | +end |
| 70 | + |
| 71 | +type t = |
| 72 | + { index : int |
| 73 | + ; type_ : string |
| 74 | + ; enclosings : Range.t list |
| 75 | + } |
| 76 | + |
| 77 | +let yojson_of_t { index; type_; enclosings } = |
| 78 | + `Assoc |
| 79 | + [ ("index", `Int index) |
| 80 | + ; ("enclosings", `List (List.map ~f:Range.yojson_of_t enclosings)) |
| 81 | + ; ("type", `String type_) |
| 82 | + ] |
| 83 | + |
| 84 | +let config_with_given_verbosity config verbosity = |
| 85 | + let open Mconfig in |
| 86 | + { config with query = { config.query with verbosity } } |
| 87 | + |
| 88 | +let with_pipeline state uri verbosity with_pipeline = |
| 89 | + let doc = Document_store.get state.State.store uri in |
| 90 | + match Document.kind doc with |
| 91 | + | `Other -> Fiber.return `Null |
| 92 | + | `Merlin merlin -> |
| 93 | + let open Fiber.O in |
| 94 | + let* config = Document.Merlin.mconfig merlin in |
| 95 | + Document.Merlin.with_configurable_pipeline_exn |
| 96 | + ~config:(config_with_given_verbosity config verbosity) |
| 97 | + merlin |
| 98 | + with_pipeline |
| 99 | + |
| 100 | +let make_enclosing_command position index = |
| 101 | + Query_protocol.Type_enclosing (None, position, Some index) |
| 102 | + |
| 103 | +let get_first_enclosing_index range_end enclosings = |
| 104 | + List.find_mapi enclosings ~f:(fun i (loc, _, _) -> |
| 105 | + let range = Range.of_loc loc in |
| 106 | + match Position.compare range_end range.end_ with |
| 107 | + | Ordering.Lt | Ordering.Eq -> Some i |
| 108 | + | Ordering.Gt -> None) |
| 109 | + |
| 110 | +let dispatch_command pipeline command first_index index = |
| 111 | + let rec aux i acc = function |
| 112 | + | (_, `String typ, _) :: _ as enclosings when i = index -> |
| 113 | + Some |
| 114 | + ( typ |
| 115 | + , List.map |
| 116 | + ~f:(fun (loc, _, _) -> Range.of_loc loc) |
| 117 | + (List.rev_append acc enclosings) ) |
| 118 | + | curr :: enclosings -> aux (succ i) (curr :: acc) enclosings |
| 119 | + | [] -> None |
| 120 | + in |
| 121 | + let result = |
| 122 | + List.drop (Query_commands.dispatch pipeline command) first_index |
| 123 | + in |
| 124 | + aux 0 [] result |
| 125 | + |
| 126 | +let dispatch_with_range_end pipeline position index range_end = |
| 127 | + (* merlin's `type-enclosing` command takes a position and returns a list of |
| 128 | + increasing enclosures around that position. If it is given the [index] |
| 129 | + parameter, it annotates the corresponding enclosing with its type. |
| 130 | +
|
| 131 | + As the request would like to allow the target of an interval, we want to |
| 132 | + truncate the list of enclosures that include the interval. Something merlin |
| 133 | + cannot do. |
| 134 | +
|
| 135 | + We use a little hack where we use the `type-enclosing` command (with a |
| 136 | + negative index, so as not to make unnecessary computations) to calculate |
| 137 | + the enclosings around the given position. Then, we look for the index |
| 138 | + corresponding to the first enclosing included in the range which will act |
| 139 | + as an offset to calculate the real index, relative to the range *) |
| 140 | + let dummy_command = make_enclosing_command position (-1) in |
| 141 | + let enclosings = Query_commands.dispatch pipeline dummy_command in |
| 142 | + Option.bind |
| 143 | + (get_first_enclosing_index range_end enclosings) |
| 144 | + ~f:(fun first_index -> |
| 145 | + let real_index = first_index + index in |
| 146 | + let command = make_enclosing_command position real_index in |
| 147 | + dispatch_command pipeline command first_index index) |
| 148 | + |
| 149 | +let dispatch_without_range_end pipeline position index = |
| 150 | + let command = make_enclosing_command position index in |
| 151 | + dispatch_command pipeline command 0 index |
| 152 | + |
| 153 | +let dispatch_type_enclosing position index range_end pipeline = |
| 154 | + let position = Position.logical position in |
| 155 | + let result = |
| 156 | + match range_end with |
| 157 | + | None -> dispatch_without_range_end pipeline position index |
| 158 | + | Some range_end -> |
| 159 | + dispatch_with_range_end pipeline position index range_end |
| 160 | + in |
| 161 | + let type_, enclosings = |
| 162 | + match result with |
| 163 | + | None -> ("<no information>", []) |
| 164 | + | Some (typ, enclosings) -> (typ, enclosings) |
| 165 | + in |
| 166 | + yojson_of_t { index; type_; enclosings } |
| 167 | + |
| 168 | +let on_request ~params state = |
| 169 | + Fiber.of_thunk (fun () -> |
| 170 | + let params = (Option.value ~default:(`Assoc []) params :> Json.t) in |
| 171 | + let Request_params.{ index; verbosity; text_document; at } = |
| 172 | + Request_params.t_of_yojson params |
| 173 | + in |
| 174 | + let position, range_end = |
| 175 | + match at with |
| 176 | + | `Position p -> (p, None) |
| 177 | + | `Range r -> (r.start, Some r.end_) |
| 178 | + in |
| 179 | + let uri = text_document.uri in |
| 180 | + let verbosity = Mconfig.Verbosity.Lvl verbosity in |
| 181 | + with_pipeline state uri verbosity |
| 182 | + @@ dispatch_type_enclosing position index range_end) |
0 commit comments