Skip to content

Add code action combine cases - #1514

Merged
xvw merged 3 commits into
ocaml:masterfrom
xvw:add-code-action-combine-cases
Apr 16, 2025
Merged

xvw merged 3 commits into
ocaml:masterfrom
xvw:add-code-action-combine-cases

Conversation

@xvw

@xvw xvw commented Apr 8, 2025 •

Copy link
Copy Markdown
Member

The combine-cases code action allows the user to select several one-line cases of a match and combine them into a single one-line case. If there is a unique non-empty right-hand-side expression it will be preserved; otherwise the RHS will be a hole.

For example, if the user highlights the King and Queen lines of the following snippet and invokes combine-cases:

match card with
| Ace -> _
| King -> _
| Queen -> "Face card!"
| Jack -> "Face card?"
| Number _ -> _

then the code action will update the snippet to:

match card with
| Ace -> _
| King | Queen -> "Face card!"
| Jack -> "Face card?"
| Number _ -> _

If instead (or afterwards) they invoke combine-cases highlighting the King, Queen, and Jack lines, they'll get:

match card with
| Ace -> _
| King | Queen | Jack -> _
| Number _ -> _

The intended use-case is immediately following a destruct-line code action, which produces lots of empty cases on separate lines; this allows several related cases to be quickly combined.

Combine-cases avoids invoking Merlin and instead just works by regular expression matching to detect the "|" and "->" tokens. This means it doesn't handle multi-line cases and doesn't do anything smart when combining cases.

(cc @awilliambauer)

@xvw
xvw requested a review from voodoos April 8, 2025 21:41
@coveralls

coveralls commented Apr 8, 2025 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 4807

Details

  • 37 of 40 (92.5%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 23.08%

Changes Missing Coverage Covered Lines Changed/Added Lines %
ocaml-lsp-server/src/code_actions/action_combine_cases.ml 37 40 92.5%
Totals Coverage Status
Change from base Build 4786: 0.1%
Covered Lines: 5909
Relevant Lines: 25602

💛 - Coveralls

@voodoos voodoos left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I wonder what happens performance wise if a large number of lines from the buffer is selected, but it's unlikely to cause real trouble, especially since this has been battle-tested at JS.

@xvw
xvw merged commit 056032b into ocaml:master Apr 16, 2025
@xvw
xvw deleted the add-code-action-combine-cases branch April 16, 2025 19:44
voodoos pushed a commit to voodoos/ocaml-lsp that referenced this pull request Jun 6, 2025
* Add `combine_cases` code Action

* Add tests for `combine-cases`

* Add CHANGE entry
voodoos added a commit to voodoos/opam-repository that referenced this pull request Jun 23, 2025
CHANGES:

## Features

- Make `inlay-hint` for function parameters configurable (ocaml/ocaml-lsp#1515)

- Add custom `ocamllsp/jumpToTypedHole` to navigate through typed holes (ocaml/ocaml-lsp#1516)

- Add a code-action for combining pattern cases (just relaying on regex) (ocaml/ocaml-lsp#1514)

- Allow (by configuration) shortening of diagnostics (just highlighting the first line) (ocaml/ocaml-lsp#1513)

- Fix `yojson_of_t` for `Nullable_option`: serialize `None` as `Null` instead of asserting false (ocaml/ocaml-lsp#1525 fixes ocaml/ocaml-lsp#1524)

## Fixes

- Support for `class`, `class type`, `method` and `property` for `DocumentSymbol` query (ocaml/ocaml-lsp#1487 fixes ocaml/ocaml-lsp#1449)

- Fix `inlay-hint` for function parameters (ocaml/ocaml-lsp#1515)

- More precise diagnostics in the event of a failed identifier search (`Definition_query`) (ocaml/ocaml-lsp#1518)

- Remove `ocamlformat` application after `destruct` (that remove some useful parenthesis) (ocaml/ocaml-lsp#1519)

- Add a new server option `standardHover`, that can be used by clients to
  disable the default hover provider.  When `standardHover = false`
  `textDocument/hover` requests always returns with empty result. (ocaml/ocaml-lsp#1416)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Jun 23, 2025
CHANGES:

## Features

- Make `inlay-hint` for function parameters configurable (ocaml/ocaml-lsp#1515)

- Add custom `ocamllsp/jumpToTypedHole` to navigate through typed holes (ocaml/ocaml-lsp#1516)

- Add a code-action for combining pattern cases (just relaying on regex) (ocaml/ocaml-lsp#1514)

- Allow (by configuration) shortening of diagnostics (just highlighting the first line) (ocaml/ocaml-lsp#1513)

- Fix `yojson_of_t` for `Nullable_option`: serialize `None` as `Null` instead of asserting false (ocaml/ocaml-lsp#1525 fixes ocaml/ocaml-lsp#1524)

## Fixes

- Support for `class`, `class type`, `method` and `property` for `DocumentSymbol` query (ocaml/ocaml-lsp#1487 fixes ocaml/ocaml-lsp#1449)

- Fix `inlay-hint` for function parameters (ocaml/ocaml-lsp#1515)

- More precise diagnostics in the event of a failed identifier search (`Definition_query`) (ocaml/ocaml-lsp#1518)

- Remove `ocamlformat` application after `destruct` (that remove some useful parenthesis) (ocaml/ocaml-lsp#1519)

- Add a new server option `standardHover`, that can be used by clients to
  disable the default hover provider.  When `standardHover = false`
  `textDocument/hover` requests always returns with empty result. (ocaml/ocaml-lsp#1416)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Jun 24, 2025
CHANGES:

## Features

- Make `inlay-hint` for function parameters configurable (ocaml/ocaml-lsp#1515)
- Add custom `ocamllsp/jumpToTypedHole` to navigate through typed holes (ocaml/ocaml-lsp#1516)
- Add a code-action for combining pattern cases (just relaying on regex) (ocaml/ocaml-lsp#1514)
- Allow (by configuration) shortening of diagnostics (just highlighting the first line) (ocaml/ocaml-lsp#1513)
- Fix `yojson_of_t` for `Nullable_option`: serialize `None` as `Null` instead of asserting false (ocaml/ocaml-lsp#1525 fixes ocaml/ocaml-lsp#1524)

## Fixes

- Support for `class`, `class type`, `method` and `property` for `DocumentSymbol` query (ocaml/ocaml-lsp#1487 fixes ocaml/ocaml-lsp#1449)
- Fix `inlay-hint` for function parameters (ocaml/ocaml-lsp#1515)
- More precise diagnostics in the event of a failed identifier search (`Definition_query`) (ocaml/ocaml-lsp#1518)
- Remove `ocamlformat` application after `destruct` (that remove some useful parenthesis) (ocaml/ocaml-lsp#1519)
voodoos added a commit to voodoos/opam-repository that referenced this pull request Jun 24, 2025
CHANGES:

## Features

- Make `inlay-hint` for function parameters configurable (ocaml/ocaml-lsp#1515)
- Add custom `ocamllsp/jumpToTypedHole` to navigate through typed holes (ocaml/ocaml-lsp#1516)
- Add a code-action for combining pattern cases (just relaying on regex) (ocaml/ocaml-lsp#1514)
- Allow (by configuration) shortening of diagnostics (just highlighting the first line) (ocaml/ocaml-lsp#1513)
- Fix `yojson_of_t` for `Nullable_option`: serialize `None` as `Null` instead of asserting false (ocaml/ocaml-lsp#1525 fixes ocaml/ocaml-lsp#1524)

## Fixes

- Support for `class`, `class type`, `method` and `property` for `DocumentSymbol` query (ocaml/ocaml-lsp#1487 fixes ocaml/ocaml-lsp#1449)
- Fix `inlay-hint` for function parameters (ocaml/ocaml-lsp#1515)
- More precise diagnostics in the event of a failed identifier search (`Definition_query`) (ocaml/ocaml-lsp#1518)
- Remove `ocamlformat` application after `destruct` (that remove some useful parenthesis) (ocaml/ocaml-lsp#1519)
davesnx pushed a commit to davesnx/ocaml-lsp that referenced this pull request Nov 26, 2025
* Add `combine_cases` code Action

* Add tests for `combine-cases`

* Add CHANGE entry
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.

3 participants