Skip to content

Commit 746e4d6

Browse files
vspinubbatsov
authored andcommitted
Make cider-current-connection retrieve the most recently used connection
1 parent 79b9d64 commit 746e4d6

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Make it possible to disable auto-evaluation of changed ns forms via the defcustom `cider-auto-track-ns-form-changes`.
2929
* [#1991](https://github.com/clojure-emacs/cider/issues/1832): Make it possible to disable the prompt to open a ClojureScript in a browser on connect via `cider-offer-to-open-cljs-app-in-browser`.
3030
* [#1995](https://github.com/clojure-emacs/cider/pull/1995): Add new customization variable `cider-doc-auto-select-buffer` to control cider-doc popup buffer auto selection.
31+
* Ensure that `cider-current-connection` picks the most recently used connection in ambiguous cases.
3132
* Ensure that `cider-switch-to-repl-buffer` picks the most recent repl buffer if multiple connections are available.
3233
* Add new function `cider-project-connections-types`.
3334

cider-client.el

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ evaluation commands to use a particular connection."
253253
A REPL is relevant if its `nrepl-project-dir' is compatible with the
254254
current directory (see `cider-find-connection-buffer-for-project-directory').
255255
256+
When there are multiple relevant connections of the same TYPE, return the
257+
most recently used one.
258+
256259
If TYPE is provided, it is either \"clj\" or \"cljs\", and only a
257260
connection of that type is returned. If no connections of that TYPE exist,
258261
return nil.
@@ -263,32 +266,38 @@ returned. In this case, only return nil if there are no active connections
263266
at all."
264267
;; If TYPE was specified, we only return that type (or nil). OW, we prefer
265268
;; that TYPE, but ultimately allow any type.
266-
(cl-labels ((right-type-p
267-
(c)
268-
(when (or (not type)
269-
(and (buffer-live-p c)
270-
(with-current-buffer c (equal cider-repl-type type))))
271-
c)))
269+
(cl-labels ((right-type-p (c type)
270+
(when (or (not type)
271+
(and (buffer-live-p c)
272+
(equal (cider--connection-type c) type)))
273+
c))
274+
(most-recent-buf (connections type)
275+
(when connections
276+
(seq-find (lambda (c)
277+
(and (member c connections)
278+
(right-type-p c type)))
279+
(buffer-list)))))
272280
(let ((connections (cider-connections)))
273281
(cond
274282
((not connections) nil)
275283
;; if you're in a REPL buffer, it's the connection buffer
276-
((and (derived-mode-p 'cider-repl-mode) (right-type-p (current-buffer))))
284+
((and (derived-mode-p 'cider-repl-mode) (right-type-p (current-buffer) type)))
277285
((eq cider-request-dispatch 'static) (car connections))
278-
((= 1 (length connections)) (right-type-p (car connections)))
286+
((= 1 (length connections)) (right-type-p (car connections) type))
279287
(t (let ((project-connections (cider-find-connection-buffer-for-project-directory
280288
nil :all-connections))
281289
(guessed-type (or type (cider-connection-type-for-buffer))))
282-
;; So we have multiple connections. Look for the connection type we
283-
;; want, prioritizing the current project.
284-
(or (seq-find (lambda (conn)
285-
(equal (cider--connection-type conn) guessed-type))
286-
project-connections)
287-
(seq-find (lambda (conn)
288-
(equal (cider--connection-type conn) guessed-type))
289-
connections)
290-
(right-type-p (car project-connections))
291-
(right-type-p (car connections)))))))))
290+
(or
291+
;; cljc
292+
(and (equal guessed-type "multi")
293+
(most-recent-buf project-connections nil))
294+
;; clj or cljs
295+
(and guessed-type
296+
(or (most-recent-buf project-connections guessed-type)
297+
(most-recent-buf connections guessed-type)))
298+
;; when type was not specified or guessed
299+
(most-recent-buf project-connections type)
300+
(most-recent-buf connections type))))))))
292301

293302
(defun cider-other-connection (&optional connection)
294303
"Return the first connection of another type than CONNECTION.

test/cider-client-tests.el

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ SYMBOL is locally let-bound to the current buffer."
8888
(setq major-mode 'clojurescript-mode)
8989
(expect (cider-current-connection) :to-equal b2)))))))
9090

91+
(it "always returns the most recently used connection"
92+
(with-connection-buffer "clj" bb1
93+
(with-connection-buffer "cljs" bb2
94+
(with-connection-buffer "clj" b1
95+
(with-connection-buffer "cljs" b2
96+
97+
(switch-to-buffer bb2)
98+
(switch-to-buffer bb1)
99+
(expect (cider-current-connection) :to-equal bb1)
100+
101+
;; follows type arguments
102+
(expect (cider-current-connection "clj") :to-equal bb1)
103+
(expect (cider-current-connection "cljs") :to-equal bb2)
104+
105+
;; follows file type
106+
(with-temp-buffer
107+
(setq major-mode 'clojure-mode)
108+
(expect (cider-current-connection) :to-equal bb1))
109+
110+
(with-temp-buffer
111+
(setq major-mode 'clojurescript-mode)
112+
(expect (cider-current-connection) :to-equal bb2)))))))
113+
91114
(describe "when type argument is given"
92115
(describe "when connection of that type exists"
93116
(it "returns that connection buffer"
@@ -288,13 +311,15 @@ SYMBOL is locally let-bound to the current buffer."
288311
(with-temp-buffer
289312
(setq-local nrepl-endpoint '("localhost" 4005))
290313
(setq-local nrepl-project-dir "proj")
314+
(setq-local cider-repl-type "clj")
291315
(expect (cider--connection-info (current-buffer))
292316
:to-equal "CLJ proj@localhost:4005 (Java 1.7, Clojure 1.7.0, nREPL 0.2.1)"))))
293317

294318
(describe "when current project is not known"
295319
(it "returns information about the connection buffer without project name"
296320
(with-temp-buffer
297321
(setq-local nrepl-endpoint '("localhost" 4005))
322+
(setq-local cider-repl-type "clj")
298323
(expect (cider--connection-info (current-buffer))
299324
:to-equal "CLJ <no project>@localhost:4005 (Java 1.7, Clojure 1.7.0, nREPL 0.2.1)")))))
300325

@@ -333,9 +358,9 @@ SYMBOL is locally let-bound to the current buffer."
333358
(setq cider-repl-type "cljs")
334359
(expect (cider-connection-type-for-buffer) :to-equal "cljs"))
335360

336-
(it "returns clj as its default value"
361+
(it "returns nil as its default value"
337362
(setq cider-repl-type nil)
338-
(expect (cider-connection-type-for-buffer) :to-equal "clj")))
363+
(expect (cider-connection-type-for-buffer) :to-equal nil)))
339364

340365

341366
(describe "cider-nrepl-send-unhandled-request"
@@ -372,12 +397,16 @@ SYMBOL is locally let-bound to the current buffer."
372397

373398

374399
(describe "cider-extract-designation-from-current-repl-buffer"
400+
375401
(describe "when the buffers have a designation"
376402
(it "returns that designation string"
377403
(with-temp-buffer
378-
(let* ((cider-connections (list (current-buffer))))
404+
(let ((cider-connections (list (current-buffer)))
405+
(nrepl-repl-buffer-name-template "*cider-repl%s*"))
379406
(rename-buffer "*cider-repl bob*")
407+
(switch-to-buffer (current-buffer))
380408
(with-temp-buffer
409+
(switch-to-buffer (current-buffer))
381410
(expect (cider-extract-designation-from-current-repl-buffer)
382411
:to-equal "bob")
383412
(rename-buffer "*cider-repl apa*")
@@ -402,6 +431,7 @@ SYMBOL is locally let-bound to the current buffer."
402431
(expect (cider-extract-designation-from-current-repl-buffer)
403432
:to-equal "<no designation>")))))))))
404433

434+
405435
(describe "cider-project-name"
406436
(it "returns the project name extracted from the project dir"
407437
(expect (cider-project-name nil) :to-equal "-")

0 commit comments

Comments
 (0)