Skip to content

Commit 4b75a02

Browse files
committed
prepare code actions in analysis bin
1 parent 1e5fb49 commit 4b75a02

File tree

3 files changed

+71
-41
lines changed

3 files changed

+71
-41
lines changed

analysis/src/CodeActions.ml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ let kindToString kind = match kind with RefactorRewrite -> "refactor.rewrite"
55
module TextDocument = struct
66
type t = {version : string option; uri : string}
77

8+
let make ~version ~uri = {version; uri}
9+
810
let toString t =
911
Printf.sprintf {|{"version": %s, "uri": "%s"}|}
1012
(match t.version with
@@ -16,6 +18,8 @@ end
1618
module Position = struct
1719
type t = {line : int; character : int}
1820

21+
let make ~line ~character = {line; character}
22+
1923
let toString t =
2024
Printf.sprintf {|{"line": %s, "character": %s}|} (string_of_int t.line)
2125
(string_of_int t.character)
@@ -24,6 +28,8 @@ end
2428
module TextEditRange = struct
2529
type t = {start : Position.t; end_ : Position.t}
2630

31+
let make ~start ~end_ = {start; end_}
32+
2733
let toString t =
2834
Printf.sprintf {|{"start": %s, "end": %s}|}
2935
(t.start |> Position.toString)
@@ -33,34 +39,55 @@ end
3339
module TextEdit = struct
3440
type t = {newText : string; range : TextEditRange.t}
3541

42+
let make ~newText ~range = {newText; range}
43+
3644
let toString t =
37-
Printf.sprintf {|{"newText": %s, "range": %s}|} t.newText
45+
Printf.sprintf {|{"newText": "%s", "range": %s}|} (Json.escape t.newText)
3846
(t.range |> TextEditRange.toString)
3947
end
4048

4149
module DocumentChange = struct
4250
type t = {textDocument : TextDocument.t; edits : TextEdit.t list}
4351

52+
let make ~textDocument ~edits = {textDocument; edits}
53+
4454
let toString t =
4555
Printf.sprintf {|{"textDocument": %s, "edits": [%s]}|}
4656
(t.textDocument |> TextDocument.toString)
47-
""
57+
(t.edits
58+
|> List.map (fun edit -> edit |> TextEdit.toString)
59+
|> String.concat ",")
4860
end
4961

5062
module CodeActionEdit = struct
5163
type t = {documentChanges : DocumentChange.t list}
5264

53-
let toString t = Printf.sprintf {|{"documentChanges": [%s]}|} ""
65+
let make ~documentChanges = {documentChanges}
66+
67+
let toString t =
68+
Printf.sprintf {|{"documentChanges": [%s]}|}
69+
(t.documentChanges
70+
|> List.map (fun documentChange ->
71+
documentChange |> DocumentChange.toString)
72+
|> String.concat ",")
5473
end
5574

5675
module CodeAction = struct
5776
type t = {title : string; kind : kind; edit : CodeActionEdit.t}
5877

78+
let make ~title ~kind ~edit = {title; kind; edit}
79+
5980
let toString t =
60-
Printf.sprintf {|{"title": %s, "kind": "%s", "edit": %s}|} t.title
81+
Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} t.title
6182
(kindToString t.kind)
6283
(t.edit |> CodeActionEdit.toString)
6384
end
6485

6586
(* This is the return that's expected when resolving code actions *)
66-
type result = CodeAction.t list
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 ",")

analysis/src/Commands.ml

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,42 @@ let codeAction ~path ~line ~col =
116116
match locItem.SharedTypes.locType with
117117
| Typed (n, t, _) -> (
118118
match Printtyp.tree_of_typexp false t with
119-
| Otyp_constr (Oide_ident "option", _) -> Protocol.stringifyCodeAction {range = Utils.cmtLocToRange locItem.loc; newText = "switch " ^ n ^ " { | None => failWith(\"TODO\") | Some(" ^ n ^ ") => _" ^ n ^ " }"}
120-
| _ -> Protocol.null
121-
)
122-
| Constant _constant ->
123-
Protocol.stringifyHover {contents = "constant"}
119+
| Otyp_constr (Oide_ident "option", _) ->
120+
let range = Utils.cmtLocToRange locItem.loc in
121+
CodeActions.(
122+
stringifyCodeActions
123+
[
124+
CodeAction.make ~title:"Unwrap optional"
125+
~kind:RefactorRewrite
126+
~edit:
127+
(CodeActionEdit.make
128+
~documentChanges:
129+
[
130+
DocumentChange.make
131+
~textDocument:
132+
(TextDocument.make ~version:None ~uri:path)
133+
~edits:
134+
[
135+
TextEdit.make
136+
~newText:
137+
("switch " ^ n
138+
^ " { | None => failWith(\"TODO\") | \
139+
Some(" ^ n ^ ") => _" ^ n ^ " }")
140+
~range:
141+
(TextEditRange.make
142+
~start:
143+
(Position.make
144+
~line:range.start.line
145+
~character:
146+
range.start.character)
147+
~end_:
148+
(Position.make
149+
~line:range.end_.line
150+
~character:range.end_.character));
151+
];
152+
]);
153+
])
154+
| _ -> Protocol.null)
124155
| _ -> Protocol.null))
125156
in
126157
print_endline result

server/src/server.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -438,43 +438,15 @@ function codeAction(msg: p.RequestMessage): p.ResponseMessage {
438438
],
439439
msg
440440
);
441-
let result: null | {
442-
content: string;
443-
range: {
444-
start: { line: number; character: number };
445-
end: { line: number; character: number };
446-
};
447-
} = response.result as any;
441+
442+
let { result } = response;
448443

449444
let res: v.ResponseMessage = {
450445
jsonrpc: c.jsonrpcVersion,
451446
id: msg.id,
452447
};
453448

454-
if (result == null) {
455-
res.result = null;
456-
return res;
457-
}
458-
459-
let textEdit: v.TextEdit = { newText: result.content, range: result.range };
460-
461-
let codeAction: v.CodeAction = {
462-
title: "Unwrap optional",
463-
kind: v.CodeActionKind.RefactorRewrite,
464-
edit: {
465-
documentChanges: [
466-
{
467-
textDocument: {
468-
version: null,
469-
uri: params.textDocument.uri,
470-
},
471-
edits: [textEdit],
472-
},
473-
],
474-
},
475-
};
476-
477-
res.result = [codeAction];
449+
res.result = result ?? null;
478450
return res;
479451
}
480452

0 commit comments

Comments
 (0)