Skip to content

Commit bd30ad7

Browse files
committed
Enhance ns form caching
Now we cache the ns form in a hash table, which makes it possible to have separate value for each connection. After this change the caching will work properly no matter how many connections we're using to work with the same source file.
1 parent 5029755 commit bd30ad7

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

cider-interaction.el

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,8 +1571,22 @@ otherwise fall back to \"user\"."
15711571
#'identity)
15721572
"Function to translate Emacs filenames to nREPL namestrings.")
15731573

1574-
(defvar-local cider--cached-ns-form nil
1575-
"Cached ns form in the current buffer.")
1574+
(defvar-local cider--ns-form-cache (make-hash-table :test 'equal)
1575+
"ns form cache for the current buffer.
1576+
1577+
The cache is a hash where the keys are connection names and the values
1578+
are ns forms. This allows every connection to keep track of the ns
1579+
form independently.")
1580+
1581+
(defun cider--cache-ns-form ()
1582+
"Cache the form in the current buffer for the current connection."
1583+
(puthash (nrepl-current-connection-buffer)
1584+
(cider-ns-form)
1585+
cider--ns-form-cache))
1586+
1587+
(defun cider--cached-ns-form ()
1588+
"Retrieve the cached ns form for the current buffer & connection."
1589+
(gethash (nrepl-current-connection-buffer) cider--ns-form-cache))
15761590

15771591
(defun cider--prep-interactive-eval (form)
15781592
"Prepares the environment for an interactive eval of FORM.
@@ -1586,12 +1600,12 @@ Clears any compilation highlights and kills the error window."
15861600
(cider--quit-error-window)
15871601
(let ((cur-ns-form (cider-ns-form)))
15881602
(when (and cur-ns-form
1589-
(not (string= cur-ns-form cider--cached-ns-form))
1603+
(not (string= cur-ns-form (cider--cached-ns-form)))
15901604
(not (cider-ns-form-p form)))
15911605
;; this should probably be done synchronously
15921606
;; otherwise an errors in the ns form will go unnoticed
15931607
(cider-eval-ns-form)
1594-
(setq cider--cached-ns-form cur-ns-form))))
1608+
(cider--cache-ns-form))))
15951609

15961610
(defun cider-interactive-eval (form &optional callback)
15971611
"Evaluate FORM and dispatch the response to CALLBACK.
@@ -1937,7 +1951,7 @@ unconditionally."
19371951
(buffer-file-name))))))
19381952
(cider--clear-compilation-highlights)
19391953
(cider--quit-error-window)
1940-
(setq cider--cached-ns-form (cider-ns-form))
1954+
(cider--cache-ns-form)
19411955
(cider-request:load-file
19421956
(cider-file-string filename)
19431957
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
@@ -2102,7 +2116,7 @@ Quitting closes all active nREPL connections and kills all CIDER buffers."
21022116
;; which connection we simply clean the cache for all buffers
21032117
(dolist (clojure-buffer (cider-util--clojure-buffers))
21042118
(with-current-buffer clojure-buffer
2105-
(setq cider--cached-ns-form nil)))))
2119+
(setq cider--ns-form-cache (make-hash-table :test 'equal))))))
21062120

21072121
(defun cider-restart (&optional prompt-project)
21082122
"Quit CIDER and restart it.

0 commit comments

Comments
 (0)