-
Notifications
You must be signed in to change notification settings - Fork 60
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
Code actions #373
Conversation
This file contains hidden or 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
335c0ee
to
575ac58
Compare
…d add a first batch of code actions
If a value declaration (such as a let or a case in a switch) is not type annotated already, add the option to add the type annotation.
…ted." This reverts commit 1f5df25.
Does not need changes to the type processing.
- Only fire on the body, not on the argument. - On `(x,y,z) => e` check that we're on `z => e` and not `x` or `y` by checking that `e` is not a function. - Use location trick on braces so expression is always on a new line. - Start pretty printing from latest structure item (typically the let defining the function), so the local pretty printing looks decent.
…expects option<t>
…the compiler isn't always enough to add ignore (we'd have to look at the AST, and we'd like to avoid that for the diagnostics driven actions)
…ing/unwrapping optional values
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This implements basic infrastructure for code actions (refactors, quick fixes, etc).
Code actions are divided into 2 categories - 1) actions driven by compiler errors/warnings (quick fixes), and 2) actions driven by reading the current context and suggesting various refactors.
For 1), we read the actual warning/error message from the compiler, and put together a quick fix code action that can be used to fix the warning/error. This is done in TS, inside of the language server, as part of parsing the compiler log (which is turned into the editor diagnostics).
For 2), we leverage our OCaml analysis binary, which is capable of using both the real ReScript AST, as well as type information from the compiler.
It implements the following initial code actions:
Provided code actions
Quick fixes
Add undefined record fields
We build a record value, but miss defining fields.
Insert missing pattern match cases
We switch on something, but we don't provide all the possible cases. The compiler tell us what cases are missing.
(Currently works with single polyvariant/variant cases, with 0 or 1 payload)
Simple conversions (int to string, etc)
We try to put an int where a string is supposed to be, or a float that's supposed to be an int, etc. The compiler suggests how to convert to the desired format.
Unwrap optional values
We put an optional value somewhere which expects a non-optional. Add a switch to unwrap the optional value.
Wrap optional value
We put a non-optional value somewhere that expects an optional. Wrap the non-optional value in
Some()
.Apply curried function call
We call a curried function, but forget to apply it with a dot.
Apply "hint" for misspelled identifiers
We misspell an identifier, and the compiler suggests a fix.
Refactors
Add type annotation
Automatically add type annotations to let bindings and function parameters.
Add braces to function body
Convert if/else and ternary to switch
Rewrite a simple (one case) if/else, or a ternary to a switch.
Closes #312