Skip to content

Code actions #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f75321c
crude, hacky version of code actions
zth Mar 23, 2022
32d944a
start mapping out the response structure from analysis bin
zth Mar 23, 2022
caa47c3
prepare code actions in analysis bin
zth Mar 23, 2022
fbd710e
move most things to Protocol
zth Mar 24, 2022
dcbd04e
extract potential code actions from diagnostics where appropriate, an…
zth Mar 25, 2022
8d6c9ed
Test action if-then-else to switch.
cristianoc Mar 24, 2022
91a33ae
Print to a string, not standard output.
cristianoc Mar 25, 2022
7b81173
Variants and records.
cristianoc Mar 25, 2022
621fd9d
Trigger on the body of if-then-else.
cristianoc Mar 25, 2022
933aacf
tweak test
cristianoc Mar 25, 2022
f68c7db
Rename/cleanup.
cristianoc Mar 26, 2022
86a632b
refactor
cristianoc Mar 26, 2022
9a34ab4
Use simple diff to output xform result.
cristianoc Mar 26, 2022
d3cf9cd
wire up code actions with the analysis bin
zth Mar 27, 2022
217ceee
Use local pretty-printing of single expression.
cristianoc Mar 28, 2022
4f3eb50
Make code actions work on unsaved files.
cristianoc Mar 29, 2022
f5f8125
dead code
cristianoc Mar 29, 2022
119d403
Move types to protocol.
cristianoc Mar 29, 2022
fedb5f0
Refactor to allow more code actions later.
cristianoc Mar 29, 2022
f4edc44
More generic test printing of code actions.
cristianoc Mar 29, 2022
04d9639
Use iterator instead of mapper.
cristianoc Mar 29, 2022
8edb551
Keep information on whether a value definition is type annotated.
cristianoc Mar 30, 2022
ab90b5e
First version of action to add type annotation.
cristianoc Mar 30, 2022
2a2773d
Only send type annotation.
cristianoc Mar 30, 2022
a51d5c7
mark code actions from diagnostics as quick fixes
zth Mar 30, 2022
042d358
no leading space in generated type annotations
zth Mar 30, 2022
6e2f55f
Revert "Keep information on whether a value definition is type annota…
cristianoc Mar 31, 2022
e0fb87f
Use parser to determine if a type annotation is already present.
cristianoc Mar 31, 2022
1b3ba18
Only add type annotations to let-bound variables, and function argume…
cristianoc Mar 31, 2022
1daa84a
Add example of "~foo as bar".
cristianoc Mar 31, 2022
b626dbd
code action for applying uncurried function call with dot
zth Mar 31, 2022
e828904
Rebase on master.
cristianoc Mar 31, 2022
fac9736
Don't add type annotation to a react component.
cristianoc Mar 31, 2022
a1437bd
Type annotations: work around location of labels including "~".
cristianoc Mar 31, 2022
fa0cb2c
add code action for adding braces to function without braces
zth Apr 4, 2022
b5f0393
Make debug print for actions less confusing.
cristianoc Apr 5, 2022
063c7d0
Tweak action for adding braces to function body.
cristianoc Apr 6, 2022
47c9f50
fix bug with wrapping single character ranges in quick fix code actions
zth Apr 6, 2022
382bb95
code action for inserting simple missing variant cases
zth Apr 6, 2022
cd73ff9
code action for wrapping a value of type t with Some where something …
zth Apr 6, 2022
983bdd7
add code action for unwrapping optionals
zth Apr 6, 2022
d9a761b
explanations for the current diagnostics driven code actions
zth Apr 6, 2022
b63a187
remove top level unit type action, since the error location given by …
zth Apr 6, 2022
e26d79d
ensure code actions blowing up won't kill the server
zth Apr 8, 2022
39a38b8
Merge branch 'master' into code-actions
zth Apr 8, 2022
060cb90
tweaking the add matches action. more robust implementation for wrapp…
zth Apr 11, 2022
7933283
Merge branch 'master' into code-actions
cristianoc Apr 23, 2022
e381574
clean up positions
cristianoc Apr 23, 2022
00b8e0b
Merge branch 'master' into code-actions
cristianoc Apr 26, 2022
b08b1ee
Merge branch 'master' into code-actions
cristianoc Apr 27, 2022
1701eb6
Merge branch 'master' into code-actions
cristianoc Apr 28, 2022
fde2ebb
Merge branch 'master' into code-actions
cristianoc Apr 28, 2022
b839d3f
Merge branch 'master' into code-actions
cristianoc Apr 28, 2022
48c83f2
Merge branch 'master' into code-actions
cristianoc Apr 29, 2022
4cc459c
Merge branch 'master' into code-actions
cristianoc Apr 29, 2022
77c5b29
simplify insert missing cases even more
zth Apr 29, 2022
0ea313c
cleanup
zth Apr 29, 2022
accc270
dead code
cristianoc Apr 30, 2022
b2d3387
refactor
cristianoc Apr 30, 2022
1d4a85c
cleanup
cristianoc Apr 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions analysis/src/Cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ let main () =
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
| [_; "hover"; path; line; col] ->
Commands.hover ~path ~line:(int_of_string line) ~col:(int_of_string col)
| [_; "codeAction"; path; line; col; currentFile] ->
Commands.codeAction ~path ~line:(int_of_string line)
~col:(int_of_string col) ~currentFile
| [_; "references"; path; line; col] ->
Commands.references ~path ~line:(int_of_string line)
~col:(int_of_string col)
Expand Down
17 changes: 17 additions & 0 deletions analysis/src/CodeActions.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(* This is the return that's expected when resolving code actions *)
type result = Protocol.codeAction list

let stringifyCodeActions codeActions =
Printf.sprintf {|%s|}
(codeActions |> List.map Protocol.stringifyCodeAction |> Protocol.array)

let make ~title ~kind ~uri ~newText ~range =
{
Protocol.title;
codeActionKind = kind;
edit =
{
documentChanges =
[{textDocument = {version = None; uri}; edits = [{newText; range}]}];
};
}
24 changes: 24 additions & 0 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ let hover ~path ~line ~col =
in
print_endline result

let codeAction ~path ~line ~col ~currentFile =
Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile
|> CodeActions.stringifyCodeActions |> print_endline

let definition ~path ~line ~col =
let locationOpt =
match Cmt.fromPath ~path with
Expand Down Expand Up @@ -325,6 +329,26 @@ let test ~path =
dir ++ parent_dir_name ++ "lib" ++ "bs" ++ "src" ++ name
in
Printf.printf "%s" (CreateInterface.command ~path ~cmiFile)
| "xfm" ->
print_endline
("Xform " ^ path ^ " " ^ string_of_int line ^ ":"
^ string_of_int col);
let codeActions =
Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile:path
in
codeActions
|> List.iter (fun {Protocol.title; edit = {documentChanges}} ->
Printf.printf "Hit: %s\n" title;
documentChanges
|> List.iter (fun {Protocol.edits} ->
edits
|> List.iter (fun {Protocol.range; newText} ->
let indent =
String.make range.start.character ' '
in
Printf.printf "%s\nnewText:\n%s<--here\n%s%s\n"
(Protocol.stringifyRange range)
indent indent newText)))
| _ -> ());
print_newline ())
in
Expand Down
28 changes: 21 additions & 7 deletions analysis/src/Protocol.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
type position = {line : int; character : int}

type range = {start : position; end_ : position}

type markupContent = {kind : string; value : string}

type completionItem = {
Expand All @@ -13,13 +11,9 @@ type completionItem = {
}

type hover = {contents : string}

type location = {uri : string; range : range}

type documentSymbolItem = {name : string; kind : int; location : location}

type renameFile = {oldUri : string; newUri : string}

type textEdit = {range : range; newText : string}

type optionalVersionedTextDocumentIdentifier = {
Expand All @@ -32,8 +26,16 @@ type textDocumentEdit = {
edits : textEdit list;
}

let null = "null"
type codeActionEdit = {documentChanges : textDocumentEdit list}
type codeActionKind = RefactorRewrite

type codeAction = {
title : string;
codeActionKind : codeActionKind;
edit : codeActionEdit;
}

let null = "null"
let array l = "[" ^ String.concat ", " l ^ "]"

let stringifyPosition p =
Expand Down Expand Up @@ -110,3 +112,15 @@ let stringifyTextDocumentEdit tde =
}|}
(stringifyoptionalVersionedTextDocumentIdentifier tde.textDocument)
(tde.edits |> List.map stringifyTextEdit |> array)

let codeActionKindToString kind =
match kind with RefactorRewrite -> "refactor.rewrite"

let stringifyCodeActionEdit cae =
Printf.sprintf {|{"documentChanges": %s}|}
(cae.documentChanges |> List.map stringifyTextDocumentEdit |> array)

let stringifyCodeAction ca =
Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title
(codeActionKindToString ca.codeActionKind)
(ca.edit |> stringifyCodeActionEdit)
Loading