Skip to content

Fix `nrepl-switch-to-repl-buffer' to work with all connections #361

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

Merged
merged 1 commit into from
Aug 21, 2013
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* <kbd>C-c M-r</kbd> will rotate and display the current nREPL connection.
* Setting the variable `nrepl-buffer-name-show-port` will display the port on which the nRepl server is running.
* nRepl buffer name uses project directory name; `*nrepl*` will appear as `*nrepl project-directory-name*`.
* <kbd>C-c C-z</kbd> will select the clojure buffer based on the current namespace.
* <kbd>C-u C-u C-c C-z</kbd> will select the clojure buffer based on a user directory prompt.
* <kbd>C-c C-Z</kbd> will select the nrepl buffer based on the current namespace.
* <kbd>C-u C-c C-Z</kbd> will select the nrepl buffer based on a user project directory prompt.

### Bugs fixed

Expand Down
57 changes: 38 additions & 19 deletions nrepl.el
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,7 @@ This will not work on non-current prompts."
(define-key map (kbd "C-c C-d") 'nrepl-doc)
(define-key map (kbd "C-c C-s") 'nrepl-src)
(define-key map (kbd "C-c C-z") 'nrepl-switch-to-repl-buffer)
(define-key map (kbd "C-c C-Z") 'nrepl-switch-to-relevant-repl-buffer)
(define-key map (kbd "C-c M-o") 'nrepl-find-and-clear-repl-buffer)
(define-key map (kbd "C-c C-k") 'nrepl-load-current-buffer)
(define-key map (kbd "C-c C-l") 'nrepl-load-file)
Expand Down Expand Up @@ -1597,6 +1598,7 @@ This will not work on non-current prompts."
"--"
["Set ns" nrepl-set-ns]
["Switch to REPL" nrepl-switch-to-repl-buffer]
["Switch to Relevant REPL" nrepl-switch-to-relevant-repl-buffer]
["Toggle REPL Pretty Print" nrepl-pretty-toggle]
["Clear REPL" nrepl-find-and-clear-repl-buffer]
["Interrupt" nrepl-interrupt]
Expand Down Expand Up @@ -2701,6 +2703,26 @@ Insert a banner, unless NOPROMPT is non-nil."

(defun nrepl-switch-to-repl-buffer (arg)
"Select the repl buffer, when possible in an existing window.

Hint: You can use `display-buffer-reuse-frames' and
`special-display-buffer-names' to customize the frame in which
the buffer should appear.

With a prefix ARG sets the name of the repl buffer to the one
of the current source file."
(interactive "P")
(if (not (get-buffer (nrepl-current-connection-buffer)))
(message "No active nREPL connection.")
(progn
(let ((buffer (current-buffer)))
(when arg
(nrepl-set-ns (nrepl-current-ns)))
(pop-to-buffer (nrepl-find-or-create-repl-buffer))
(nrepl-remember-clojure-buffer buffer)
(goto-char (point-max))))))

(defun nrepl-switch-to-relevant-repl-buffer (arg)
"Select the repl buffer, when possible in an existing window.
The buffer chosen is based on the file open in the current buffer.

Hint: You can use `display-buffer-reuse-frames' and
Expand All @@ -2712,32 +2734,29 @@ of the current source file.

With a second prefix ARG the chosen repl buffer is based on a
supplied project directory."
(interactive "p")
(interactive "P")
(if (not (get-buffer (nrepl-current-connection-buffer)))
(message "No active nREPL connection.")
(progn
(let ((project-directory
(or (when (eq 16 arg)
(or (when arg
(ido-read-directory-name "Project: "))
(nrepl-project-directory-for (nrepl-current-dir)))))
(when project-directory
(lexical-let ((buf (car (cl-remove-if-not
(lambda (conn)
(equal (file-truename project-directory)
(file-truename
(with-current-buffer (get-buffer conn)
nrepl-project-dir))))
nrepl-connection-list))))
(if (not buf)
(message (format "nREPL connection not found for %s." (file-truename project-directory)))
(if project-directory
(let ((buf (car (-filter
(lambda (conn)
(let ((conn-proj-dir (with-current-buffer (get-buffer conn)
nrepl-project-dir)))
(when conn-proj-dir
(equal (file-truename project-directory)
(file-truename conn-proj-dir)))))
nrepl-connection-list))))
(if buf
(setq nrepl-connection-list
(cons buf (delq buf nrepl-connection-list)))))))
(let ((buffer (current-buffer)))
(when (eq 4 arg)
(nrepl-set-ns (nrepl-current-ns)))
(pop-to-buffer (nrepl-find-or-create-repl-buffer))
(nrepl-remember-clojure-buffer buffer)
(goto-char (point-max))))))
(cons buf (delq buf nrepl-connection-list)))
(message "No relevant nREPL connection found. Switching to default connection.")))
(message "No project directory found. Switching to default nREPL connection.")))
(nrepl-switch-to-repl-buffer '()))))

(defun nrepl-switch-to-last-clojure-buffer ()
"Switch to the last clojure buffer.
Expand Down
54 changes: 54 additions & 0 deletions test/nrepl-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,57 @@
(should
(equal (nrepl-repl-buffer-name) "*nrepl proj:4009*<2>")))
(kill-buffer nrepl-new-buffer)))))

(ert-deftest test-nrepl-switch-to-relevant-repl-buffer ()
(noflet ((nrepl-project-directory-for (dontcare)
nrepl-project-dir))
(let* ((b1 (generate-new-buffer "temp"))
(b2 (generate-new-buffer "temp"))
(b3 (generate-new-buffer "temp"))
(b4 (generate-new-buffer "temp"))
(b5 (generate-new-buffer "temp"))
(b6 (generate-new-buffer "temp"))
(nrepl-connection-list (list (buffer-name b1)
(buffer-name b2)
(buffer-name b3))))
(with-current-buffer b1 ;; nrepl-jack-in 1
(set (make-local-variable 'nrepl-endpoint) '("localhost" 4005))
(set (make-local-variable 'nrepl-project-dir) "proj1")
(set (make-local-variable 'nrepl-repl-buffer) b4))
(with-current-buffer b2 ;; nrepl-jack-in 2
(set (make-local-variable 'nrepl-endpoint) '("localhost" 4006))
(set (make-local-variable 'nrepl-project-dir) "proj2")
(set (make-local-variable 'nrepl-repl-buffer) b5))
(with-current-buffer b3 ;; nrepl-connect - no relevant buffer
(set (make-local-variable 'nrepl-endpoint) '("123.123.123.123" 4009))
(set (make-local-variable 'nrepl-repl-buffer) b6))

(with-current-buffer b1
(nrepl-switch-to-relevant-repl-buffer '())
(should (equal b4 (current-buffer)))
(should (equal (list (buffer-name b1) (buffer-name b2) (buffer-name b3))
nrepl-connection-list)))

(with-current-buffer b2
(nrepl-switch-to-relevant-repl-buffer '())
(should (equal b5 (current-buffer)))
(should (equal (list (buffer-name b2) (buffer-name b1) (buffer-name b3))
nrepl-connection-list)))

(with-current-buffer b3
(nrepl-switch-to-relevant-repl-buffer '())
(should (equal b5 (current-buffer))) ;; didn't switch to anything
(should (equal (list (buffer-name b2) (buffer-name b1) (buffer-name b3))
nrepl-connection-list)))

(let ((nrepl-connection-list (list (buffer-name b3)
(buffer-name b2)
(buffer-name b1))))
(with-current-buffer b1
(nrepl-switch-to-relevant-repl-buffer '())
(should (equal b4 (current-buffer)))
(should (equal (list (buffer-name b1) (buffer-name b3) (buffer-name b2))
nrepl-connection-list))))

(dolist (buf (list b1 b2 b3 b4 b5 b6))
(kill-buffer buf)))))