Skip to content

Commit a89a87a

Browse files
ahungrybbatsov
authored andcommitted
Improve the cider-selector handling of some edge cases (#2711)
This commit adjusts cider-selector handling with the following benefits: - If the selector is run outside of an active sesman-session, it will still find the REPL when the REPL selector is chosen (it will fallback to the most recently visited REPL). - If the selector is run while a window is visible, it will switch to that window (focus) as opposed to giving a "No such buffer" message.
1 parent b2449ec commit a89a87a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Changes
6+
7+
* `cider-selector` has more robust handling for edge cases.
8+
59
### Bugs fixed
610

711
* [#2715](https://github.com/clojure-emacs/cider/issues/2715): Fix the `shadow-cljs` presence check.

cider-selector.el

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,23 @@ DESCRIPTION is a one-line description of what the key selects.")
4949
Not meant to be set by users. It's used internally
5050
by `cider-selector'.")
5151

52-
(defun cider-selector--recently-visited-buffer (mode)
52+
(defun cider-selector--recently-visited-buffer (mode &optional consider-visible-p)
5353
"Return the most recently visited buffer, deriving its `major-mode' from MODE.
54-
Only considers buffers that are not already visible."
54+
CONSIDER-VISIBLE-P will allow handling of visible windows as well.
55+
First pass only considers buffers that are not already visible.
56+
Second pass will attempt one of visible ones for scenarios where the window
57+
is visible, but not focused."
5558
(cl-loop for buffer in (buffer-list)
5659
when (and (with-current-buffer buffer
5760
(derived-mode-p mode))
5861
;; names starting with space are considered hidden by Emacs
5962
(not (string-match-p "^ " (buffer-name buffer)))
60-
(null (get-buffer-window buffer 'visible)))
63+
(or consider-visible-p
64+
(null (get-buffer-window buffer 'visible))))
6165
return buffer
62-
finally (error "Can't find unshown buffer in %S" mode)))
66+
finally (if consider-visible-p
67+
(error "Can't find unshown buffer in %S" mode)
68+
(cider-selector--recently-visited-buffer mode t))))
6369

6470
;;;###autoload
6571
(defun cider-selector (&optional other-window)
@@ -97,7 +103,7 @@ is chosen. The returned buffer is selected with
97103
`switch-to-buffer'."
98104
(let ((method `(lambda ()
99105
(let ((buffer (progn ,@body)))
100-
(cond ((not (get-buffer buffer))
106+
(cond ((not (and buffer (get-buffer buffer)))
101107
(message "No such buffer: %S" buffer)
102108
(ding))
103109
((get-buffer-window buffer)
@@ -138,8 +144,10 @@ is chosen. The returned buffer is selected with
138144
(top-level))
139145

140146
(def-cider-selector-method ?r
141-
"Current REPL buffer."
142-
(cider-current-repl))
147+
"Current REPL buffer or as a fallback, the most recently
148+
visited cider-repl-mode buffer."
149+
(or (cider-current-repl)
150+
(cider-selector--recently-visited-buffer 'cider-repl-mode)))
143151

144152
(def-cider-selector-method ?m
145153
"Current connection's *nrepl-messages* buffer."

0 commit comments

Comments
 (0)