Skip to content

Commit 27b6067

Browse files
expezMalabarba
authored andcommitted
[Fix #1299] Eval in both REPLs for cljc and cljx
This changes the load file request to dispatch to `cider-other-connection` in addition to `cider-current-connection`, when the file is a cljc or cljx file.
1 parent a480f48 commit 27b6067

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
### Changes
5151

52+
* [#1299]https://github.com/clojure-emacs/cider/issues/1299 <kbd>C-c C-k</kbd> and <kbd> C-c C-l</kbd> now dispatch to both the Clojure and ClojureScript REPL (in the same project) when called from a `.cljc` or `.cljx` file.
5253
* [#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.
5354
* [#1348](https://github.com/clojure-emacs/cider/issues/1348): Drop the dash dependency.
5455
* 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`).

cider-client.el

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ from the file extension."
218218
;; OW, find one matching the extension of current file.
219219
(let ((type (or type (file-name-extension (or (buffer-file-name) "")))))
220220
(or (seq-find (lambda (conn)
221-
(equal (with-current-buffer conn
222-
cider-repl-type)
223-
type))
221+
(equal (cider--connection-type conn) type))
224222
project-connections)
225223
(car project-connections)
226224
(car cider-connections)))))))
@@ -431,21 +429,27 @@ If the nREPL request lacks a session `cider-current-session' is added."
431429
request
432430
(append request (list "session" (cider-current-session)))))
433431

434-
(defun cider-nrepl-send-request (request callback)
432+
(defun cider-nrepl-send-request (request callback &optional connection)
435433
"Send REQUEST and register response handler CALLBACK.
436434
REQUEST is a pair list of the form (\"op\" \"operation\" \"par1-name\"
437435
\"par1\" ... ).
436+
If CONNECTION is provided dispatch to that connection instead of
437+
the current connection.
438+
438439
Return the id of the sent message."
439-
(nrepl-send-request (cider--ensure-session request) callback (cider-current-connection)))
440+
(nrepl-send-request (cider--ensure-session request) callback
441+
(or connection (cider-current-connection))))
440442

441-
(defun cider-nrepl-send-sync-request (request &optional abort-on-input)
443+
(defun cider-nrepl-send-sync-request (request &optional abort-on-input connection)
442444
"Send REQUEST to the nREPL server synchronously.
443445
Hold till final \"done\" message has arrived and join all response messages
444446
of the same \"op\" that came along and return the accumulated response.
445447
If ABORT-ON-INPUT is non-nil, the function will return nil
446448
at the first sign of user input, so as not to hang the
447449
interface."
448-
(nrepl-send-sync-request (cider--ensure-session request) (cider-current-connection) abort-on-input))
450+
(nrepl-send-sync-request (cider--ensure-session request)
451+
(or connection (cider-current-connection))
452+
abort-on-input))
449453

450454
(defun cider-nrepl-send-unhandled-request (request)
451455
"Send REQUEST to the nREPL server and ignore any responses.
@@ -613,17 +617,21 @@ thing at point."
613617
;;; Requests
614618

615619
(declare-function cider-load-file-handler "cider-interaction")
616-
(defun cider-request:load-file (file-contents file-path file-name &optional callback)
620+
(defun cider-request:load-file (file-contents file-path file-name &optional connection callback)
617621
"Perform the nREPL \"load-file\" op.
618622
FILE-CONTENTS, FILE-PATH and FILE-NAME are details of the file to be
619-
loaded. If CALLBACK is nil, use `cider-load-file-handler'."
623+
loaded.
624+
625+
If CONNECTION is nil, use `cider-current-connection'.
626+
If CALLBACK is nil, use `cider-load-file-handler'."
620627
(cider-nrepl-send-request (list "op" "load-file"
621628
"session" (cider-current-session)
622629
"file" file-contents
623630
"file-path" file-path
624631
"file-name" file-name)
625632
(or callback
626-
(cider-load-file-handler (current-buffer)))))
633+
(cider-load-file-handler (current-buffer)))
634+
connection))
627635

628636

629637
;;; Sync Requests

cider-common.el

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,21 @@ existing file ending with URL has been found."
224224
(16 t) ; empty empty
225225
(_ nil))))
226226

227+
(defmacro cider-do-connections (var &rest body)
228+
"For each appropriate connection, bind it to VAR, and execute BODY.
229+
230+
The appropriate connections are found by inspecting the current buffer. If
231+
the buffer is associated with a .cljc or .cljx file, BODY will be executed
232+
multiple times."
233+
(declare (indent 1)
234+
(debug (form body)))
235+
(let ((other-connection 'oh-my-god-the-linter-wont-let-me-use-gensym))
236+
`(if-let ((_ (cider--cljc-or-cljx-buffer-p))
237+
(,other-connection (cider-other-connection)))
238+
(dolist (,var (list (cider-current-connection) ,other-connection))
239+
,@body)
240+
(let ((,var (cider-current-connection)))
241+
,@body))))
242+
227243
(provide 'cider-common)
228244
;;; cider-common.el ends here

cider-interaction.el

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,32 +1350,15 @@ unloaded."
13501350
(with-current-buffer (find-file-noselect file)
13511351
(substring-no-properties (buffer-string))))
13521352

1353-
(defun cider-load-file (filename)
1354-
"Load (eval) the Clojure file FILENAME in nREPL."
1355-
(interactive (list
1356-
(read-file-name "Load file: " nil nil nil
1357-
(when (buffer-file-name)
1358-
(file-name-nondirectory
1359-
(buffer-file-name))))))
1360-
(cider-ensure-connected)
1361-
(when-let ((buf (find-buffer-visiting filename)))
1362-
(with-current-buffer buf
1363-
(remove-overlays nil nil 'cider-type 'instrumented-defs)
1364-
(cider--clear-compilation-highlights)))
1365-
(cider--quit-error-window)
1366-
(cider--cache-ns-form)
1367-
(cider-request:load-file
1368-
(cider-file-string filename)
1369-
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1370-
(file-name-nondirectory filename))
1371-
(message "Loading %s..." filename))
1372-
13731353
(defun cider-load-buffer (&optional buffer)
13741354
"Load (eval) BUFFER's file in nREPL.
13751355
If no buffer is provided the command acts on the current buffer.
1376-
The heavy lifting is done by `cider-load-file'."
1356+
1357+
If the buffer is for a cljc or cljx file, and both a Clojure and
1358+
ClojureScript REPL exists for the project, it is evaluated in both REPLs."
13771359
(interactive)
13781360
(check-parens)
1361+
(cider-ensure-connected)
13791362
(setq buffer (or buffer (current-buffer)))
13801363
(with-current-buffer buffer
13811364
(unless buffer-file-name
@@ -1385,7 +1368,33 @@ The heavy lifting is done by `cider-load-file'."
13851368
(or (eq cider-prompt-save-file-on-load 'always-save)
13861369
(y-or-n-p (format "Save file %s? " buffer-file-name))))
13871370
(save-buffer))
1388-
(cider-load-file buffer-file-name)))
1371+
(remove-overlays nil nil 'cider-type 'instrumented-defs)
1372+
(cider--clear-compilation-highlights)
1373+
(cider--quit-error-window)
1374+
(cider--cache-ns-form)
1375+
(let ((filename (buffer-file-name)))
1376+
(cider-do-connections connection
1377+
(cider-request:load-file
1378+
(cider-file-string filename)
1379+
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1380+
(file-name-nondirectory filename)
1381+
connection))
1382+
(message "Loading %s..." filename))))
1383+
1384+
(defun cider-load-file (filename)
1385+
"Load (eval) the Clojure file FILENAME in nREPL.
1386+
1387+
If the file is a cljc or cljx file, and both a Clojure and ClojureScript
1388+
REPL exists for the project, it is evaluated in both REPLs.
1389+
1390+
The heavy lifting is done by `cider-load-buffer'."
1391+
(interactive (list
1392+
(read-file-name "Load file: " nil nil nil
1393+
(when (buffer-file-name)
1394+
(file-name-nondirectory
1395+
(buffer-file-name))))))
1396+
(when-let ((buffer (find-buffer-visiting filename)))
1397+
(cider-load-buffer buffer)))
13891398

13901399
(defalias 'cider-eval-file 'cider-load-file
13911400
"A convenience alias as some people are confused by the load-* names.")

0 commit comments

Comments
 (0)