Skip to content

Commit 3d21d01

Browse files
committed
[Fix #814] Use cider-current-ns instead of nrepl-buffer-ns
Other changes: - Don't reset `nrepl-buffer-ns` in default response handler. - Remove `cider-find-ns` (it's confusing to have two functions doing almost the same thing). - Use nil global value of `nrepl-buffer-ns`. This will allow easy detection of the cases when this variable shouldn't be used.
1 parent b7ca04e commit 3d21d01

File tree

6 files changed

+27
-57
lines changed

6 files changed

+27
-57
lines changed

cider-client.el

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,7 @@ NS & SESSION specify the context in which to evaluate the request."
9696
;; namespace forms are always evaluated in the "user" namespace
9797
(let ((ns (if (cider-ns-form-p input)
9898
"user"
99-
ns)))
100-
;; prevent forms from being evaluated in the wrong or a non-existing namespace
101-
(when (and ns
102-
(derived-mode-p 'clojure-mode)
103-
(not (string= ns nrepl-buffer-ns))
104-
(not (cider-ns-form-p input)))
105-
(cider-eval-ns-form))
99+
(or ns (cider-current-ns)))))
106100
(nrepl-request:eval input callback ns session)))
107101

108102
(defun cider-tooling-eval (input callback &optional ns)

cider-interaction.el

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ When invoked with a prefix ARG the command doesn't prompt for confirmation."
503503
(defun cider-symbol-at-point ()
504504
"Return the name of the symbol at point, otherwise nil."
505505
(let ((str (substring-no-properties (or (thing-at-point 'symbol) ""))))
506-
(if (equal str (concat (cider-find-ns) "> "))
506+
(if (equal str (concat (cider-current-ns) "> "))
507507
""
508508
str)))
509509

@@ -910,7 +910,6 @@ This is controlled via `cider-interactive-eval-output-destination'."
910910
(lambda (buffer value)
911911
(message "%s" value)
912912
(with-current-buffer buffer
913-
(setq nrepl-buffer-ns (clojure-find-ns))
914913
(run-hooks 'cider-file-loaded-hook)))
915914
(lambda (_buffer value)
916915
(cider-emit-interactive-eval-output value))
@@ -1207,29 +1206,17 @@ If prefix argument KILL-BUFFER-P is non-nil, kill the buffer instead of burying
12071206
(ansi-color-apply-on-region (point-min) (point-max)))
12081207
(goto-char (point-min))))
12091208

1210-
;;; Namespace handling
1211-
(defun cider-find-ns ()
1212-
"Return the ns of the current buffer.
1213-
1214-
For Clojure buffers the ns is extracted from the ns header. If
1215-
it's missing \"user\" is used as fallback."
1216-
(cond
1217-
((derived-mode-p 'clojure-mode)
1218-
(or (save-restriction
1219-
(widen)
1220-
(clojure-find-ns))
1221-
"user"))
1222-
((derived-mode-p 'cider-repl-mode)
1223-
nrepl-buffer-ns)))
1224-
12251209
(defun cider-current-ns ()
1226-
"Return the ns in the current context.
1227-
If `nrepl-buffer-ns' has a value then return that, otherwise
1228-
search for and read a `ns' form."
1229-
(let ((ns nrepl-buffer-ns))
1230-
(or (and (string= ns "user")
1231-
(cider-find-ns))
1232-
ns)))
1210+
"Return current ns.
1211+
The ns is extracted from the ns form. If missing, use current REPL's ns,
1212+
otherwise fall back to \"user\"."
1213+
(if (derived-mode-p 'cider-repl-mode)
1214+
nrepl-buffer-ns
1215+
(or (clojure-find-ns)
1216+
(-when-let (repl-buf (cider-current-repl-buffer))
1217+
(buffer-local-value 'nrepl-buffer-ns (get-buffer repl-buf)))
1218+
nrepl-buffer-ns
1219+
"user")))
12331220

12341221

12351222
;;; Evaluation
@@ -1256,10 +1243,7 @@ START-POS is a starting position of the form in the original context."
12561243
""
12571244
(or (-when-let (form (cider-ns-form))
12581245
(replace-regexp-in-string ":reload\\(-all\\)?\\>" "" form))
1259-
(->> (get-buffer (cider-current-repl-buffer))
1260-
(buffer-local-value 'nrepl-buffer-ns)
1261-
(setq nrepl-buffer-ns)
1262-
(format "(ns %s)")))))
1246+
(format "(ns %s)" (cider-current-ns)))))
12631247
(ns-form-lines (length (split-string ns-form "\n")))
12641248
(start-pos (or start-pos 1))
12651249
(start-line (line-number-at-pos start-pos))
@@ -1423,7 +1407,6 @@ See command `cider-mode'."
14231407
(interactive)
14241408
(dolist (buffer (cider-util--clojure-buffers))
14251409
(with-current-buffer buffer
1426-
(setq nrepl-buffer-ns "user")
14271410
(clojure-disable-cider))))
14281411

14291412
(defun cider-possibly-disable-on-existing-clojure-buffers ()
@@ -1488,8 +1471,7 @@ The result of the completing read will be passed to COMPLETING-READ-CALLBACK."
14881471

14891472
(defun cider-completing-read-sym-form (label form callback)
14901473
"Eval the FORM and pass the result to the response handler."
1491-
(cider-tooling-eval form (cider-completing-read-sym-handler label callback (current-buffer))
1492-
nrepl-buffer-ns))
1474+
(cider-tooling-eval form (cider-completing-read-sym-handler label callback (current-buffer))))
14931475

14941476
(defun cider-completing-read-var (prompt ns callback)
14951477
"Perform completing read var in NS using CALLBACK."
@@ -1513,12 +1495,13 @@ The result of the completing read will be passed to COMPLETING-READ-CALLBACK."
15131495
Once selected, the name of the fn will appear in the repl buffer in parens
15141496
ready to call."
15151497
(interactive)
1516-
(cider-completing-read-sym-form (format "Fn: %s/" nrepl-buffer-ns)
1517-
(cider-fetch-fns-form (cider-current-ns))
1518-
(lambda (f _targets)
1519-
(with-current-buffer (cider-current-repl-buffer)
1520-
(cider-repl--replace-input (format "(%s)" f))
1521-
(goto-char (- (point-max) 1))))))
1498+
(let ((ns (cider-current-ns)))
1499+
(cider-completing-read-sym-form (format "Fn: %s/" ns)
1500+
(cider-fetch-fns-form ns)
1501+
(lambda (f _targets)
1502+
(with-current-buffer (cider-current-repl-buffer)
1503+
(cider-repl--replace-input (format "(%s)" f))
1504+
(goto-char (- (point-max) 1)))))))
15221505

15231506
(defun cider-read-symbol-name (prompt callback &optional query)
15241507
"Either read a symbol name using PROMPT or choose the one at point.
@@ -1531,7 +1514,7 @@ if there is no symbol at point, or if QUERY is non-nil."
15311514
(not symbol-name)
15321515
(equal "" symbol-name)))
15331516
(funcall callback symbol-name)
1534-
(cider-completing-read-var prompt nrepl-buffer-ns callback))))
1517+
(cider-completing-read-var prompt (cider-current-ns) callback))))
15351518

15361519
(defun cider-toggle-trace (query)
15371520
"Toggle tracing for the given QUERY.
@@ -1736,12 +1719,6 @@ strings, include private vars, and be case sensitive."
17361719
"(clojure.core/require 'clojure.tools.namespace.repl) (clojure.tools.namespace.repl/refresh)"
17371720
(cider-interactive-eval-handler (current-buffer))))
17381721

1739-
;; TODO: implement reloading ns
1740-
(defun cider-eval-load-file (form)
1741-
"Load FORM."
1742-
(let ((buffer (current-buffer)))
1743-
(cider-eval form (cider-interactive-eval-handler buffer))))
1744-
17451722
(defun cider-file-string (file)
17461723
"Read the contents of a FILE and return as a string."
17471724
(with-current-buffer (find-file-noselect file)

cider-macroexpansion.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ This variable specifies both what was expanded and the expander.")
8585
(defun cider-macroexpand-again ()
8686
"Repeat the last macroexpansion."
8787
(interactive)
88-
(cider-initialize-macroexpansion-buffer cider-last-macroexpand-expression nrepl-buffer-ns))
88+
(cider-initialize-macroexpansion-buffer cider-last-macroexpand-expression (cider-current-ns)))
8989

9090
;;;###autoload
9191
(defun cider-macroexpand-1 (&optional prefix)

cider-repl.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ If NEWLINE is true then add a newline at the end of the input."
551551
(goto-char (point-max))
552552
(cider-repl--mark-input-start)
553553
(cider-repl--mark-output-start)
554-
(cider-eval form (cider-repl-handler (current-buffer)) nrepl-buffer-ns)))
554+
(cider-eval form (cider-repl-handler (current-buffer)))))
555555

556556
(defun cider-repl-return (&optional end-of-input)
557557
"Evaluate the current input string, or insert a newline.

nrepl-client.el

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ To be used for tooling calls (i.e. completion, eldoc, etc)")
214214

215215
(defvar-local nrepl-completed-requests nil)
216216

217-
(defvar-local nrepl-buffer-ns "user"
217+
(defvar-local nrepl-buffer-ns nil
218218
"Current Clojure namespace of this buffer.")
219219

220220
(defvar-local nrepl-last-sync-response nil
@@ -624,6 +624,7 @@ the newly created client connection process."
624624
;; FIXME: REPL and connection buffers are the same thing
625625
nrepl-connection-buffer client-buf
626626
nrepl-repl-buffer (when replp client-buf)
627+
nrepl-buffer-ns "user"
627628
nrepl-tunnel-buffer (and tunnel-proc (process-buffer tunnel-proc))
628629
nrepl-pending-requests (make-hash-table :test 'equal)
629630
nrepl-completed-requests (make-hash-table :test 'equal)))
@@ -711,8 +712,6 @@ server responses."
711712
(nrepl-dbind-response response (value ns out err status id ex root-ex
712713
session)
713714
(cond (value
714-
(with-current-buffer buffer
715-
(when ns (setq nrepl-buffer-ns ns)))
716715
(when value-handler
717716
(funcall value-handler buffer value)))
718717
(out

test/cider-tests.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@
530530

531531
(ert-deftest cider-symbol-at-point-at-repl-prompt ()
532532
(noflet ((thing-at-point (thing) "user> ")
533-
(cider-find-ns () "user"))
533+
(cider-current-ns () "user"))
534534
(should (string= (cider-symbol-at-point) ""))))
535535

536536
(ert-deftest test-cider--url-to-file ()

0 commit comments

Comments
 (0)