Skip to content

Commit 9d92049

Browse files
authored
Merge pull request #373 from rescript-lang/code-actions
Code actions
2 parents f38c043 + 1d4a85c commit 9d92049

File tree

12 files changed

+1369
-12
lines changed

12 files changed

+1369
-12
lines changed

analysis/src/Cli.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ let main () =
8080
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
8181
| [_; "hover"; path; line; col] ->
8282
Commands.hover ~path ~line:(int_of_string line) ~col:(int_of_string col)
83+
| [_; "codeAction"; path; line; col; currentFile] ->
84+
Commands.codeAction ~path ~line:(int_of_string line)
85+
~col:(int_of_string col) ~currentFile
8386
| [_; "references"; path; line; col] ->
8487
Commands.references ~path ~line:(int_of_string line)
8588
~col:(int_of_string col)

analysis/src/CodeActions.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(* This is the return that's expected when resolving code actions *)
2+
type result = Protocol.codeAction list
3+
4+
let stringifyCodeActions codeActions =
5+
Printf.sprintf {|%s|}
6+
(codeActions |> List.map Protocol.stringifyCodeAction |> Protocol.array)
7+
8+
let make ~title ~kind ~uri ~newText ~range =
9+
{
10+
Protocol.title;
11+
codeActionKind = kind;
12+
edit =
13+
{
14+
documentChanges =
15+
[{textDocument = {version = None; uri}; edits = [{newText; range}]}];
16+
};
17+
}

analysis/src/Commands.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ let hover ~path ~line ~col =
6363
in
6464
print_endline result
6565

66+
let codeAction ~path ~line ~col ~currentFile =
67+
Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile
68+
|> CodeActions.stringifyCodeActions |> print_endline
69+
6670
let definition ~path ~line ~col =
6771
let locationOpt =
6872
match Cmt.fromPath ~path with
@@ -325,6 +329,26 @@ let test ~path =
325329
dir ++ parent_dir_name ++ "lib" ++ "bs" ++ "src" ++ name
326330
in
327331
Printf.printf "%s" (CreateInterface.command ~path ~cmiFile)
332+
| "xfm" ->
333+
print_endline
334+
("Xform " ^ path ^ " " ^ string_of_int line ^ ":"
335+
^ string_of_int col);
336+
let codeActions =
337+
Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile:path
338+
in
339+
codeActions
340+
|> List.iter (fun {Protocol.title; edit = {documentChanges}} ->
341+
Printf.printf "Hit: %s\n" title;
342+
documentChanges
343+
|> List.iter (fun {Protocol.edits} ->
344+
edits
345+
|> List.iter (fun {Protocol.range; newText} ->
346+
let indent =
347+
String.make range.start.character ' '
348+
in
349+
Printf.printf "%s\nnewText:\n%s<--here\n%s%s\n"
350+
(Protocol.stringifyRange range)
351+
indent indent newText)))
328352
| _ -> ());
329353
print_newline ())
330354
in

analysis/src/Protocol.ml

Lines changed: 21 additions & 7 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,13 +11,9 @@ 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 renameFile = {oldUri : string; newUri : string}
22-
2317
type textEdit = {range : range; newText : string}
2418

2519
type optionalVersionedTextDocumentIdentifier = {
@@ -32,8 +26,16 @@ type textDocumentEdit = {
3226
edits : textEdit list;
3327
}
3428

35-
let null = "null"
29+
type codeActionEdit = {documentChanges : textDocumentEdit list}
30+
type codeActionKind = RefactorRewrite
3631

32+
type codeAction = {
33+
title : string;
34+
codeActionKind : codeActionKind;
35+
edit : codeActionEdit;
36+
}
37+
38+
let null = "null"
3739
let array l = "[" ^ String.concat ", " l ^ "]"
3840

3941
let stringifyPosition p =
@@ -110,3 +112,15 @@ let stringifyTextDocumentEdit tde =
110112
}|}
111113
(stringifyoptionalVersionedTextDocumentIdentifier tde.textDocument)
112114
(tde.edits |> List.map stringifyTextEdit |> array)
115+
116+
let codeActionKindToString kind =
117+
match kind with RefactorRewrite -> "refactor.rewrite"
118+
119+
let stringifyCodeActionEdit cae =
120+
Printf.sprintf {|{"documentChanges": %s}|}
121+
(cae.documentChanges |> List.map stringifyTextDocumentEdit |> array)
122+
123+
let stringifyCodeAction ca =
124+
Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title
125+
(codeActionKindToString ca.codeActionKind)
126+
(ca.edit |> stringifyCodeActionEdit)

0 commit comments

Comments
 (0)