Skip to content

Commit d80acae

Browse files
[connection] Introduce cider--ancillary-buffer-current-repl
1 parent ee5ee37 commit d80acae

File tree

3 files changed

+53
-60
lines changed

3 files changed

+53
-60
lines changed

cider-connection.el

+18-11
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,12 @@ function with the repl buffer set as current."
938938
(user-error "No %s REPLs in current session \"%s\""
939939
type (car (sesman-current-session 'CIDER)))))
940940

941+
(defvar-local cider--ancillary-buffer-repl nil
942+
"Special buffer-local variable that contains reference to the REPL connection.
943+
This should be set in ancillary CIDER buffers that originate from some
944+
event (e.g. *cider-inspector*, *cider-error*) and which never change the
945+
REPL (connection) which produced them.")
946+
941947
(defun cider-current-repl (&optional type ensure)
942948
"Get the most recent REPL of TYPE from the current session.
943949
TYPE is either clj, cljs, multi or any.
@@ -951,17 +957,18 @@ no linked session or there is no REPL of TYPE within the current session."
951957
(eq cider-repl-type type)))
952958
;; shortcut when in REPL buffer
953959
(current-buffer)
954-
(let* ((type (or type (cider-repl-type-for-buffer)))
955-
(repls (cider-repls type ensure))
956-
(repl (if (<= (length repls) 1)
957-
(car repls)
958-
;; pick the most recent one
959-
(seq-find (lambda (b)
960-
(member b repls))
961-
(buffer-list)))))
962-
(if (and ensure (null repl))
963-
(cider--no-repls-user-error type)
964-
repl)))))
960+
(or cider--ancillary-buffer-repl
961+
(let* ((type (or type (cider-repl-type-for-buffer)))
962+
(repls (cider-repls type ensure))
963+
(repl (if (<= (length repls) 1)
964+
(car repls)
965+
;; pick the most recent one
966+
(seq-find (lambda (b)
967+
(member b repls))
968+
(buffer-list)))))
969+
(if (and ensure (null repl))
970+
(cider--no-repls-user-error type)
971+
repl))))))
965972

966973
(defun cider--match-repl-type (type buffer)
967974
"Return non-nil if TYPE matches BUFFER's REPL type."

cider-debug.el

-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,6 @@ needed. It is expected to contain at least \"key\", \"input-type\", and
683683
(setq cider--debug-mode-response response)
684684
(cider--debug-mode 1)))
685685
(when inspect
686-
(setq cider-inspector--current-repl (cider-current-repl))
687686
(cider-inspector--render-value inspect)))
688687
;; If something goes wrong, we send a "quit" or the session hangs.
689688
(error (cider-debug-mode-send-reply ":quit" key)

cider-inspector.el

+35-48
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ This is used as an alternative to the built-in `last-command'. Whenever we
216216
invoke any command through \\[execute-extended-command] and its variants,
217217
the value of `last-command' is not set to the command it invokes.")
218218

219-
(defvar cider-inspector--current-repl nil
220-
"Contains the reference to the REPL where inspector was last invoked from.
221-
This is needed for internal inspector buffer operations (push,
222-
pop) to execute against the correct REPL session.")
223-
224219
;; Operations
225220
;;;###autoload
226221
(defun cider-inspect-expr (expr ns)
@@ -229,7 +224,6 @@ Interactively, EXPR is read from the minibuffer, and NS the
229224
current buffer's namespace."
230225
(interactive (list (cider-read-from-minibuffer "Inspect expression: " (cider-sexp-at-point))
231226
(cider-current-ns)))
232-
(setq cider-inspector--current-repl (cider-current-repl))
233227
(let ((result (cider-sync-request:inspect-expr expr ns)))
234228
(when (nrepl-dict-get result "value")
235229
(cider-inspector--render-value result))))
@@ -267,7 +261,6 @@ See `cider-sync-request:inspect-push' and `cider-inspector--render-value'"
267261
"Inspects the exception in the cause stack identified by INDEX."
268262
(interactive)
269263
(cl-assert (numberp index))
270-
(setq cider-inspector--current-repl (cider-current-repl))
271264
(let ((result (cider-sync-request:inspect-last-exception index)))
272265
(when (nrepl-dict-get result "value")
273266
(push (point) cider-inspector-location-stack)
@@ -298,7 +291,7 @@ See `cider-sync-request:inspect-next-sibling' and `cider-inspector--render-value
298291
"Invokes `inspect-refresh' op with supplied extra OPTS.
299292
Re-renders the currently inspected value."
300293
(let ((result (cider-nrepl-send-sync-request `("op" "inspect-refresh" ,@opts)
301-
cider-inspector--current-repl)))
294+
(cider-current-repl))))
302295
(when (nrepl-dict-get result "value")
303296
(cider-inspector--render-value result))))
304297

@@ -355,7 +348,7 @@ MAX-NESTED-DEPTH is the new value."
355348
"Toggle the view mode of the inspector between normal and object view mode."
356349
(interactive)
357350
(let ((result (cider-nrepl-send-sync-request `("op" "inspect-toggle-view-mode")
358-
cider-inspector--current-repl)))
351+
(cider-current-repl))))
359352
(when (nrepl-dict-get result "value")
360353
(cider-inspector--render-value result))))
361354

@@ -394,120 +387,112 @@ current-namespace."
394387
(defun cider-inspector-tap-current-val ()
395388
"Sends the current Inspector current value to `tap>'."
396389
(interactive)
397-
;; NOTE: we don't set `cider-inspector--current-repl', because we mean to tap the current value of an existing Inspector,
398-
;; so whatever repl was used for it, should be used here.
399-
(if cider-inspector--current-repl
400-
(let ((response (cider-sync-request:inspect-tap-current-val)))
401-
(nrepl-dbind-response response (value err)
402-
(if value
403-
(message "Successfully tapped the current Inspector value")
404-
(error "Could not tap the current Inspector value: %s" err))))
405-
(user-error "No CIDER session found")))
390+
(let ((response (cider-sync-request:inspect-tap-current-val)))
391+
(nrepl-dbind-response response (value err)
392+
(if value
393+
(message "Successfully tapped the current Inspector value")
394+
(error "Could not tap the current Inspector value: %s" err)))))
406395

407396
(defun cider-inspector-tap-at-point ()
408397
"Sends the current Inspector current sub-value (per POINT) to `tap>'."
409398
(interactive)
410-
;; NOTE: we don't set `cider-inspector--current-repl', because we mean to tap the current value of an existing Inspector,
411-
;; so whatever repl was used for it, should be used here.
412-
(if cider-inspector--current-repl
413-
(seq-let (property value) (cider-inspector-property-at-point)
414-
(pcase property
415-
(`cider-value-idx
416-
(let* ((idx value)
417-
(response (cider-sync-request:inspect-tap-indexed idx)))
418-
(nrepl-dbind-response response (value err)
419-
(if value
420-
(message "Successfully tapped the Inspector item at point")
421-
(error "Could not tap the Inspector item at point: %s" err)))))
422-
(_ (error "No object at point"))))
423-
(user-error "No CIDER session found")))
399+
(seq-let (property value) (cider-inspector-property-at-point)
400+
(pcase property
401+
(`cider-value-idx
402+
(let* ((idx value)
403+
(response (cider-sync-request:inspect-tap-indexed idx)))
404+
(nrepl-dbind-response response (value err)
405+
(if value
406+
(message "Successfully tapped the Inspector item at point")
407+
(error "Could not tap the Inspector item at point: %s" err)))))
408+
(_ (error "No object at point")))))
424409

425410
;; nREPL interactions
426411
(defun cider-sync-request:inspect-pop ()
427412
"Move one level up in the inspector stack."
428413
(cider-nrepl-send-sync-request `("op" "inspect-pop")
429-
cider-inspector--current-repl))
414+
(cider-current-repl)))
430415

431416
(defun cider-sync-request:inspect-push (idx)
432417
"Inspect the inside value specified by IDX."
433418
(cider-nrepl-send-sync-request `("op" "inspect-push"
434419
"idx" ,idx)
435-
cider-inspector--current-repl))
420+
(cider-current-repl)))
436421

437422
(defun cider-sync-request:inspect-previous-sibling ()
438423
"Inspect the previous sibling value within a sequential parent."
439424
(cider-nrepl-send-sync-request `("op" "inspect-previous-sibling")
440-
cider-inspector--current-repl))
425+
(cider-current-repl)))
441426

442427
;;;###autoload
443428
(defun cider-sync-request:inspect-last-exception (index)
444429
"Inspects the exception in the cause stack identified by INDEX."
445430
(cl-assert (numberp index))
446431
(cider-nrepl-send-sync-request `("op" "inspect-last-exception"
447432
"index" ,index)
448-
cider-inspector--current-repl))
433+
(cider-current-repl)))
449434

450435
(defun cider-sync-request:inspect-next-sibling ()
451436
"Inspect the next sibling value within a sequential parent."
452437
(cider-nrepl-send-sync-request `("op" "inspect-next-sibling")
453-
cider-inspector--current-repl))
438+
(cider-current-repl)))
454439

455440
(defun cider-sync-request:inspect-next-page ()
456441
"Jump to the next page in paginated collection view."
457442
(cider-nrepl-send-sync-request '("op" "inspect-next-page")
458-
cider-inspector--current-repl))
443+
(cider-current-repl)))
459444

460445
(defun cider-sync-request:inspect-prev-page ()
461446
"Jump to the previous page in paginated collection view."
462447
(cider-nrepl-send-sync-request '("op" "inspect-prev-page")
463-
cider-inspector--current-repl))
448+
(cider-current-repl)))
464449

465450
(defun cider-sync-request:inspect-set-page-size (page-size)
466451
"Set the page size in paginated view to PAGE-SIZE."
467452
(declare (obsolete "use `inspect-refresh' op instead." "1.15.0"))
468453
(cider-nrepl-send-sync-request `("op" "inspect-set-page-size"
469454
"page-size" ,page-size)
470-
cider-inspector--current-repl))
455+
(cider-current-repl)))
471456

472457
(defun cider-sync-request:inspect-set-max-atom-length (max-length)
473458
"Set the max length of nested atoms to MAX-LENGTH."
474459
(declare (obsolete "use `inspect-refresh' op instead." "1.15.0"))
475460
(cider-nrepl-send-sync-request `("op" "inspect-set-max-atom-length"
476461
"max-atom-length" ,max-length)
477-
cider-inspector--current-repl))
462+
(cider-current-repl)))
478463

479464
(defun cider-sync-request:inspect-set-max-coll-size (max-size)
480465
"Set the maximum number of nested collection members to display to MAX-SIZE."
481466
(declare (obsolete "use `inspect-refresh' op instead." "1.15.0"))
482467
(cider-nrepl-send-sync-request `("op" "inspect-set-max-coll-size"
483468
"max-coll-size" ,max-size)
484-
cider-inspector--current-repl))
469+
(cider-current-repl)))
485470

486471
(defun cider-sync-request:inspect-set-max-nested-depth (max-nested-depth)
487472
"Set the level of nesting to display before truncating to MAX-NESTED-DEPTH."
488473
(declare (obsolete "use `inspect-refresh' op instead." "1.15.0"))
489474
(cider-nrepl-send-sync-request `("op" "inspect-set-max-nested-depth"
490475
"max-nested-depth" ,max-nested-depth)
491-
cider-inspector--current-repl))
476+
(cider-current-repl)))
492477

493478
(defun cider-sync-request:inspect-def-current-val (ns var-name)
494479
"Defines a var with VAR-NAME in NS with the current inspector value."
495480
(cider-nrepl-send-sync-request `("op" "inspect-def-current-value"
496481
"ns" ,ns
497482
"var-name" ,var-name)
498-
cider-inspector--current-repl))
483+
(cider-current-repl)))
499484

500485
(defun cider-sync-request:inspect-tap-current-val ()
501486
"Sends current inspector value to tap>."
502487
(cider-nrepl-send-sync-request '("op" "inspect-tap-current-value")
503-
cider-inspector--current-repl))
488+
(cider-current-repl)))
504489

505490
(defun cider-sync-request:inspect-tap-indexed (idx)
506491
"Sends current inspector sub-value to tap>, per IDX."
507492
(cl-assert idx)
508493
(cider-nrepl-send-sync-request `("op" "inspect-tap-indexed"
509494
"idx" ,idx)
510-
cider-inspector--current-repl))
495+
(cider-current-repl)))
511496

512497
(defun cider-sync-request:inspect-expr (expr ns)
513498
"Evaluate EXPR in context of NS and inspect its result.
@@ -525,7 +510,7 @@ MAX-COLL-SIZE if non nil."
525510
`("max-coll-size" ,cider-inspector-max-coll-size))
526511
,@(when cider-inspector-max-nested-depth
527512
`("max-nested-depth" ,cider-inspector-max-nested-depth))))
528-
(cider-nrepl-send-sync-request cider-inspector--current-repl)))
513+
(cider-nrepl-send-sync-request (cider-current-repl))))
529514

530515
(declare-function cider-set-buffer-ns "cider-mode")
531516

@@ -549,7 +534,8 @@ It can either be a value directly or a inspector response that contains
549534
(local-variable-p 'truncate-lines b)))
550535
(truncate-lines-p (when-let* ((b (get-buffer cider-inspector-buffer))
551536
(continue truncate-lines-defined))
552-
(buffer-local-value 'truncate-lines b))))
537+
(buffer-local-value 'truncate-lines b)))
538+
(repl (cider-current-repl)))
553539
(cider-make-popup-buffer cider-inspector-buffer 'cider-inspector-mode 'ancillary)
554540
(cider-inspector-render cider-inspector-buffer value
555541
:font-size font-size
@@ -559,6 +545,7 @@ It can either be a value directly or a inspector response that contains
559545
(when cider-inspector-fill-frame (delete-other-windows))
560546
(ignore-errors (cider-inspector-next-inspectable-object 1))
561547
(with-current-buffer cider-inspector-buffer
548+
(setq cider--ancillary-buffer-repl repl)
562549
(cider-set-buffer-ns ns)
563550
(when (eq cider-inspector-last-command 'cider-inspector-pop)
564551
(setq cider-inspector-last-command nil)

0 commit comments

Comments
 (0)