Skip to content

Commit

Permalink
[Fix #1297] C-c M-n should act on both repls
Browse files Browse the repository at this point in the history
In a cljc or cljx buffer hitting `C-c M-n` to switch the repl ns will
now act on both repls so they are in sync.
  • Loading branch information
expez committed Nov 4, 2015
1 parent b3f69bf commit e67be7e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

### Changes

* [#1397](https://github.com/clojure-emacs/cider/issues/1297) <kbd>C-c M-n</kbd> now changes the ns of both the Clojure and ClojureScript REPL (in the same project) when called from a cljc or cljx file.
* [#1348](https://github.com/clojure-emacs/cider/issues/1348): Drop the dash dependency.
* The usage of the default connection has been reduced significantly. Now evaluations & related commands will be routed via the connection matching the current project automatically unless there's some ambiguity when determining the connection (like multiple or no matching connections). Simply put you'll no longer have to mess around much with connecting-setting commands (e.g. `nrepl-connection-browser`, `cider-rotate-default-connection`).
* [#732](https://github.com/clojure-emacs/cider/issues/732): `cider-quit` and `cider-restart` now operate on the current connection only. With a prefix argument they operate on all connections.
Expand Down
29 changes: 22 additions & 7 deletions cider-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,15 @@ such a link cannot be established automatically."
A REPL is relevant if its `nrepl-project-dir' is compatible with the
current directory (see `cider-find-connection-buffer-for-project-directory').
If there is ambiguity, it is resolved by matching TYPE with the REPL
type (Clojure or ClojureScript). If TYPE is nil, it is derived from the
file extension."
;; Cleanup the connections list.
(cider-connections)
type (Clojure or ClojureScript). TYPE is a string, which when nil is derived
from the file extension."
(cider-connections) ; Cleanup the connections list.
(if (eq cider-request-dispatch 'dynamic)
(cond
((cider--in-connection-buffer-p) (current-buffer))
((= 1 (length cider-connections)) (car cider-connections))
(t (let* ((project-directory (clojure-project-dir (cider-current-dir)))
(repls (and project-directory
(cider-find-connection-buffer-for-project-directory project-directory 'all))))
(t (let ((repls (cider-find-connection-buffer-for-project-directory
nil :all-connections)))
(if (= 1 (length repls))
;; Only one match, just return it.
(car repls)
Expand All @@ -228,6 +226,16 @@ file extension."
;; TODO: Add logic to dispatch to a matching Clojure/ClojureScript REPL based on file type
(car cider-connections)))

(defun cider-other-connection (connection)
"Return the first connection of another type than CONNECTION, \
in the same project, or nil."
(let* ((connection-type (cider--connection-type connection))
(other (if (equal connection-type "clj")
(cider-current-connection "cljs")
(cider-current-connection "clj"))))
(unless (equal connection-type (cider--connection-type other))
other)))


;;; Connection Browser
(defvar cider-connections-buffer-mode-map
Expand Down Expand Up @@ -791,10 +799,17 @@ endpoint and Clojure version."
"Extract the essential properties of CONN-BUFFER."
(with-current-buffer conn-buffer
(list
:type cider-repl-type
:host (car nrepl-endpoint)
:port (cadr nrepl-endpoint)
:project-dir nrepl-project-dir)))

(defun cider--connection-type (conn-buffer)
"Get CONN-BUFFER's type.
Return value matches `cider-repl-type'."
(plist-get (cider--connection-properties conn-buffer) :type))

(defun cider--connection-host (conn-buffer)
"Get CONN-BUFFER's host."
(plist-get (cider--connection-properties conn-buffer) :host))
Expand Down
19 changes: 14 additions & 5 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
(require 'cider-eldoc) ; for cider-eldoc-setup
(require 'cider-common)
(require 'cider-compat)
(require 'cider-util)

(require 'clojure-mode)
(require 'easymenu)
Expand Down Expand Up @@ -761,18 +762,26 @@ text property `cider-old-input'."
(defun cider-repl-set-ns (ns)
"Switch the namespace of the REPL buffer to NS.
If invoked in a REPL buffer the command will prompt you for the name of the
If called from a cljc or cljx buffer act on both the Clojure and
ClojureScript REPL if there are more than one REPL present.
If invoked in a REPL buffer the command will prompt for the name of the
namespace to switch to."
(interactive (list (if (or (derived-mode-p 'cider-repl-mode)
(null (cider-ns-form)))
(completing-read "Switch to namespace: "
(cider-sync-request:ns-list))
(cider-current-ns))))
(if (and ns (not (equal ns "")))
(when (or (not ns) (equal ns ""))
(user-error "No namespace selected"))
(cider-nrepl-request:eval (format "(in-ns '%s)" ns)
(cider-repl-switch-ns-handler
(cider-current-connection)))
(when (cider--cljc-or-cljx-buffer-p)
(when-let ((other-connection (cider-other-connection
(cider-current-connection))))
(cider-nrepl-request:eval (format "(in-ns '%s)" ns)
(cider-repl-switch-ns-handler (cider-current-connection))
nil)
(error "No namespace selected")))
(cider-repl-switch-ns-handler other-connection)))))


;;;;; History
Expand Down
14 changes: 14 additions & 0 deletions cider-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ which nREPL uses for temporary evaluation file names."
(let ((fname (file-name-nondirectory file-name)))
(string-match-p "^form-init" fname)))

(defun cider--cljc-or-cljx-buffer-p (&optional buffer)
"Return true if the current buffer is visiting a cljc or cljx file.
If BUFFER is provided act on that buffer instead."
(with-current-buffer (or buffer (current-buffer))
(or (derived-mode-p 'clojurec-mode) (derived-mode-p 'clojurex-mode))))


;;; Thing at point
(defun cider-defun-at-point ()
"Return the text of the top-level sexp at point."
Expand Down Expand Up @@ -131,6 +139,8 @@ which nREPL uses for temporary evaluation file names."
""
str)))


;;; sexp navigation
(defun cider-sexp-at-point ()
"Return the sexp at point as a string, otherwise nil."
(let ((bounds (cider-bounds-of-sexp-at-point)))
Expand Down Expand Up @@ -171,6 +181,7 @@ Can only error if SKIP is non-nil."
(forward-sexp 1)
(cider-start-of-next-sexp))))


;;; Text properties

(defun cider-maybe-intern (name)
Expand Down Expand Up @@ -205,6 +216,7 @@ PROP is the name of a text property."
(when more-text (insert more-text))
(when break (insert "\n")))


;;; Font lock

(defun cider--font-lock-ensure ()
Expand Down Expand Up @@ -283,6 +295,7 @@ Unless you specify a BUFFER it will default to the current one."
(pkg-info-version-info 'cider)
(error cider-version)))


;;; Strings

(defun cider-string-join (strings &optional separator)
Expand Down Expand Up @@ -333,6 +346,7 @@ Any other value is just returned."
(mapcar #'cider--deep-vector-to-list x)
x))


;;; Words of inspiration
(defun cider-user-first-name ()
"Find the current user's first name."
Expand Down

0 comments on commit e67be7e

Please sign in to comment.