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

Handle ocaml-eglot-destruct (as a main command) #18

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,28 @@ document and interactively substitute them:

![Construct Example](media/construct.gif)

If the `ocaml-eglot-construct` command is prefixed by an argument, ie:
`C-u M-x ocaml-eglot-construct`, the command will also search for
valid candidates in the current environment:
If the `ocaml-eglot-construct` (<kbd>C-c</kbd> <kbd>\</kbd>) command
is prefixed by an argument, ie: `C-u M-x ocaml-eglot-construct`, the
command will also search for valid candidates in the current
environment:

![Construct with prefix-arg Example](media/construct2.gif)

### Destruct (or case-anlysis)

Destruct, `ocaml-eglot-destruct` (<kbd>C-c</kbd> <kbd>|</kbd>) is a
powerful feature that allows one to generate and manipulate pattern
matching expressions. It behaves differently depending on the cursor’s
context:

- on an expression: it replaces it by a pattern matching over it’s
constructors
- on a wildcard pattern: it will refine it if possible
- on a pattern of a non-exhaustive matching: it will make the pattern
matching exhaustive by adding missing cases

![Destruct Example](media/destruct.gif)

### Source Browsing

OCaml-eglot allows you to navigate semantically in a buffer, passing
Expand Down
Binary file added media/destruct.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions ocaml-eglot-req.el
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,14 @@ VERBOSITY is a potential verbosity index."
(let ((params (ocaml-eglot-req--TypeEnclosingParams at index verbosity)))
(ocaml-eglot-req--send :ocamllsp/typeEnclosing params)))

(defun ocaml-eglot-req--call-code-action (action-kind)
"Call ACTION-KIND promptly."
(eglot-code-actions nil nil action-kind t))

(defun ocaml-eglot-req--destruct ()
"Call code-action `destruct' for a given position."
(let ((action-kind "destruct (enumerate cases)"))
(ocaml-eglot-req--call-code-action action-kind)))

(provide 'ocaml-eglot-req)
;;; ocaml-eglot-req.el ends here
12 changes: 10 additions & 2 deletions ocaml-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ of result (LIMIT)."
It use the ARG to use local values or not."
(interactive "P")
(eglot--server-capable-or-lose :experimental :ocamllsp :handleConstruct)
(let* ((_with-local-values (ocaml-eglot--construct-local-values arg))
(current-range (ocaml-eglot-util--current-range))
(let* ((current-range (ocaml-eglot-util--current-range))
(start (cl-getf current-range :start))
(end (cl-getf current-range :end))
(hole (ocaml-eglot--get-first-hole-in start end)))
Expand Down Expand Up @@ -458,6 +457,13 @@ If called repeatedly, increase the verbosity of the type shown."
(interactive)
(ocaml-eglot-type-enclosing--call))

;; Case Analysis

(defun ocaml-eglot-destruct ()
"Perform case-analysis at the current point."
(interactive)
(ocaml-eglot-req--destruct))

;;; Mode

(defvar ocaml-eglot-map
Expand All @@ -469,6 +475,8 @@ If called repeatedly, increase the verbosity of the type shown."
(define-key ocaml-eglot-keymap (kbd "C-c C-a") #'ocaml-eglot-alternate-file)
(define-key ocaml-eglot-keymap (kbd "C-c C-d") #'ocaml-eglot-document)
(define-key ocaml-eglot-keymap (kbd "C-c C-t") #'ocaml-eglot-type-enclosing)
(define-key ocaml-eglot-keymap (kbd "C-c |") #'ocaml-eglot-destruct)
(define-key ocaml-eglot-keymap (kbd "C-c \\") #'ocaml-eglot-construct)
ocaml-eglot-keymap)
"Keymap for OCaml-eglot minor mode.")

Expand Down
Loading