Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions cider-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ NS & SESSION specify the context in which to evaluate the request."
;; namespace forms are always evaluated in the "user" namespace
(let ((ns (if (cider-ns-form-p input)
"user"
ns)))
;; prevent forms from being evaluated in the wrong or a non-existing namespace
(when (and ns
(derived-mode-p 'clojure-mode)
(not (string= ns nrepl-buffer-ns))
(not (cider-ns-form-p input)))
(cider-eval-ns-form))
(or ns (cider-current-ns)))))
(nrepl-request:eval input callback ns session)))

(defun cider-tooling-eval (input callback &optional ns)
Expand All @@ -121,9 +115,8 @@ NS specifies the namespace in which to evaluate the request."

(defun cider-current-repl-buffer ()
"The current REPL buffer."
(when (nrepl-current-connection-buffer)
(buffer-local-value 'nrepl-repl-buffer
(get-buffer (nrepl-current-connection-buffer)))))
(-when-let (repl-buf (nrepl-current-connection-buffer 'no-error))
(buffer-local-value 'nrepl-repl-buffer (get-buffer repl-buf))))

(defun cider--var-choice (var-info)
"Prompt to choose from among multiple VAR-INFO candidates, if required.
Expand Down
69 changes: 22 additions & 47 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ When invoked with a prefix ARG the command doesn't prompt for confirmation."
(defun cider-symbol-at-point ()
"Return the name of the symbol at point, otherwise nil."
(let ((str (substring-no-properties (or (thing-at-point 'symbol) ""))))
(if (equal str (concat (cider-find-ns) "> "))
(if (equal str (concat (cider-current-ns) "> "))
""
str)))

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

;;; Namespace handling
(defun cider-find-ns ()
"Return the ns of the current buffer.

For Clojure buffers the ns is extracted from the ns header. If
it's missing \"user\" is used as fallback."
(cond
((derived-mode-p 'clojure-mode)
(or (save-restriction
(widen)
(clojure-find-ns))
"user"))
((derived-mode-p 'cider-repl-mode)
nrepl-buffer-ns)))

(defun cider-current-ns ()
"Return the ns in the current context.
If `nrepl-buffer-ns' has a value then return that, otherwise
search for and read a `ns' form."
(let ((ns nrepl-buffer-ns))
(or (and (string= ns "user")
(cider-find-ns))
ns)))
"Return current ns.
The ns is extracted from the ns form. If missing, use current REPL's ns,
otherwise fall back to \"user\"."
(if (derived-mode-p 'cider-repl-mode)
nrepl-buffer-ns
(or (clojure-find-ns)
(-when-let (repl-buf (cider-current-repl-buffer))
(buffer-local-value 'nrepl-buffer-ns (get-buffer repl-buf)))
nrepl-buffer-ns
"user")))


;;; Evaluation
Expand All @@ -1256,10 +1243,7 @@ START-POS is a starting position of the form in the original context."
""
(or (-when-let (form (cider-ns-form))
(replace-regexp-in-string ":reload\\(-all\\)?\\>" "" form))
(->> (get-buffer (cider-current-repl-buffer))
(buffer-local-value 'nrepl-buffer-ns)
(setq nrepl-buffer-ns)
(format "(ns %s)")))))
(format "(ns %s)" (cider-current-ns)))))
(ns-form-lines (length (split-string ns-form "\n")))
(start-pos (or start-pos 1))
(start-line (line-number-at-pos start-pos))
Expand Down Expand Up @@ -1400,9 +1384,7 @@ Useful in hooks."

(defun cider-connected-p ()
"Return t if CIDER is currently connected, nil otherwise."
(condition-case nil
(nrepl-current-connection-buffer)
(error nil)))
(nrepl-current-connection-buffer 'no-error))

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

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

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

(defun cider-completing-read-var (prompt ns callback)
"Perform completing read var in NS using CALLBACK."
Expand All @@ -1515,12 +1495,13 @@ The result of the completing read will be passed to COMPLETING-READ-CALLBACK."
Once selected, the name of the fn will appear in the repl buffer in parens
ready to call."
(interactive)
(cider-completing-read-sym-form (format "Fn: %s/" nrepl-buffer-ns)
(cider-fetch-fns-form (cider-current-ns))
(lambda (f _targets)
(with-current-buffer (cider-current-repl-buffer)
(cider-repl--replace-input (format "(%s)" f))
(goto-char (- (point-max) 1))))))
(let ((ns (cider-current-ns)))
(cider-completing-read-sym-form (format "Fn: %s/" ns)
(cider-fetch-fns-form ns)
(lambda (f _targets)
(with-current-buffer (cider-current-repl-buffer)
(cider-repl--replace-input (format "(%s)" f))
(goto-char (- (point-max) 1)))))))

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

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

;; TODO: implement reloading ns
(defun cider-eval-load-file (form)
"Load FORM."
(let ((buffer (current-buffer)))
(cider-eval form (cider-interactive-eval-handler buffer))))

(defun cider-file-string (file)
"Read the contents of a FILE and return as a string."
(with-current-buffer (find-file-noselect file)
Expand Down
2 changes: 1 addition & 1 deletion cider-macroexpansion.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ This variable specifies both what was expanded and the expander.")
(defun cider-macroexpand-again ()
"Repeat the last macroexpansion."
(interactive)
(cider-initialize-macroexpansion-buffer cider-last-macroexpand-expression nrepl-buffer-ns))
(cider-initialize-macroexpansion-buffer cider-last-macroexpand-expression (cider-current-ns)))

;;;###autoload
(defun cider-macroexpand-1 (&optional prefix)
Expand Down
4 changes: 2 additions & 2 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ joined together.")
(defun cider-repl-buffer-name (&optional project-dir host port)
"Generate a REPL buffer name based on current connection buffer.
PROJECT-DIR, PORT and HOST are as in `nrepl-make-buffer-name'."
(with-current-buffer (or (get-buffer (nrepl-current-connection-buffer))
(with-current-buffer (or (nrepl-current-connection-buffer 'no-error)
(current-buffer))
(nrepl-make-buffer-name nrepl-repl-buffer-name-template project-dir host port)))

Expand Down Expand Up @@ -551,7 +551,7 @@ If NEWLINE is true then add a newline at the end of the input."
(goto-char (point-max))
(cider-repl--mark-input-start)
(cider-repl--mark-output-start)
(cider-eval form (cider-repl-handler (current-buffer)) nrepl-buffer-ns)))
(cider-eval form (cider-repl-handler (current-buffer)))))

(defun cider-repl-return (&optional end-of-input)
"Evaluate the current input string, or insert a newline.
Expand Down
14 changes: 8 additions & 6 deletions nrepl-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ To be used for tooling calls (i.e. completion, eldoc, etc)")

(defvar-local nrepl-completed-requests nil)

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

(defvar-local nrepl-last-sync-response nil
Expand Down Expand Up @@ -624,6 +624,7 @@ the newly created client connection process."
;; FIXME: REPL and connection buffers are the same thing
nrepl-connection-buffer client-buf
nrepl-repl-buffer (when replp client-buf)
nrepl-buffer-ns "user"
nrepl-tunnel-buffer (and tunnel-proc (process-buffer tunnel-proc))
nrepl-pending-requests (make-hash-table :test 'equal)
nrepl-completed-requests (make-hash-table :test 'equal)))
Expand Down Expand Up @@ -711,8 +712,6 @@ server responses."
(nrepl-dbind-response response (value ns out err status id ex root-ex
session)
(cond (value
(with-current-buffer buffer
(when ns (setq nrepl-buffer-ns ns)))
(when value-handler
(funcall value-handler buffer value)))
(out
Expand Down Expand Up @@ -1061,12 +1060,15 @@ PROJECT-DIR, HOST and PORT are as in `nrepl-make-buffer-name'."
(setq-local kill-buffer-query-functions nil))
buffer))

(defun nrepl-current-connection-buffer ()
"The connection to use for nREPL interaction."
(defun nrepl-current-connection-buffer (&optional no-error)
"The connection to use for nREPL interaction.
When NO-ERROR is non-nil, don't throw an error when no connection has been
found."
(or nrepl-connection-dispatch
nrepl-connection-buffer
(car (nrepl-connection-buffers))
(error "No nREPL connection buffer")))
(unless no-error
(error "No nREPL connection buffer"))))

(defun nrepl-connection-buffers ()
"Return the connection list.
Expand Down
2 changes: 1 addition & 1 deletion test/cider-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@

(ert-deftest cider-symbol-at-point-at-repl-prompt ()
(noflet ((thing-at-point (thing) "user> ")
(cider-find-ns () "user"))
(cider-current-ns () "user"))
(should (string= (cider-symbol-at-point) ""))))

(ert-deftest test-cider--url-to-file ()
Expand Down