Skip to content

Commit

Permalink
Removing dynamic vars for symbol loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpither authored and gtrak committed Mar 18, 2014
1 parent c97df52 commit d4018ac
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,6 @@ See command `cider-mode'."
(unless (cider-connected-p)
(cider-disable-on-existing-clojure-buffers)))

;; this is horrible, but with async callbacks we can't rely on dynamic scope
(defvar cider-ido-ns nil)
(defvar cider-ido-var-callback nil)

(defun cider-ido-form (ns)
"Construct a Clojure form for ido read using NS."
`(concat (if (find-ns (symbol ,ns))
Expand All @@ -1197,40 +1193,40 @@ See command `cider-mode'."
"Perform up using NS."
(mapconcat 'identity (butlast (split-string ns "\\.")) "."))

(defun cider-ido-var-select (selected targets)
(defun cider-ido-var-select (ido-callback cider-ido-ns selected targets)
"Peform ido select using SELECTED and TARGETS."
;; TODO: immediate RET gives "" as selected for some reason
;; this is an OK workaround though
(cond ((equal "" selected)
(cider-ido-var-select (car targets) targets))
(cider-ido-var-select ido-callback cider-ido-ns (car targets) targets))
((equal "/" (substring selected -1)) ; selected a namespace
(cider-ido-read-var (substring selected 0 -1) cider-ido-var-callback))
(cider-ido-read-var (substring selected 0 -1) ido-callback))
((equal ".." selected)
(cider-ido-read-var (cider-ido-up-ns cider-ido-ns) cider-ido-var-callback))
(cider-ido-read-var (cider-ido-up-ns cider-ido-ns) ido-callback))
;; non ido variable selection techniques don't return qualified symbols, so this shouldn't either
(t (funcall cider-ido-var-callback selected))))
(t (funcall ido-callback selected))))

(defun cider-ido-read-sym-handler (label ido-select buffer)
"Create an ido read var handler with IDO-SELECT for BUFFER."
(let ((ido-select ido-select)
(label label))
(nrepl-make-response-handler buffer
(lambda (buffer value)
;; make sure to eval the callback in the buffer that the symbol was requested from so we get the right namespace
(with-current-buffer buffer
(let* ((targets (car (read-from-string value)))
(selected (ido-completing-read label targets nil t)))
(funcall ido-select selected targets))))
nil nil nil)))
(nrepl-make-response-handler buffer
(lambda (buffer value)
;; make sure to eval the callback in the buffer that the symbol was requested from so we get the right namespace
(with-current-buffer buffer
(let* ((targets (car (read-from-string value)))
(selected (ido-completing-read label targets nil t)))
(funcall ido-select selected targets))))
nil nil nil))

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

(defun cider-ido-read-var (ns ido-callback)
"Perform ido read var in NS using IDO-CALLBACK."
;; Have to be stateful =(
(setq cider-ido-ns ns)
(setq cider-ido-var-callback ido-callback)
(cider-tooling-eval (prin1-to-string (cider-ido-form cider-ido-ns))
(cider-ido-read-sym-handler "Var: " 'cider-ido-var-select (current-buffer))
nrepl-buffer-ns))
(cider-ido-read-sym-form "Var: " (prin1-to-string (cider-ido-form ns))
(lambda (selected targets)
(cider-ido-var-select ido-callback ns selected targets))))

(defun cider-ido-fns-form (ns)
"Construct a Clojure form for reading fns using supplied NS."
Expand All @@ -1243,20 +1239,17 @@ See command `cider-mode'."
(ns-interns '%s)
(ns-refers '%s))))))" ns ns))

(defun cider-ido-fn-callback (f targets)
(with-current-buffer (cider-current-repl-buffer)
(cider-repl--replace-input (format "(%s)" f))
(goto-char (- (point-max) 1))))

(defun cider-load-fn-into-repl-buffer ()
"Browse functions available in current repl buffer using ido.
Once selected, the name of the fn will appear in the repl buffer in parens
ready to call."
(interactive)
(cider-tooling-eval (cider-ido-fns-form (cider-current-ns))
(cider-ido-read-sym-handler (format "Fn: %s/" nrepl-buffer-ns)
'cider-ido-fn-callback (current-buffer))
nrepl-buffer-ns))
(cider-ido-read-sym-form (format "Fn: %s/" nrepl-buffer-ns)
(cider-ido-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))))))

(defun cider-read-symbol-name (prompt callback &optional query)
"Either read a symbol name using PROMPT or choose the one at point.
Expand Down

0 comments on commit d4018ac

Please sign in to comment.