Skip to content

Commit 604ffac

Browse files
authored
Offer the Member in class: prompt to be easily skipped under ido-mode (#3501)
1 parent 47d9a95 commit 604ffac

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- Always match friendly sessions for `cider-ancillary-buffers` (like `*cider-error*`, `*cider-result*`, etc).
5959
- `cider-test`: only show diffs for collections.
6060
- `cider-inspector-def-current-val` now can suggest a var name (default none), which can be customized via `cider-inspector-preferred-var-names`.
61+
- The "Member in class: " prompt can now be optionally skipped in ido-mode by pressing `<up>` or `<down>` ([doc](https://docs.cider.mx/cider/usage/code_completion.html)).
6162
- [#3375](https://github.com/clojure-emacs/cider/pull/3375): `cider-test`: don't render a newline between expected and actual, most times.
6263
- Interactive evaluation: show a shorter overlay when rendering compilation errors.
6364
- e.g., the `Syntax error compiling clojure.core/let at (foo/bar.clj:10:1)` prefix is now removed.

cider-client.el

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,38 @@ itself is present."
474474
(with-current-buffer (cider-current-repl)
475475
nrepl-tooling-session))
476476

477+
(declare-function ido-exit-minibuffer "ido" t)
478+
479+
;; Not used anywhere, except in documentation as a suggestion for users.
480+
(defmacro cider--with-temporary-ido-keys (UP DOWN &rest body)
481+
"Temporarily define UP, DOWN keys for ido and execute BODY.
482+
483+
This makes the UX for auto-completion more streamlined,
484+
since one often wants to go to the next candidate (DOWN key)
485+
without having to specify a Java class for the current candidate
486+
\(because the current candidate may be irrelevant to the user)."
487+
`(if (bound-and-true-p ido-common-completion-map)
488+
(let ((original-up-binding (lookup-key ido-common-completion-map (kbd ,UP)))
489+
(original-down-binding (lookup-key ido-common-completion-map (kbd ,DOWN))))
490+
(define-key ido-common-completion-map (kbd ,UP) (lambda ()
491+
(interactive)
492+
(ido-exit-minibuffer)))
493+
(define-key ido-common-completion-map (kbd ,DOWN) (lambda ()
494+
(interactive)
495+
(ido-exit-minibuffer)))
496+
(unwind-protect
497+
(progn ,@body)
498+
(define-key ido-common-completion-map (kbd ,UP) original-up-binding)
499+
(define-key ido-common-completion-map (kbd ,DOWN) original-down-binding)))
500+
,@body))
501+
502+
(defun cider-class-choice-completing-read (prompt candidates)
503+
"A completing read that can be customized with the `advice' mechanism,
504+
forwarding PROMPT and CANDIDATES as-is.
505+
506+
See also: `cider--with-temporary-ido-keys'."
507+
(completing-read prompt candidates))
508+
477509
(defun cider--var-choice (var-info)
478510
"Prompt to choose from among multiple VAR-INFO candidates, if required.
479511
This is needed only when the symbol queried is an unqualified host platform
@@ -482,7 +514,7 @@ contain a `candidates' key, it is returned as is."
482514
(let ((candidates (nrepl-dict-get var-info "candidates")))
483515
(if candidates
484516
(let* ((classes (nrepl-dict-keys candidates))
485-
(choice (completing-read "Member in class: " classes nil t))
517+
(choice (cider-class-choice-completing-read "Member in class: " classes))
486518
(info (nrepl-dict-get candidates choice)))
487519
info)
488520
var-info)))

doc/modules/ROOT/pages/usage/code_completion.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ image::completion-annotations.png[Completion Annotations]
150150
TIP: Completion annotations can be disabled by setting
151151
`cider-annotate-completion-candidates` to `nil`.
152152

153+
=== Notes on class disambiguation
154+
155+
Sometimes, the completion user experience may be interrupted by a `completing-read`
156+
that asks for the `Member in class`. This is used for better Java completions and documentation.
157+
158+
However, if you are not interested in the current candidate, disambiguating it is of no use,
159+
and the prompt can be a nuisance.
160+
161+
If you are using Company for completions and IDO for `completing-read`, you can cause the `<up>` and `<down>`
162+
keys to cancel the prompt by customizing:
163+
164+
[source,lisp]
165+
----
166+
(advice-add 'cider-class-choice-completing-read
167+
:around
168+
(lambda (f a b)
169+
(cider--with-temporary-ido-keys "<up>" "<down>"
170+
(funcall f a b))))
171+
----
172+
153173
=== Changing the completion style
154174

155175
Sometimes the user may want to use a different completion style just for the CIDER

0 commit comments

Comments
 (0)