Skip to content

Commit 0d3db73

Browse files
committed
Merge pull request #817 from vitoshka/buffer-ns
[Fix #814] Use `cider-current-ns` instead of `nrepl-buffer-ns`
2 parents 34ba845 + 3d21d01 commit 0d3db73

File tree

6 files changed

+37
-67
lines changed

6 files changed

+37
-67
lines changed

cider-client.el

Lines changed: 3 additions & 10 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)
@@ -121,9 +115,8 @@ NS specifies the namespace in which to evaluate the request."
121115

122116
(defun cider-current-repl-buffer ()
123117
"The current REPL buffer."
124-
(when (nrepl-current-connection-buffer)
125-
(buffer-local-value 'nrepl-repl-buffer
126-
(get-buffer (nrepl-current-connection-buffer)))))
118+
(-when-let (repl-buf (nrepl-current-connection-buffer 'no-error))
119+
(buffer-local-value 'nrepl-repl-buffer (get-buffer repl-buf))))
127120

128121
(defun cider--var-choice (var-info)
129122
"Prompt to choose from among multiple VAR-INFO candidates, if required.

cider-interaction.el

Lines changed: 22 additions & 47 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))
@@ -1400,9 +1384,7 @@ Useful in hooks."
14001384

14011385
(defun cider-connected-p ()
14021386
"Return t if CIDER is currently connected, nil otherwise."
1403-
(condition-case nil
1404-
(nrepl-current-connection-buffer)
1405-
(error nil)))
1387+
(nrepl-current-connection-buffer 'no-error))
14061388

14071389
(defun cider-ensure-connected ()
14081390
"Ensure there is a cider connection present, otherwise
@@ -1425,7 +1407,6 @@ See command `cider-mode'."
14251407
(interactive)
14261408
(dolist (buffer (cider-util--clojure-buffers))
14271409
(with-current-buffer buffer
1428-
(setq nrepl-buffer-ns "user")
14291410
(clojure-disable-cider))))
14301411

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

14911472
(defun cider-completing-read-sym-form (label form callback)
14921473
"Eval the FORM and pass the result to the response handler."
1493-
(cider-tooling-eval form (cider-completing-read-sym-handler label callback (current-buffer))
1494-
nrepl-buffer-ns))
1474+
(cider-tooling-eval form (cider-completing-read-sym-handler label callback (current-buffer))))
14951475

14961476
(defun cider-completing-read-var (prompt ns callback)
14971477
"Perform completing read var in NS using CALLBACK."
@@ -1515,12 +1495,13 @@ The result of the completing read will be passed to COMPLETING-READ-CALLBACK."
15151495
Once selected, the name of the fn will appear in the repl buffer in parens
15161496
ready to call."
15171497
(interactive)
1518-
(cider-completing-read-sym-form (format "Fn: %s/" nrepl-buffer-ns)
1519-
(cider-fetch-fns-form (cider-current-ns))
1520-
(lambda (f _targets)
1521-
(with-current-buffer (cider-current-repl-buffer)
1522-
(cider-repl--replace-input (format "(%s)" f))
1523-
(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)))))))
15241505

15251506
(defun cider-read-symbol-name (prompt callback &optional query)
15261507
"Either read a symbol name using PROMPT or choose the one at point.
@@ -1533,7 +1514,7 @@ if there is no symbol at point, or if QUERY is non-nil."
15331514
(not symbol-name)
15341515
(equal "" symbol-name)))
15351516
(funcall callback symbol-name)
1536-
(cider-completing-read-var prompt nrepl-buffer-ns callback))))
1517+
(cider-completing-read-var prompt (cider-current-ns) callback))))
15371518

15381519
(defun cider-toggle-trace (query)
15391520
"Toggle tracing for the given QUERY.
@@ -1738,12 +1719,6 @@ strings, include private vars, and be case sensitive."
17381719
"(clojure.core/require 'clojure.tools.namespace.repl) (clojure.tools.namespace.repl/refresh)"
17391720
(cider-interactive-eval-handler (current-buffer))))
17401721

1741-
;; TODO: implement reloading ns
1742-
(defun cider-eval-load-file (form)
1743-
"Load FORM."
1744-
(let ((buffer (current-buffer)))
1745-
(cider-eval form (cider-interactive-eval-handler buffer))))
1746-
17471722
(defun cider-file-string (file)
17481723
"Read the contents of a FILE and return as a string."
17491724
(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ joined together.")
158158
(defun cider-repl-buffer-name (&optional project-dir host port)
159159
"Generate a REPL buffer name based on current connection buffer.
160160
PROJECT-DIR, PORT and HOST are as in `nrepl-make-buffer-name'."
161-
(with-current-buffer (or (get-buffer (nrepl-current-connection-buffer))
161+
(with-current-buffer (or (nrepl-current-connection-buffer 'no-error)
162162
(current-buffer))
163163
(nrepl-make-buffer-name nrepl-repl-buffer-name-template project-dir host port)))
164164

@@ -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: 8 additions & 6 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
@@ -1061,12 +1060,15 @@ PROJECT-DIR, HOST and PORT are as in `nrepl-make-buffer-name'."
10611060
(setq-local kill-buffer-query-functions nil))
10621061
buffer))
10631062

1064-
(defun nrepl-current-connection-buffer ()
1065-
"The connection to use for nREPL interaction."
1063+
(defun nrepl-current-connection-buffer (&optional no-error)
1064+
"The connection to use for nREPL interaction.
1065+
When NO-ERROR is non-nil, don't throw an error when no connection has been
1066+
found."
10661067
(or nrepl-connection-dispatch
10671068
nrepl-connection-buffer
10681069
(car (nrepl-connection-buffers))
1069-
(error "No nREPL connection buffer")))
1070+
(unless no-error
1071+
(error "No nREPL connection buffer"))))
10701072

10711073
(defun nrepl-connection-buffers ()
10721074
"Return the connection list.

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)