Skip to content

Add interactive command for case splitting #112

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions lsp-haskell.el
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ if projectile way fails"
(user-error "Couldn't find cabal file, using: %s" dir)
dir))))


;; ---------------------------------------------------------------------
;; Interactive commands

(defun lsp-haskell-case-split ()
"Case split on an identifier.
To use this, place make sure the point is over a type hole."
(interactive)
(let* ((case-split-regex "Case split on \\(.*\\)")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels a little fragile. I think the right way to do this is to use code action kinds: they're explicitly there to allow filtering by the client (i.e. us). We might need to teach the tactics plugin to add a kind, though.

Copy link
Author

Choose a reason for hiding this comment

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

I can update the tactics plugin with kinds, good call!

(actions (-filter (lambda (action) (s-matches-p case-split-regex (lsp:code-action-title action))) (lsp-code-actions-at-point))))
(lsp-execute-code-action (cond ((seq-empty-p actions) (signal 'lsp-no-code-actions nil))
((and (eq (seq-length actions) 1) lsp-auto-execute-action)
(lsp-seq-first actions))
(t (lsp--completing-read "Identifier: "
(seq-into actions 'list)
(lambda (action)
(cadr (s-match case-split-regex (lsp:code-action-title action))))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, you do rather rely on the regex here. I guess I don't have a better suggestion, then, since AFAICT there's no proper way to include some extra structured data in a CodeAction...

I guess we could avoid the regex by just using the code action title as is, for something like:

Pick a case-splitting action:
1. Case split on x
2. Case split on y

but I guess that's a worse UX.

Copy link
Author

Choose a reason for hiding this comment

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

If it is too fragile, then we can remove it! Shame that the spec doesn't let us communicate more in the kind.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I just want to avoid the situation where someone changes the title, rightly reasoning that it's a user-facing string that can be changed freely, and then this function breaks.

nil t))))))

;; ---------------------------------------------------------------------
;; Starting the server and registration with lsp-mode

Expand Down