Skip to content

Commit 61ee56a

Browse files
committed
[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 eb03c94 commit 61ee56a

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed

CHANGELOG.md

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

4949
### Changes
5050

51+
* [#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.
5152
* [#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.
5253
* [#1348](https://github.com/clojure-emacs/cider/issues/1348): Drop the dash dependency.
5354
* 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: 14 additions & 8 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
;; TODO: Add logic to dispatch to a matching Clojure/ClojureScript REPL based on file type
@@ -422,12 +420,16 @@ Signal an error if it is not supported."
422420
(unless (cider-nrepl-op-supported-p op)
423421
(error "Can't find nREPL middleware providing op \"%s\". Please, install (or update) cider-nrepl %s and restart CIDER" op (upcase cider-version))))
424422

425-
(defun cider-nrepl-send-request (request callback)
423+
(defun cider-nrepl-send-request (request callback &optional connection)
426424
"Send REQUEST and register response handler CALLBACK.
427425
REQUEST is a pair list of the form (\"op\" \"operation\" \"par1-name\"
428426
\"par1\" ... ).
427+
428+
If CONNECTION is provided dispatch to that connection instead of
429+
the current connection.
430+
429431
Return the id of the sent message."
430-
(nrepl-send-request request callback (cider-current-connection)))
432+
(nrepl-send-request request callback (or connection (cider-current-connection))))
431433

432434
(defun cider-nrepl-send-sync-request (request &optional abort-on-input)
433435
"Send REQUEST to the nREPL server synchronously.
@@ -596,17 +598,21 @@ thing at point."
596598
;;; Requests
597599

598600
(declare-function cider-load-file-handler "cider-interaction")
599-
(defun cider-request:load-file (file-contents file-path file-name &optional callback)
601+
(defun cider-request:load-file (file-contents file-path file-name &optional connection callback)
600602
"Perform the nREPL \"load-file\" op.
601603
FILE-CONTENTS, FILE-PATH and FILE-NAME are details of the file to be
602-
loaded. If CALLBACK is nil, use `cider-load-file-handler'."
604+
loaded.
605+
606+
If CONNECTION is nil, use `cider-current-connection'.
607+
If CALLBACK is nil, use `cider-load-file-handler'."
603608
(cider-nrepl-send-request (list "op" "load-file"
604609
"session" (cider-current-session)
605610
"file" file-contents
606611
"file-path" file-path
607612
"file-name" file-name)
608613
(or callback
609-
(cider-load-file-handler (current-buffer)))))
614+
(cider-load-file-handler (current-buffer)))
615+
connection))
610616

611617

612618
;;; Sync Requests

cider-common.el

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,20 @@ 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+
(let ((other-connection (gensym)))
235+
`(if-let ((_ (cider--cljc-or-cljx-buffer-p))
236+
(,other-connection (cider-other-connection)))
237+
(dolist (,var (list (cider-current-connection) ,other-connection))
238+
,@body)
239+
(let ((,var (cider-current-connection)))
240+
,@body))))
241+
227242
(provide 'cider-common)
228243
;;; cider-common.el ends here

cider-interaction.el

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

1345-
(defun cider-load-file (filename)
1346-
"Load (eval) the Clojure file FILENAME in nREPL."
1347-
(interactive (list
1348-
(read-file-name "Load file: " nil nil nil
1349-
(when (buffer-file-name)
1350-
(file-name-nondirectory
1351-
(buffer-file-name))))))
1352-
(cider-ensure-connected)
1353-
(when-let ((buf (find-buffer-visiting filename)))
1354-
(with-current-buffer buf
1355-
(remove-overlays nil nil 'cider-type 'instrumented-defs)
1356-
(cider--clear-compilation-highlights)))
1357-
(cider--quit-error-window)
1358-
(cider--cache-ns-form)
1359-
(cider-request:load-file
1360-
(cider-file-string filename)
1361-
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1362-
(file-name-nondirectory filename))
1363-
(message "Loading %s..." filename))
1364-
13651345
(defun cider-load-buffer (&optional buffer)
13661346
"Load (eval) BUFFER's file in nREPL.
13671347
If no buffer is provided the command acts on the current buffer.
1368-
The heavy lifting is done by `cider-load-file'."
1348+
1349+
If the buffer is for a cljc or cljx file, and both a Clojure and
1350+
ClojureScript REPL exists for the project, it is evaluated in both REPLs."
13691351
(interactive)
13701352
(check-parens)
1353+
(cider-ensure-connected)
13711354
(setq buffer (or buffer (current-buffer)))
13721355
(with-current-buffer buffer
13731356
(unless buffer-file-name
@@ -1377,7 +1360,33 @@ The heavy lifting is done by `cider-load-file'."
13771360
(or (eq cider-prompt-save-file-on-load 'always-save)
13781361
(y-or-n-p (format "Save file %s? " buffer-file-name))))
13791362
(save-buffer))
1380-
(cider-load-file buffer-file-name)))
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+
(let ((filename (buffer-file-name)))
1368+
(cider-do-connections connection
1369+
(cider-request:load-file
1370+
(cider-file-string filename)
1371+
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1372+
(file-name-nondirectory filename)
1373+
connection))
1374+
(message "Loading %s..." filename))))
1375+
1376+
(defun cider-load-file (filename)
1377+
"Load (eval) the Clojure file FILENAME in nREPL.
1378+
1379+
If the file is a cljc or cljx file, and both a Clojure and ClojureScript
1380+
REPL exists for the project, it is evaluated in both REPLs.
1381+
1382+
The heavy lifting is done by `cider-load-buffer'."
1383+
(interactive (list
1384+
(read-file-name "Load file: " nil nil nil
1385+
(when (buffer-file-name)
1386+
(file-name-nondirectory
1387+
(buffer-file-name))))))
1388+
(when-let ((buffer (find-buffer-visiting filename)))
1389+
(cider-load-buffer buffer)))
13811390

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

cider-util.el

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ If BUFFER is provided act on that buffer instead."
8787
(with-current-buffer (or buffer (current-buffer))
8888
(or (derived-mode-p 'clojurec-mode) (derived-mode-p 'clojurex-mode))))
8989

90+
(defun cider--cljc-or-cljx-file-p (file-name)
91+
"Return true if FILE-NAME is for a cljc or cljx file."
92+
(string-match "\\.clj\\(x\\|c\\)\\'" file-name))
93+
9094

9195
;;; Thing at point
9296
(defun cider-defun-at-point ()

0 commit comments

Comments
 (0)