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

Code actions #373

merged 60 commits into from
Apr 30, 2022

Conversation

zth
Copy link
Collaborator

@zth zth commented Mar 27, 2022

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.

add-undefined-record-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.

insert-missing-variant-cases

(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.

simple-conversions

Unwrap optional values

We put an optional value somewhere which expects a non-optional. Add a switch to unwrap the optional value.

unwrap-optional

Wrap optional value

We put a non-optional value somewhere that expects an optional. Wrap the non-optional value in Some().

wrap-optional-value-in-some

Apply curried function call

We call a curried function, but forget to apply it with a dot.

apply-curried-fn

Apply "hint" for misspelled identifiers

We misspell an identifier, and the compiler suggests a fix.

apply-hint

Refactors

Add type annotation

Automatically add type annotations to let bindings and function parameters.

add-type-anotation-2

add-typpe-annotation

Add braces to function body

add-braces-to-function

Convert if/else and ternary to switch

Rewrite a simple (one case) if/else, or a ternary to a switch.

if-ternary-to-switch

Closes #312

@cristianoc cristianoc force-pushed the code-actions branch 2 times, most recently from 335c0ee to 575ac58 Compare March 31, 2022 13:02
zth and others added 28 commits April 7, 2022 09:11
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.
Does not need changes to the type processing.
cristianoc and others added 10 commits April 7, 2022 09:16
- 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.
…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)
@zth zth merged commit 9d92049 into master Apr 30, 2022
@zth zth deleted the code-actions branch April 30, 2022 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Code action to convert misstyped name to actual name
2 participants