Skip to content

Commit d63a7df

Browse files
authored
Merge pull request #2127 from vspinu/switch
Switch to REPL of correct dialect
2 parents 8cd7648 + 59905c3 commit d63a7df

File tree

5 files changed

+49
-53
lines changed

5 files changed

+49
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
### Changes
1212

13+
* `cider-switch-to-last-clojure-buffer` switches to most recent relevant Clojure(script) buffer instead of the last "remembered" buffer.
1314
* [cider-nrepl#438](https://github.com/clojure-emacs/cider-nrepl/pull/438): Improve startup time by deferring loading CIDER's middleware until the first usage.
1415
* [#2078](https://github.com/clojure-emacs/cider/pull/2078): Improve startup time by bundling together sync requests during startup.
1516
* `cider-rotate-default-connection` will warn if you use it with only a single active connection.
1617
* `cider-format-buffer` tries to preserve the point position.
1718

1819
### Bugs Fixed
1920

21+
* [#2084](https://github.com/clojure-emacs/cider/issues/2084): Select correct REPL type (clj or cljs) in cider-switch-to-repl-buffer conditional on the current buffer.
2022
* [#2088](https://github.com/clojure-emacs/cider/issues/2088): Fix functions defined with `def` being font-locked as vars instead of functions.
2123
* [#1651](https://github.com/clojure-emacs/cider/issues/1651), [cider-nrepl#445](https://github.com/clojure-emacs/cider-nrepl/pull/455): Fix `cider-expected-ns` returns `nil` on boot projects.
2224
* [#2120](https://github.com/clojure-emacs/cider/issues/2120): Fix Travis CI build errors for emacs versions >25.2.

cider-client.el

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,19 @@ connections are returned, instead of just the most recent."
178178
cider-connections
179179
(car cider-connections)))))
180180

181-
(defun cider-connection-type-for-buffer ()
182-
"Return the matching connection type (clj or cljs) for the current buffer.
181+
(defun cider-connection-type-for-buffer (&optional buffer)
182+
"Return the matching connection type (clj or cljs) for BUFFER.
183183
In cljc and cljx buffers return \"multi\". This function infers connection
184184
type based on the major mode. See `cider-project-connections-types' for a
185-
list of types of actual connections within a project."
186-
(cond
187-
((derived-mode-p 'clojurescript-mode) "cljs")
188-
((derived-mode-p 'clojurec-mode) "multi")
189-
((derived-mode-p 'clojurex-mode) "multi")
190-
((derived-mode-p 'clojure-mode) "clj")
191-
(cider-repl-type)))
185+
list of types of actual connections within a project. BUFFER defaults to
186+
the `current-buffer'."
187+
(with-current-buffer (or buffer (current-buffer))
188+
(cond
189+
((derived-mode-p 'clojurescript-mode) "cljs")
190+
((derived-mode-p 'clojurec-mode) "multi")
191+
((derived-mode-p 'clojurex-mode) "multi")
192+
((derived-mode-p 'clojure-mode) "clj")
193+
(cider-repl-type))))
192194

193195
(defun cider-project-connections-types ()
194196
"Return a list of types of connections within current project."

cider-mode.el

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ entirely."
8383

8484

8585
;;; Switching between REPL & source buffers
86-
(defvar-local cider-last-clojure-buffer nil
87-
"A buffer-local variable holding the last Clojure source buffer.
88-
`cider-switch-to-last-clojure-buffer' uses this variable to jump
89-
back to last Clojure source buffer.")
90-
91-
(defun cider-remember-clojure-buffer (buffer)
92-
"Try to remember the BUFFER from which the user jumps.
93-
The BUFFER needs to be a Clojure buffer and current major mode needs
94-
to be `cider-repl-mode'. The user can use `cider-switch-to-last-clojure-buffer'
95-
to jump back to the last Clojure source buffer."
96-
(when (and buffer
97-
(with-current-buffer buffer
98-
(derived-mode-p 'clojure-mode))
99-
(derived-mode-p 'cider-repl-mode))
100-
(setq cider-last-clojure-buffer buffer)))
10186

10287
(defun cider--switch-to-repl-buffer (repl-buffer &optional set-namespace)
10388
"Select the REPL-BUFFER, when possible in an existing window.
@@ -117,7 +102,6 @@ that of the namespace in the Clojure source buffer."
117102
;; then if necessary we update its namespace
118103
(when set-namespace
119104
(cider-repl-set-ns (with-current-buffer buffer (cider-current-ns))))
120-
(cider-remember-clojure-buffer buffer)
121105
(goto-char (point-max))))
122106

123107
(defun cider-switch-to-repl-buffer (&optional set-namespace)
@@ -138,9 +122,17 @@ With a prefix arg SET-NAMESPACE sets the namespace in the REPL buffer to that
138122
of the namespace in the Clojure source buffer."
139123
(interactive "P")
140124
(let* ((connections (cider-connections))
141-
(buffer (seq-find (lambda (b) (member b connections))
142-
(buffer-list))))
143-
(cider--switch-to-repl-buffer buffer set-namespace)))
125+
(type (cider-connection-type-for-buffer))
126+
(a-repl)
127+
(the-repl (seq-find (lambda (b)
128+
(when (member b connections)
129+
(unless a-repl
130+
(setq a-repl b))
131+
(equal type (cider-connection-type-for-buffer b))))
132+
(buffer-list))))
133+
(if-let ((repl (or the-repl a-repl)))
134+
(cider--switch-to-repl-buffer repl set-namespace)
135+
(user-error "No REPL found"))))
144136

145137
(declare-function cider-load-buffer "cider-interaction")
146138

@@ -159,12 +151,23 @@ the same as `cider-switch-to-repl-buffer',
159151
so that it is very convenient to jump between a
160152
Clojure buffer and the REPL buffer."
161153
(interactive)
162-
(if (and (derived-mode-p 'cider-repl-mode)
163-
(buffer-live-p cider-last-clojure-buffer))
164-
(if cider-repl-display-in-current-window
165-
(pop-to-buffer-same-window cider-last-clojure-buffer)
166-
(pop-to-buffer cider-last-clojure-buffer))
167-
(message "Don't know the original Clojure buffer")))
154+
(if (derived-mode-p 'cider-repl-mode)
155+
(let* ((a-buf)
156+
(the-buf (let ((repl-type (cider-connection-type-for-buffer)))
157+
(seq-find (lambda (b)
158+
(unless (with-current-buffer b (derived-mode-p 'cider-repl-mode))
159+
(when-let ((type (cider-connection-type-for-buffer b)))
160+
(unless a-buf
161+
(setq a-buf b))
162+
(or (equal type "multi")
163+
(equal type repl-type)))))
164+
(buffer-list)))))
165+
(if-let ((buf (or the-buf a-buf)))
166+
(if cider-repl-display-in-current-window
167+
(pop-to-buffer-same-window buf)
168+
(pop-to-buffer buf))
169+
(user-error "No Clojure buffer found")))
170+
(user-error "Not in a CIDER REPL buffer")))
168171

169172
(defun cider-find-and-clear-repl-output (&optional clear-repl)
170173
"Find the current REPL buffer and clear it.

cider-repl.el

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,15 @@ efficiency."
274274
(cider-set-buffer-ns initial-ns)))))
275275

276276
(defun cider-repl-require-repl-utils ()
277-
"Require standard REPL util functions into the current REPL."
278-
(interactive)
279-
(nrepl-send-sync-request
280-
(lax-plist-put
281-
(nrepl--eval-request
282-
"(when (clojure.core/resolve 'clojure.main/repl-requires)
277+
"Require standard REPL util functions into the current REPL."
278+
(interactive)
279+
(nrepl-send-sync-request
280+
(lax-plist-put
281+
(nrepl--eval-request
282+
"(when (clojure.core/resolve 'clojure.main/repl-requires)
283283
(clojure.core/map clojure.core/require clojure.main/repl-requires))")
284-
"inhibit-cider-middleware" "true")
285-
(cider-current-connection)))
286-
287-
(defvar cider-current-clojure-buffer nil
288-
"This variable holds current buffer temporarily when connecting to a REPL.
289-
It is set to current buffer when `cider' or `cider-jack-in' is called.
290-
After the REPL buffer is created, the value of this variable is used
291-
to call `cider-remember-clojure-buffer'.")
292-
293-
(declare-function cider-remember-clojure-buffer "cider-mode")
284+
"inhibit-cider-middleware" "true")
285+
(cider-current-connection)))
294286

295287
(defun cider-repl-init (buffer &optional no-banner)
296288
"Initialize the REPL in BUFFER.
@@ -305,7 +297,6 @@ client process connection. Unless NO-BANNER is non-nil, insert a banner."
305297
(cider-repl-require-repl-utils)
306298
(unless no-banner
307299
(cider-repl--insert-banner-and-prompt buffer))
308-
(cider-remember-clojure-buffer cider-current-clojure-buffer)
309300
buffer)
310301

311302
(defun cider-repl--insert-banner-and-prompt (buffer)

cider.el

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ start the server.
604604
If CLJS-TOO is non-nil, also start a ClojureScript REPL session with its
605605
own buffer."
606606
(interactive "P")
607-
(setq cider-current-clojure-buffer (current-buffer))
608607
(let* ((project-type (cider-project-type))
609608
(command (cider-jack-in-command project-type))
610609
(command-resolved (cider-jack-in-resolve-command project-type))
@@ -657,7 +656,6 @@ Create REPL buffer and start an nREPL client connection.
657656
When the optional param PROJECT-DIR is present, the connection
658657
gets associated with it."
659658
(interactive (cider-select-endpoint))
660-
(setq cider-current-clojure-buffer (current-buffer))
661659
(when-let* ((repl-buff (cider-find-reusable-repl-buffer `(,host ,port) nil)))
662660
(let* ((nrepl-create-client-buffer-function #'cider-repl-create)
663661
(nrepl-use-this-as-repl-buffer repl-buff)

0 commit comments

Comments
 (0)