Skip to content

Commit a17409b

Browse files
committed
move most things to Protocol
1 parent 4b75a02 commit a17409b

File tree

3 files changed

+40
-122
lines changed

3 files changed

+40
-122
lines changed

analysis/src/CodeActions.ml

Lines changed: 26 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,36 @@
1+
open Protocol
2+
13
type kind = RefactorRewrite
24

35
let kindToString kind = match kind with RefactorRewrite -> "refactor.rewrite"
46

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}
278

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)
4813

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
6616

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)
7419

7520
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+
}
8436
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 ",")

analysis/src/Commands.ml

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,13 @@ let codeAction ~path ~line ~col =
121121
CodeActions.(
122122
stringifyCodeActions
123123
[
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-
]);
124+
CodeAction.makeRangeReplace ~title:"Unwrap optional"
125+
~kind:RefactorRewrite ~file:path
126+
~newText:
127+
("switch " ^ n
128+
^ " { | None => failWith(\"TODO\") | Some(" ^ n
129+
^ ") => _" ^ n ^ " }")
130+
~range;
153131
])
154132
| _ -> Protocol.null)
155133
| _ -> Protocol.null))

analysis/src/Protocol.ml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
type position = {line : int; character : int}
2-
32
type range = {start : position; end_ : position}
4-
53
type markupContent = {kind : string; value : string}
64

75
type completionItem = {
@@ -13,15 +11,10 @@ type completionItem = {
1311
}
1412

1513
type hover = {contents : string}
16-
1714
type location = {uri : string; range : range}
18-
1915
type documentSymbolItem = {name : string; kind : int; location : location}
20-
2116
type codeAction = {newText : string; range : range}
22-
2317
type renameFile = {oldUri : string; newUri : string}
24-
2518
type textEdit = {range : range; newText : string}
2619

2720
type optionalVersionedTextDocumentIdentifier = {
@@ -34,8 +27,9 @@ type textDocumentEdit = {
3427
edits : textEdit list;
3528
}
3629

37-
let null = "null"
30+
type codeActionEdit = {documentChanges : textDocumentEdit list}
3831

32+
let null = "null"
3933
let array l = "[" ^ String.concat ", " l ^ "]"
4034

4135
let stringifyPosition p =
@@ -74,8 +68,7 @@ let stringifyCodeAction c =
7468
"content": "%s",
7569
"range": %s
7670
}|}
77-
(Json.escape c.newText)
78-
(stringifyRange c.range)
71+
(Json.escape c.newText) (stringifyRange c.range)
7972

8073
let stringifyLocation (h : location) =
8174
Printf.sprintf {|{"uri": "%s", "range": %s}|} (Json.escape h.uri)
@@ -121,3 +114,7 @@ let stringifyTextDocumentEdit tde =
121114
}|}
122115
(stringifyoptionalVersionedTextDocumentIdentifier tde.textDocument)
123116
(tde.edits |> List.map stringifyTextEdit |> array)
117+
118+
let stringifyCodeActionEdit cae =
119+
Printf.sprintf {|{"documentChanges": %s}|}
120+
(cae.documentChanges |> List.map stringifyTextDocumentEdit |> array)

0 commit comments

Comments
 (0)