Skip to content

Commit 91a0b38

Browse files
vspinubbatsov
authored andcommitted
Use plist to cache ns->ns-form mappings
1 parent ce913e8 commit 91a0b38

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

cider-client.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ EVAL-BUFFER is the buffer where the spinner was started."
596596
"Check if FORM is an ns form."
597597
(string-match-p "^[[:space:]]*\(ns\\([[:space:]]*$\\|[[:space:]]+\\)" form))
598598

599+
(defun cider-ns-from-form (ns-form)
600+
"Get ns substring from NS-FORM."
601+
(when (string-match "^[ \t\n]*\(ns[ \t\n]+\\([^][ \t\n(){}]+\\)" ns-form)
602+
(match-string-no-properties 1 ns-form)))
603+
599604
(defvar-local cider-buffer-ns nil
600605
"Current Clojure namespace of some buffer.
601606

cider-interaction.el

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,9 +1065,6 @@ evaluation command. Honor `cider-auto-jump-to-error'."
10651065
#'identity))
10661066
"Function to translate Emacs filenames to nREPL namestrings.")
10671067

1068-
(defvar-local cider--last-ns-form nil
1069-
"Ns-form of the most recent interactive evaluation.")
1070-
10711068
(defun cider--prep-interactive-eval (form connection)
10721069
"Prepare the environment for an interactive eval of FORM in CONNECTION.
10731070
Ensure the current ns declaration has been evaluated (so that the ns
@@ -1078,19 +1075,16 @@ window."
10781075
(cider--quit-error-window)
10791076
(let ((cur-ns-form (cider-ns-form)))
10801077
(when (and cur-ns-form
1081-
(not (string= cur-ns-form
1082-
(buffer-local-value 'cider--last-ns-form connection)))
1083-
(not (cider-ns-form-p form)))
1084-
(cider-repl--cache-ns-roots cur-ns-form connection)
1078+
(not (cider-ns-form-p form))
1079+
(cider-repl--ns-form-changed-p cur-ns-form connection))
10851080
(when cider-auto-track-ns-form-changes
10861081
;; The first interactive eval on a file can load a lot of libs. This can
10871082
;; easily lead to more than 10 sec.
10881083
(let ((nrepl-sync-request-timeout 30))
10891084
;; TODO: check for evaluation errors
10901085
(cider-nrepl-sync-request:eval cur-ns-form connection)))
10911086
;; cache at the end, in case of errors
1092-
(with-current-buffer connection
1093-
(setq cider--last-ns-form cur-ns-form)))))
1087+
(cider-repl--cache-ns-form cur-ns-form connection))))
10941088

10951089
(defvar-local cider-interactive-eval-override nil
10961090
"Function to call instead of `cider-interactive-eval'.")
@@ -1611,9 +1605,7 @@ ClojureScript REPL exists for the project, it is evaluated in both REPLs."
16111605
(ns-form (cider-ns-form)))
16121606
(cider-map-connections
16131607
(lambda (connection)
1614-
(with-current-buffer connection
1615-
(setq cider--last-ns-form ns-form))
1616-
(cider-repl--cache-ns-roots ns-form connection)
1608+
(cider-repl--cache-ns-form ns-form connection)
16171609
(cider-request:load-file (cider-file-string filename)
16181610
(funcall cider-to-nrepl-filename-function
16191611
(cider--server-filename filename))

cider-repl.el

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,18 @@ When there is a possible unfinished ansi control sequence,
550550
(insert-before-markers (cadr ansi-color-context))
551551
(setq ansi-color-context nil)))
552552

553-
(defvar cider-repl--root-ns-highlitht-template "\\<\\(%s\\)[^$/: \t\n]+"
553+
(defvar-local cider-repl--ns-forms-plist nil
554+
"Plist holding ns->ns-form mappings within each connection.")
555+
556+
(defun cider-repl--ns-form-changed-p (ns-form connection)
557+
"Return non-nil if NS-FORM for CONNECTION changed since last eval."
558+
(when-let ((ns (cider-ns-from-form ns-form)))
559+
(not (string= ns-form
560+
(lax-plist-get
561+
(buffer-local-value 'cider-repl--ns-forms-plist connection)
562+
ns)))))
563+
564+
(defvar cider-repl--root-ns-highlight-template "\\<\\(%s\\)[^$/: \t\n]+"
554565
"Regexp used to highlight root ns in REPL buffers.")
555566

556567
(defvar-local cider-repl--root-ns-regexp nil
@@ -559,19 +570,24 @@ When there is a possible unfinished ansi control sequence,
559570
(defvar-local cider-repl--ns-roots nil
560571
"List holding all past root namespaces seen during interactive eval.")
561572

562-
(defun cider-repl--cache-ns-roots (ns-form connection)
573+
(defun cider-repl--cache-ns-form (ns-form connection)
563574
"Given NS-FORM cache root ns in CONNECTION."
564575
(with-current-buffer connection
565-
(when (string-match "^[ \t\n]*\(ns[ \t\n]+\\([^. \t\n]+\\)" ns-form)
566-
(let ((root (match-string-no-properties 1 ns-form)))
567-
(unless (member root cider-repl--ns-roots)
568-
(push root cider-repl--ns-roots)
569-
(let ((roots (mapconcat
570-
;; Replace _ or - with regexp patter to accommodate "raw" namespaces
571-
(lambda (r) (replace-regexp-in-string "[_-]+" "[_-]+" r))
572-
cider-repl--ns-roots "\\|")))
573-
(setq cider-repl--root-ns-regexp
574-
(format cider-repl--root-ns-highlitht-template roots))))))))
576+
(when-let ((ns (cider-ns-from-form ns-form)))
577+
;; cache ns-form
578+
(setq cider-repl--ns-forms-plist
579+
(lax-plist-put cider-repl--ns-forms-plist ns ns-form))
580+
;; cache ns roots regexp
581+
(when (string-match "\\([^.]+\\)" ns)
582+
(let ((root (match-string-no-properties 1 ns)))
583+
(unless (member root cider-repl--ns-roots)
584+
(push root cider-repl--ns-roots)
585+
(let ((roots (mapconcat
586+
;; Replace _ or - with regexp patter to accommodate "raw" namespaces
587+
(lambda (r) (replace-regexp-in-string "[_-]+" "[_-]+" r))
588+
cider-repl--ns-roots "\\|")))
589+
(setq cider-repl--root-ns-regexp
590+
(format cider-repl--root-ns-highlight-template roots)))))))))
575591

576592
(defun cider-repl--apply-current-project-color (string)
577593
"Fontify project's root namespace to make stacktraces more readable.

0 commit comments

Comments
 (0)