-
Notifications
You must be signed in to change notification settings - Fork 455
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
PoC: emit code actions from the compiler that the editor tooling can use #7040
Draft
zth
wants to merge
6
commits into
master
Choose a base branch
from
code-actions-driven-by-compiler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e91a63b
PoC: emit code actions from the compiler that the editor tooling can use
zth 46d0330
small cleanup
zth c89af81
its an array
zth 8415e75
automatic code action for spellcheck 'did you mean' text
zth edacb5b
add 'wrap in Some()' code action
zth 03dcd2f
add tests for editor enhancements mode
zth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fixtures/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Special tests for the editor tooling enhancements mode. | ||
|
||
Follow CONTRIBUTING.md and build the project, then run `node ./jscomp/build_tests/editor_tooling_enhancements/input.js` at the root of the project to check the tests against previous snapshots. | ||
|
||
Run `node ./jscomp/build_tests/editor_tooling_enhancements/input.js update` to update the snapshots. |
16 changes: 16 additions & 0 deletions
16
jscomp/build_tests/editor_tooling_enhancements/expected/replace_name.res.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/replace_name.res[0m:[2m8:3-6[0m | ||
|
||
6 [2m│[0m let x: x = { | ||
7 [2m│[0m name: "hello", | ||
[1;31m8[0m [2m│[0m [1;31magee[0m: 10, | ||
9 [2m│[0m } | ||
10 [2m│[0m | ||
|
||
The field [1;31magee[0m does not belong to type [1;33mx[0m | ||
|
||
This record expression is expected to have type [1;33mx[0m | ||
Hint: Did you mean age? | ||
=== CODE ACTIONS === | ||
[{"title": "Replace with `agee`", "kind": "quickfix" "loc": {"start": {"line": 8, "col": 74},"end": {"line": 8, "col": 78}}, "type": "replaceWith", "replaceWith": "agee"}] |
16 changes: 16 additions & 0 deletions
16
jscomp/build_tests/editor_tooling_enhancements/expected/wrap_option_in_some.res.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/wrap_option_in_some.res[0m:[2m2:3[0m | ||
|
||
1 [2m│[0m switch Some(1) { | ||
[1;31m2[0m [2m│[0m | [1;31m1[0m => () | ||
3 [2m│[0m | _ => () | ||
4 [2m│[0m } | ||
|
||
This pattern matches values of type [1;31mint[0m | ||
but a pattern was expected which matches values of type [1;33moption<int>[0m | ||
|
||
The value you're pattern matching on here is wrapped in an [1;33moption[0m, but you're trying to match on the actual value. | ||
Wrap the highlighted pattern in [1;33mSome()[0m to make it work. | ||
=== CODE ACTIONS === | ||
[{"title": "Wrap in `Some()`", "kind": "quickfix" "loc": {"start": {"line": 2, "col": 19},"end": {"line": 2, "col": 20}}, "type": "wrapWith", "wrapLeft": "Some(", "wrapRight": ")"}] |
9 changes: 9 additions & 0 deletions
9
jscomp/build_tests/editor_tooling_enhancements/fixtures/replace_name.res
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type x = { | ||
name: string, | ||
age: int, | ||
} | ||
|
||
let x: x = { | ||
name: "hello", | ||
agee: 10, | ||
} |
4 changes: 4 additions & 0 deletions
4
jscomp/build_tests/editor_tooling_enhancements/fixtures/wrap_option_in_some.res
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
switch Some(1) { | ||
| 1 => () | ||
| _ => () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const child_process = require("child_process"); | ||
|
||
const { bsc_exe: bsc } = require("#cli/bin_path"); | ||
|
||
const expectedDir = path.join(__dirname, "expected"); | ||
|
||
const fixtures = fs | ||
.readdirSync(path.join(__dirname, "fixtures")) | ||
.filter((fileName) => path.extname(fileName) === ".res"); | ||
|
||
// const runtime = path.join(__dirname, '..', '..', 'runtime') | ||
const prefix = `${bsc} -w +A -code-action-data`; | ||
|
||
const updateTests = process.argv[2] === "update"; | ||
|
||
function postProcessErrorOutput(output) { | ||
output = output.trimRight(); | ||
output = output.replace( | ||
/\/[^ ]+?jscomp\/build_tests\/editor_tooling_enhancements\//g, | ||
"/.../" | ||
); | ||
return output; | ||
} | ||
|
||
let doneTasksCount = 0; | ||
let atLeastOneTaskFailed = false; | ||
|
||
fixtures.forEach((fileName) => { | ||
const fullFilePath = path.join(__dirname, "fixtures", fileName); | ||
const command = `${prefix} -color always ${fullFilePath}`; | ||
console.log(`running ${command}`); | ||
child_process.exec(command, (err, stdout, stderr) => { | ||
doneTasksCount++; | ||
// careful of: | ||
// - warning test that actually succeeded in compiling (warning's still in stderr, so the code path is shared here) | ||
// - accidentally succeeding tests (not likely in this context), | ||
// actual, correctly erroring test case | ||
const actualErrorOutput = postProcessErrorOutput(stderr.toString()); | ||
const expectedFilePath = path.join(expectedDir, fileName + ".expected"); | ||
if (updateTests) { | ||
fs.writeFileSync(expectedFilePath, actualErrorOutput); | ||
} else { | ||
const expectedErrorOutput = postProcessErrorOutput( | ||
fs.readFileSync(expectedFilePath, { encoding: "utf-8" }) | ||
); | ||
if (expectedErrorOutput !== actualErrorOutput) { | ||
console.error( | ||
`The old and new error output for the test ${fullFilePath} aren't the same` | ||
); | ||
console.error("\n=== Old:"); | ||
console.error(expectedErrorOutput); | ||
console.error("\n=== New:"); | ||
console.error(actualErrorOutput); | ||
atLeastOneTaskFailed = true; | ||
} | ||
|
||
if (doneTasksCount === fixtures.length && atLeastOneTaskFailed) { | ||
process.exit(1); | ||
} | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,3 +112,5 @@ val as_pp : bool ref | |
val self_stack : string Stack.t | ||
|
||
val modules : bool ref | ||
|
||
val code_action_data : bool ref |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
type code_action_type = WrapWith of {left: string; right: string} | ReplaceWith of string | ||
type code_action_style = Regular | QuickFix | ||
type code_action = { | ||
style: code_action_style; | ||
type_: code_action_type; | ||
title: string; | ||
} | ||
|
||
let code_actions_enabled = ref true | ||
|
||
let code_action_data = ref [] | ||
let add_code_action (data : code_action) = | ||
code_action_data := data :: !code_action_data | ||
let get_code_action_data () = !code_action_data | ||
|
||
let escape text = | ||
let ln = String.length text in | ||
let buf = Buffer.create ln in | ||
let rec loop i = | ||
if i < ln then ( | ||
(match text.[i] with | ||
| '\012' -> Buffer.add_string buf "\\f" | ||
| '\\' -> Buffer.add_string buf "\\\\" | ||
| '"' -> Buffer.add_string buf "\\\"" | ||
| '\n' -> Buffer.add_string buf "\\n" | ||
| '\b' -> Buffer.add_string buf "\\b" | ||
| '\r' -> Buffer.add_string buf "\\r" | ||
| '\t' -> Buffer.add_string buf "\\t" | ||
| c -> Buffer.add_char buf c); | ||
loop (i + 1)) | ||
in | ||
loop 0; | ||
Buffer.contents buf | ||
|
||
let loc_to_json loc = | ||
Printf.sprintf | ||
"{\"start\": {\"line\": %s, \"col\": %s},\"end\": {\"line\": %s, \"col\": \ | ||
%s}}" | ||
(loc.Location.loc_start.pos_lnum |> string_of_int) | ||
(loc.loc_start.pos_cnum |> string_of_int) | ||
(loc.loc_end.pos_lnum |> string_of_int) | ||
(loc.loc_end.pos_cnum |> string_of_int) | ||
|
||
let code_action_type_to_json = function | ||
| WrapWith {left; right} -> | ||
Printf.sprintf "\"type\": \"wrapWith\", \"wrapLeft\": \"%s\", \"wrapRight\": \"%s\"" | ||
(escape left) (escape right) | ||
| ReplaceWith text -> | ||
Printf.sprintf "\"type\": \"replaceWith\", \"replaceWith\": \"%s\"" | ||
(escape text) | ||
|
||
let emit_code_actions_data loc ppf = | ||
match !code_action_data with | ||
| [] -> () | ||
| code_actions -> | ||
Format.fprintf ppf "@\n=== CODE ACTIONS ===@\n["; | ||
Format.fprintf ppf "%s" | ||
(code_actions | ||
|> List.map (fun data -> | ||
Format.sprintf | ||
"{\"title\": \"%s\", \"kind\": \"%s\" \"loc\": %s, %s}" | ||
(escape data.title) | ||
(match data.style with | ||
| Regular -> "regular" | ||
| QuickFix -> "quickfix") | ||
(loc_to_json loc) | ||
(code_action_type_to_json data.type_)) | ||
|> String.concat ","); | ||
Format.fprintf ppf "]" | ||
|
||
|
||
module Actions = struct | ||
let add_replace_with name = | ||
if !code_actions_enabled then | ||
add_code_action | ||
{ | ||
style = QuickFix; | ||
type_ = ReplaceWith name; | ||
title = "Replace with `" ^ name ^ "`"; | ||
} | ||
let add_wrap_in_constructor name = | ||
if !code_actions_enabled then | ||
add_code_action | ||
{ | ||
style = QuickFix; | ||
type_ = WrapWith {left = name ^ "("; right = ")"}; | ||
title = "Wrap in `" ^ name ^ "()`"; | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
type code_action_type = WrapWith of {left: string; right: string} | ReplaceWith of string | ||
type code_action_style = Regular | QuickFix | ||
type code_action = { | ||
style: code_action_style; | ||
type_: code_action_type; | ||
title: string; | ||
} | ||
|
||
val add_code_action: code_action -> unit | ||
val get_code_action_data: unit -> code_action list | ||
|
||
val emit_code_actions_data: Location.t -> Format.formatter -> unit | ||
|
||
module Actions : sig | ||
val add_replace_with: string -> unit | ||
val add_wrap_in_constructor: string -> unit | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't actually used yet, but we can probably have a
bsc
config like this.