|
| 1 | +open Import |
| 2 | + |
| 3 | +let capability = "handleDestruct", `Bool true |
| 4 | +let meth = "ocamllsp/destruct" |
| 5 | + |
| 6 | +module Request_params = struct |
| 7 | + type t = |
| 8 | + { text_document : TextDocumentIdentifier.t |
| 9 | + ; range : Range.t |
| 10 | + } |
| 11 | + |
| 12 | + let create ~text_document ~range () = { text_document; range } |
| 13 | + |
| 14 | + let yojson_of_t { text_document; range } = |
| 15 | + match TextDocumentIdentifier.yojson_of_t text_document with |
| 16 | + | `Assoc assoc -> `Assoc (("range", Range.yojson_of_t range) :: assoc) |
| 17 | + | _ -> (* unreachable *) assert false |
| 18 | + ;; |
| 19 | + |
| 20 | + let t_of_yojson json = |
| 21 | + let open Yojson.Safe.Util in |
| 22 | + let text_document = json |> TextDocumentIdentifier.t_of_yojson in |
| 23 | + let range = json |> member "range" |> Range.t_of_yojson in |
| 24 | + create ~text_document ~range () |
| 25 | + ;; |
| 26 | +end |
| 27 | + |
| 28 | +type t = |
| 29 | + { range : Range.t |
| 30 | + ; content : string |
| 31 | + } |
| 32 | + |
| 33 | +let t_of_yojson json = |
| 34 | + let open Yojson.Safe.Util in |
| 35 | + let range = json |> member "range" |> Range.t_of_yojson |
| 36 | + and content = json |> member "content" |> to_string in |
| 37 | + { range; content } |
| 38 | +;; |
| 39 | + |
| 40 | +let yojson_of_t { range; content } = |
| 41 | + `Assoc [ "range", Range.yojson_of_t range; "content", `String content ] |
| 42 | +;; |
| 43 | + |
| 44 | +let with_pipeline state uri f = |
| 45 | + let doc = Document_store.get state.State.store uri in |
| 46 | + match Document.kind doc with |
| 47 | + | `Other -> Fiber.return `Null |
| 48 | + | `Merlin merlin -> |
| 49 | + (match Document.Merlin.kind merlin with |
| 50 | + | Document.Kind.Intf -> |
| 51 | + (* Destruct makes no sense if it's called from an interface. *) |
| 52 | + Fiber.return `Null |
| 53 | + | Document.Kind.Impl -> Document.Merlin.with_pipeline_exn merlin f) |
| 54 | +;; |
| 55 | + |
| 56 | +let make_destruct_command start stop = Query_protocol.Case_analysis (start, stop) |
| 57 | + |
| 58 | +let dispatch_destruct range pipeline = |
| 59 | + let start = range.Range.start |> Position.logical |
| 60 | + and stop = range.Range.end_ |> Position.logical in |
| 61 | + let command = make_destruct_command start stop in |
| 62 | + let loc, content = Query_commands.dispatch pipeline command in |
| 63 | + yojson_of_t { content; range = Range.of_loc loc } |
| 64 | +;; |
| 65 | + |
| 66 | +let on_request ~params state = |
| 67 | + Fiber.of_thunk (fun () -> |
| 68 | + let params = (Option.value ~default:(`Assoc []) params :> Json.t) in |
| 69 | + let Request_params.{ text_document; range } = Request_params.t_of_yojson params in |
| 70 | + let uri = text_document.uri in |
| 71 | + with_pipeline state uri @@ dispatch_destruct range) |
| 72 | +;; |
0 commit comments