Skip to content

Commit 41b4608

Browse files
committed
Merge pull request #494 from jonpither/rotate-no-connection
[Fix #493] Rotate nREPL connection handles no nREPL connection.
2 parents fbd92d4 + 66a67b0 commit 41b4608

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ visually distinctive from `cider-repl-output-face` (used for STDOUT output).
2828

2929
### Bugs fixed
3030

31+
* [#493](https://github.com/clojure-emacs/cider/issues/493) Fix rotate connection to handle no
32+
nREPL connection.
3133
* [#468](https://github.com/clojure-emacs/cider/issues/468) Fix
3234
pretty-printing of evaluation results so that `*1` is set properly.
3335
* [#439](https://github.com/clojure-emacs/cider/issues/439) Fix

cider-interaction.el

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ endpoint and Clojure version."
137137
(defun cider-rotate-connection ()
138138
"Rotate and display the current nREPL connection."
139139
(interactive)
140+
(cider-ensure-connected)
140141
(setq nrepl-connection-list
141142
(append (cdr nrepl-connection-list)
142143
(list (car nrepl-connection-list))))
@@ -181,14 +182,13 @@ the buffer should appear.
181182
With a prefix ARG sets the namespace in the REPL buffer to that
182183
of the namespace in the Clojure source buffer."
183184
(interactive "p")
184-
(if (not (cider-connected-p))
185-
(message "No active nREPL connection.")
186-
(let ((buffer (current-buffer)))
187-
(when (eq 4 arg)
188-
(cider-repl-set-ns (cider-current-ns)))
189-
(pop-to-buffer (cider-find-or-create-repl-buffer))
190-
(cider-remember-clojure-buffer buffer)
191-
(goto-char (point-max)))))
185+
(cider-ensure-connected)
186+
(let ((buffer (current-buffer)))
187+
(when (eq 4 arg)
188+
(cider-repl-set-ns (cider-current-ns)))
189+
(pop-to-buffer (cider-find-or-create-repl-buffer))
190+
(cider-remember-clojure-buffer buffer)
191+
(goto-char (point-max))))
192192

193193
(defun cider-find-connection-buffer-for-project-directory (project-directory)
194194
"Find the relevant connection-buffer for the given PROJECT-DIRECTORY.
@@ -234,31 +234,29 @@ of the namespace in the Clojure source buffer.
234234
With a second prefix ARG the chosen REPL buffer is based on a
235235
supplied project directory using IDO."
236236
(interactive "p")
237-
(if (not (cider-connected-p))
238-
(message "No active nREPL connection.")
239-
240-
(let* ((project-directory
241-
(or (when (eq 16 arg) (ido-read-directory-name "Project: "))
242-
(nrepl-project-directory-for (nrepl-current-dir))))
243-
(connection-buffer
244-
(or
245-
(and (= 1 (length nrepl-connection-list)) (car nrepl-connection-list))
246-
(and project-directory
247-
(cider-find-connection-buffer-for-project-directory project-directory)))))
248-
(when connection-buffer
249-
(setq nrepl-connection-list
250-
(cons connection-buffer (delq connection-buffer nrepl-connection-list))))
251-
(cider-switch-to-current-repl-buffer arg)
252-
(message
253-
(format (if connection-buffer
254-
"Switched to REPL: %s"
255-
"Could not determine relevant nREPL connection, using: %s")
256-
(with-current-buffer (nrepl-current-connection-buffer)
257-
(format "%s:%s, %s:%s"
258-
(or (nrepl--project-name nrepl-project-dir) "<no project>")
259-
nrepl-buffer-ns
260-
(car nrepl-endpoint)
261-
(cadr nrepl-endpoint))))))))
237+
(cider-ensure-connected)
238+
(let* ((project-directory
239+
(or (when (eq 16 arg) (ido-read-directory-name "Project: "))
240+
(nrepl-project-directory-for (nrepl-current-dir))))
241+
(connection-buffer
242+
(or
243+
(and (= 1 (length nrepl-connection-list)) (car nrepl-connection-list))
244+
(and project-directory
245+
(cider-find-connection-buffer-for-project-directory project-directory)))))
246+
(when connection-buffer
247+
(setq nrepl-connection-list
248+
(cons connection-buffer (delq connection-buffer nrepl-connection-list))))
249+
(cider-switch-to-current-repl-buffer arg)
250+
(message
251+
(format (if connection-buffer
252+
"Switched to REPL: %s"
253+
"Could not determine relevant nREPL connection, using: %s")
254+
(with-current-buffer (nrepl-current-connection-buffer)
255+
(format "%s:%s, %s:%s"
256+
(or (nrepl--project-name nrepl-project-dir) "<no project>")
257+
nrepl-buffer-ns
258+
(car nrepl-endpoint)
259+
(cadr nrepl-endpoint)))))))
262260

263261
(defun cider-switch-to-last-clojure-buffer ()
264262
"Switch to the last Clojure buffer.
@@ -1116,6 +1114,12 @@ Useful in hooks."
11161114
(nrepl-current-connection-buffer)
11171115
(error nil)))
11181116

1117+
(defun cider-ensure-connected ()
1118+
"Ensure there is a cider connection present, otherwise
1119+
an error is signalled."
1120+
(unless (cider-connected-p)
1121+
(error "No active nREPL connection")))
1122+
11191123
(defun cider-enable-on-existing-clojure-buffers ()
11201124
"Enable interaction mode on existing Clojure buffers.
11211125
See command `cider-mode'."

0 commit comments

Comments
 (0)