Skip to content

feat(codeactions): open Implementation/Interface/Compiled and create Interface #767

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#### :rocket: New Feature

- Add support for the rewatch build system for incremental compilation. https://github.com/rescript-lang/rescript-vscode/pull/965
- Code Actions: open Implementation/Interface/Compiled Js and create Interface file. https://github.com/rescript-lang/rescript-vscode/pull/767

## 1.50.0

Expand Down
13 changes: 2 additions & 11 deletions analysis/src/CodeActions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,5 @@ let stringifyCodeActions codeActions =
Printf.sprintf {|%s|}
(codeActions |> List.map Protocol.stringifyCodeAction |> Protocol.array)

let make ~title ~kind ~uri ~newText ~range =
let uri = uri |> Uri.fromPath |> Uri.toString in
{
Protocol.title;
codeActionKind = kind;
edit =
{
documentChanges =
[{textDocument = {version = None; uri}; edits = [{newText; range}]}];
};
}
let make ~title ~kind ~edit ~command =
{Protocol.title; codeActionKind = kind; edit; command}
28 changes: 16 additions & 12 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -433,18 +433,22 @@ let test ~path =
in
Sys.remove currentFile;
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)))
|> List.iter (fun {Protocol.title; edit} ->
match edit with
| Some {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))
| None -> ())
| "c-a" ->
let hint = String.sub rest 3 (String.length rest - 3) in
print_endline
Expand Down
1 change: 1 addition & 0 deletions analysis/src/Hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ let codeLens ~path ~debug =
single line in the editor. *)
title =
typeExpr |> Shared.typeToString ~lineWidth:400;
arguments = None;
};
})
| _ -> None)
Expand Down
45 changes: 31 additions & 14 deletions analysis/src/Protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type range = {start: position; end_: position}
type markupContent = {kind: string; value: string}

(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#command *)
type command = {title: string; command: string}
type command = {title: string; command: string; arguments: string list option}

(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens *)
type codeLens = {range: range; command: command option}
Expand Down Expand Up @@ -76,12 +76,13 @@ type textDocumentEdit = {
}

type codeActionEdit = {documentChanges: textDocumentEdit list}
type codeActionKind = RefactorRewrite
type codeActionKind = RefactorRewrite | Empty

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

let null = "null"
Expand Down Expand Up @@ -224,7 +225,7 @@ let stringifyoptionalVersionedTextDocumentIdentifier td =
| Some v -> string_of_int v)
(Json.escape td.uri)

let stringifyTextDocumentEdit tde =
let stringifyTextDocumentEdit (tde : textDocumentEdit) =
Printf.sprintf {|{
"textDocument": %s,
"edits": %s
Expand All @@ -235,15 +236,37 @@ let stringifyTextDocumentEdit tde =
let codeActionKindToString kind =
match kind with
| RefactorRewrite -> "refactor.rewrite"
| Empty -> ""

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)
let stringifyCommand (command : command) =
stringifyObject
[
("title", Some (wrapInQuotes command.title));
("command", Some (wrapInQuotes command.command));
( "arguments",
match command.arguments with
| None -> None
| Some args -> Some (args |> List.map wrapInQuotes |> array) );
]

let stringifyCodeAction (ca : codeAction) =
stringifyObject
[
("title", Some (wrapInQuotes ca.title));
("kind", Some (wrapInQuotes (codeActionKindToString ca.codeActionKind)));
( "edit",
match ca.edit with
| None -> None
| Some edit -> Some (edit |> stringifyCodeActionEdit) );
( "command",
match ca.command with
| None -> None
| Some command -> Some (command |> stringifyCommand) );
]

let stringifyHint hint =
Printf.sprintf
Expand All @@ -256,12 +279,6 @@ let stringifyHint hint =
}|}
(stringifyPosition hint.position)
(Json.escape hint.label) hint.kind hint.paddingLeft hint.paddingRight

let stringifyCommand (command : command) =
Printf.sprintf {|{"title": "%s", "command": "%s"}|}
(Json.escape command.title)
(Json.escape command.command)

let stringifyCodeLens (codeLens : codeLens) =
Printf.sprintf
{|{
Expand Down
Loading
Loading