|
| 1 | +open Protocol |
| 2 | + |
1 | 3 | type kind = RefactorRewrite
|
2 | 4 |
|
3 | 5 | let kindToString kind = match kind with RefactorRewrite -> "refactor.rewrite"
|
4 | 6 |
|
5 |
| -module TextDocument = struct |
6 |
| - type t = {version : string option; uri : string} |
7 |
| - |
8 |
| - let make ~version ~uri = {version; uri} |
9 |
| - |
10 |
| - let toString t = |
11 |
| - Printf.sprintf {|{"version": %s, "uri": "%s"}|} |
12 |
| - (match t.version with |
13 |
| - | Some version -> "\"" ^ Json.escape version ^ "\"" |
14 |
| - | None -> "null") |
15 |
| - t.uri |
16 |
| -end |
17 |
| - |
18 |
| -module Position = struct |
19 |
| - type t = {line : int; character : int} |
20 |
| - |
21 |
| - let make ~line ~character = {line; character} |
22 |
| - |
23 |
| - let toString t = |
24 |
| - Printf.sprintf {|{"line": %s, "character": %s}|} (string_of_int t.line) |
25 |
| - (string_of_int t.character) |
26 |
| -end |
| 7 | +type codeAction = {title : string; kind : kind; edit : codeActionEdit} |
27 | 8 |
|
28 |
| -module TextEditRange = struct |
29 |
| - type t = {start : Position.t; end_ : Position.t} |
30 |
| - |
31 |
| - let make ~start ~end_ = {start; end_} |
32 |
| - |
33 |
| - let toString t = |
34 |
| - Printf.sprintf {|{"start": %s, "end": %s}|} |
35 |
| - (t.start |> Position.toString) |
36 |
| - (t.end_ |> Position.toString) |
37 |
| -end |
38 |
| - |
39 |
| -module TextEdit = struct |
40 |
| - type t = {newText : string; range : TextEditRange.t} |
41 |
| - |
42 |
| - let make ~newText ~range = {newText; range} |
43 |
| - |
44 |
| - let toString t = |
45 |
| - Printf.sprintf {|{"newText": "%s", "range": %s}|} (Json.escape t.newText) |
46 |
| - (t.range |> TextEditRange.toString) |
47 |
| -end |
| 9 | +let stringifyCodeAction ca = |
| 10 | + Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title |
| 11 | + (kindToString ca.kind) |
| 12 | + (ca.edit |> stringifyCodeActionEdit) |
48 | 13 |
|
49 |
| -module DocumentChange = struct |
50 |
| - type t = {textDocument : TextDocument.t; edits : TextEdit.t list} |
51 |
| - |
52 |
| - let make ~textDocument ~edits = {textDocument; edits} |
53 |
| - |
54 |
| - let toString t = |
55 |
| - Printf.sprintf {|{"textDocument": %s, "edits": [%s]}|} |
56 |
| - (t.textDocument |> TextDocument.toString) |
57 |
| - (t.edits |
58 |
| - |> List.map (fun edit -> edit |> TextEdit.toString) |
59 |
| - |> String.concat ",") |
60 |
| -end |
61 |
| - |
62 |
| -module CodeActionEdit = struct |
63 |
| - type t = {documentChanges : DocumentChange.t list} |
64 |
| - |
65 |
| - let make ~documentChanges = {documentChanges} |
| 14 | +(* This is the return that's expected when resolving code actions *) |
| 15 | +type result = codeAction list |
66 | 16 |
|
67 |
| - let toString t = |
68 |
| - Printf.sprintf {|{"documentChanges": [%s]}|} |
69 |
| - (t.documentChanges |
70 |
| - |> List.map (fun documentChange -> |
71 |
| - documentChange |> DocumentChange.toString) |
72 |
| - |> String.concat ",") |
73 |
| -end |
| 17 | +let stringifyCodeActions codeActions = |
| 18 | + Printf.sprintf {|%s|} (codeActions |> List.map stringifyCodeAction |> array) |
74 | 19 |
|
75 | 20 | module CodeAction = struct
|
76 |
| - type t = {title : string; kind : kind; edit : CodeActionEdit.t} |
77 |
| - |
78 |
| - let make ~title ~kind ~edit = {title; kind; edit} |
79 |
| - |
80 |
| - let toString t = |
81 |
| - Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} t.title |
82 |
| - (kindToString t.kind) |
83 |
| - (t.edit |> CodeActionEdit.toString) |
| 21 | + let makeRangeReplace ~title ~kind ~file ~newText ~range = |
| 22 | + { |
| 23 | + title; |
| 24 | + kind; |
| 25 | + edit = |
| 26 | + { |
| 27 | + documentChanges = |
| 28 | + [ |
| 29 | + { |
| 30 | + textDocument = {version = None; uri = file}; |
| 31 | + edits = [{newText; range}]; |
| 32 | + }; |
| 33 | + ]; |
| 34 | + }; |
| 35 | + } |
84 | 36 | end
|
85 |
| - |
86 |
| -(* This is the return that's expected when resolving code actions *) |
87 |
| -type result = CodeAction.t list |
88 |
| - |
89 |
| -let stringifyCodeActions codeActions = |
90 |
| - Printf.sprintf {|[%s]|} |
91 |
| - (codeActions |
92 |
| - |> List.map (fun codeAction -> codeAction |> CodeAction.toString) |
93 |
| - |> String.concat ",") |
0 commit comments