Skip to content
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

Add new destruct-line code action #1283

Merged
merged 9 commits into from
May 20, 2024

Conversation

awilliambauer
Copy link
Contributor

Merlin-destruct is made more useful by adding a new LSP code-action called destruct-line which does some pre- and post-processing surrounding a call to Merlin-destruct.

This new code-action can be invoked on a line where the user has typed match... or match...with, or within an incomplete match statement on a line with an existing case. When invoked, the code-action will fill in the remaining cases of the match. This means that in VSCode, if you have one of the following lines, you can hit C-. from anywhere on the line to achieve the corresponding expansion.

Supposing x is a bool, then the line

match x

expands to

match x with
| false -> _
| true -> _

Supposing get_card returns a card_value variant, then the line

match get_card (int_of_string s) with

expands to

match get_card (int_of_string s) with
| Ace -> _
| King -> _
| Queen -> _
| Jack -> _
| Number _ -> _

Or supposing you've already filled in

match get_card (int_of_string s) with
| King -> "Face Card"
| Queen -> "Face Card"

then destruct-line on either the King or Queen line will produce

match get_card (int_of_string s) with
| King -> "Face Card"
| Queen -> "Face Card"
| Ace -> _
| Jack -> _
| Number _ -> _

In addition, the code action detects a sub-case of the | x -> y form, where the cursor is on an underscore within x.

The following expansions result from repeated applications of destruct-line:

let zip (type a b) (xs : a list) (ys : b list) : (a * b) list =
  match xs, ys

After code action is invoked anywhere on the match line:

let zip (type a b) (xs : a list) (ys : b list) : (a * b) list =
  match (xs, ys) with
  | (_, _) -> _

After code action is invoked on the first underscore:

let zip (type a b) (xs : a list) (ys : b list) : (a * b) list =
  match (xs, ys) with
  | ([], _) -> _
  | (_::_, _) -> _

After code action is invoked on the first underscore:

let zip (type a b) (xs : a list) (ys : b list) : (a * b) list =
  match (xs, ys) with
  | ([], []) -> _
  | ([], _::_) -> _
  | (_::_, _) -> _

After code action is invoked on the second-to-last underscore:

let zip (type a b) (xs : a list) (ys : b list) : (a * b) list =
  match (xs, ys) with
  | ([], []) -> _
  | ([], _::_) -> _
  | (_::_, []) -> _
  | (_::_, _::_) -> _

@rgrinberg
Copy link
Member

LGTM, but I'd like to avoid Re2. Are you in favor of replacing it with ocaml-re?

@rgrinberg
Copy link
Member

Alright, I managed to get rid of re2 in favor of re. core was unused so I just got rid of it. base can stay and we should feel free to use it to replace stdune.

@rgrinberg rgrinberg added this to the 1.18.0 milestone May 18, 2024
awilliambauer and others added 8 commits May 17, 2024 18:34
_
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
_
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
_
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
_
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
@coveralls
Copy link

Pull Request Test Coverage Report for Build 4263

Details

  • 100 of 115 (86.96%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 21.496%

Changes Missing Coverage Covered Lines Changed/Added Lines %
ocaml-lsp-server/src/code_actions/action_destruct_line.ml 98 113 86.73%
Totals Coverage Status
Change from base Build 4260: 0.3%
Covered Lines: 5246
Relevant Lines: 24405

💛 - Coveralls

@rgrinberg rgrinberg merged commit ec05b1a into ocaml:master May 20, 2024
9 checks passed
voodoos added a commit to voodoos/opam-repository that referenced this pull request Jul 5, 2024
CHANGES:

## Features

- Introduce a configuration option to control dune diagnostics. The option is
  called `duneDiganostics` and it may be set to `{ enable: false }` to disable
  diagnostics. (ocaml/ocaml-lsp#1221)

- Support folding of `ifthenelse` expressions (ocaml/ocaml-lsp#1031)

- Improve hover behavior (ocaml/ocaml-lsp#1245)

  Hovers are no longer displaye on useless parsetree nodes such as keywords,
  comments, etc.

  Multiline hovers are now filtered away.

  Display expanded ppx's in the hover window.

- Improve document symbols (ocaml/ocaml-lsp#1247)

  Use the parse tree instead of the typed tree. This means that document
  symbols will work even if the source code doesn't type check.

  Include symbols at arbitrary depth.

  Differentiate functions / types / variants / etc.

  This now includes PPXs like `let%expect_test` or `let%bench` in the outline.

- Introduce a `destruct-line` code action. This is an improved version of the
  old `destruct` code action. (ocaml/ocaml-lsp#1283)

- Improve signature inference to only include types for elements that were
  absent from the signature. Previously, all signature items would always be
  inserted. (ocaml/ocaml-lsp#1289)

- Add an `update-signature` code action to update the types of elements that
  were already present in the signature (ocaml/ocaml-lsp#1289)

- Add custom
  [`ocamllsp/merlinCallCompatible`](https://github.com/ocaml/ocaml-lsp/blob/e165f6a3962c356adc7364b9ca71788e93489dd0/ocaml-lsp-server/docs/ocamllsp/merlinCallCompatible-spec.md)
  request (ocaml/ocaml-lsp#1265)

- Add custom [`ocamllsp/typeEnclosing`](https://github.com/ocaml/ocaml-lsp/blob/109801e56f2060caf4487427bede28b824f4f1fe/ocaml-lsp-server/docs/ocamllsp/typeEnclosing-spec.md) request (ocaml/ocaml-lsp#1304)

## Fixes

- Detect document kind by looking at merlin's `suffixes` config.

  This enables more lsp features for non-.ml/.mli files. Though it still
  depends on merlin's support. (ocaml/ocaml-lsp#1237)

- Correctly accept the `--clientProcessId` flag. (ocaml/ocaml-lsp#1242)

- Disable automatic completion and signature help inside comments (ocaml/ocaml-lsp#1246)

- Includes a new optional/configurable option to toggle syntax documentation. If
  toggled on, allows display of syntax documentation on hover tooltips. Can be
  controlled via environment variables and by GUI for VS code. (ocaml/ocaml-lsp#1218)

- For completions on labels that the LSP gets from merlin, take into account
  whether the prefix being completed starts with `~` or `?`. Change the label
  completions that start with `?` to start with `~` when the prefix being
  completed starts with `~`. (ocaml/ocaml-lsp#1277)

- Fix document syncing (ocaml/ocaml-lsp#1278, ocaml/ocaml-lsp#1280, fixes ocaml/ocaml-lsp#1207)

- Stop generating inlay hints on generated code (ocaml/ocaml-lsp#1290)

- Fix parenthesizing of function types in `SignatureHelp` (ocaml/ocaml-lsp#1296)

- Fix syntax documentation rendering (ocaml/ocaml-lsp#1318)
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.

None yet

3 participants