Skip to content

Commit

Permalink
[Fix clojure-emacs#1014] Jump-to other window with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGebhart committed Apr 4, 2015
1 parent 699cbec commit 0992654
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
* [#1032](https://github.com/clojure-emacs/cider/issues/1032) New functions, `cider-find-dwim` and
`cider-find-dwim-other-window`. These functions combine the functionality of `cider-jump-to-var` and
`cider-jump-to-resource`. Which are now renamed to `cider-find-var` and `cider-find-resource` respectively.

* [#1014](https://github.com/clojure-emacs/cider/issues/1014) A prefix of <kbd>M-0</kbd> or
<kbd>M--</kbd> causes `cider-find-var` and `cider-find-resource` to show results in other window.
Additionally, <kbd>M--</kbd> also inverts the meaning of `cider-prompt-for-symbol`.

### Changes

Expand Down
78 changes: 64 additions & 14 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -800,27 +800,69 @@ If thing at point is empty dired on project."
(-if-let* ((resource (cider-sync-request:resource symbol-file))
(buffer (cider-find-file resource)))
(cider-jump-to buffer 0 other-window)
(if (cider--should-prompt-for-symbol current-prefix-arg)
(if (cider--prompt-for-symbol? current-prefix-arg)
(error "Resource or var %s not resolved" symbol-file)
(let ((current-prefix-arg (if current-prefix-arg nil '(4))))
(call-interactively callback)))))))

(defun cider--find-dwim-interactive (prompt)
"Get interactive arguments for jump-to functions using PROMPT as needed."
(if (cider--should-prompt-for-symbol current-prefix-arg)
(if (cider--prompt-for-symbol? current-prefix-arg)
(list
(cider-read-from-minibuffer prompt (thing-at-point 'filename)))
(list (or (thing-at-point 'filename) "")))) ; No prompt.

(defun cider-find-resource (path)
"Jump to the resource at the resource-relative PATH.
When called interactively, this operates on point."
(interactive (list (thing-at-point 'filename)))
"Find resource at PATH, when called interactively,
a prompt is given according to the variable `cider-prompt-for-symbol`.
A `C-u` or `M--` prefix inverts the meaning. A prefix of `M--` or `M-0` causes
the results to be displayed in other window. The default value is thing at point. "
(interactive
(if (cider--prompt-for-symbol? current-prefix-arg)
(list
(cider-read-from-minibuffer "Resource: " (thing-at-point 'filename)))
(list (or (thing-at-point 'filename) ""))))

(cider-ensure-op-supported "resource")
(-if-let* ((resource (cider-sync-request:resource path))
(buffer (cider-find-file resource)))
(cider-jump-to buffer)
(error "Cannot find resource %s" path)))
(cider-jump-to buffer nil (cider--open-other-window? current-prefix-arg))
(if (cider--prompt-for-symbol? current-prefix-arg)
(error "Cannot find resource %s" path)
(let ((current-prefix-arg (cider--invert-prefix-arg current-prefix-arg)))
(call-interactively `cider-find-resource)))))

(defun cider--open-other-window? (arg)
"Test ARG to see if it indicates displaying results in other window."
(let ((narg (prefix-numeric-value arg)))
(cond
((= narg -1) t) ;; M--
((= narg 0) t) ;; M-0
(t nil))))

(defun cider--invert-prefix-arg (arg)
"Invert the meaning of ARG as far as it's effect on the behavior set by
`cider-prompt-for-symbol` while preserving the other-window meaning of ARG."
(let ((narg (prefix-numeric-value arg)))
(cond
((= narg -1) 0) ;; M-- -> M-0
((= narg 0) -1) ;; M-0 -> M--
((= narg 4) nil) ;; C-u -> no-prefix
((= narg nil) 4)))) ;; no-prefix -> C-u

(defun cider--prefix-invert-prompt? (arg)
"Test ARG to see if it should invert the meaning of `cider-prompt-for-symbol`."
(let ((narg (prefix-numeric-value arg)))
(cond
((= narg -1) t) ;; M--
((= narg 4) t) ;; C-u
(t nil))))

(defun cider--prompt-for-symbol? (&optional prefix)
"Should cider prompt or not. Invert meaning of cider-prompt-for-symbol if PREFIX
indicates it should be."
(if (cider--prefix-invert-prompt? prefix)
(not cider-prompt-for-symbol) cider-prompt-for-symbol))

(defun cider--jump-to-loc-from-info (info &optional other-window)
"Jump to location give by INFO.
Expand All @@ -835,6 +877,14 @@ OTHER-WINDOW is passed to `cider-jamp-to'."
(cider-jump-to buffer (cons line nil) other-window)
(error "No source location"))))

(defun cider--find-var-other-window (var &optional line)
"Jump to the definition of VAR, optionally at a specific LINE."
(-if-let (info (cider-var-info var))
(progn
(if line (setq info (nrepl-dict-put info "line" line)))
(cider--jump-to-loc-from-info info t))
(error "Symbol %s not resolved" var)))

(defun cider--find-var (var &optional line)
"Jump to the definition of VAR, optionally at a specific LINE."
(-if-let (info (cider-var-info var))
Expand All @@ -846,15 +896,18 @@ OTHER-WINDOW is passed to `cider-jamp-to'."
(defun cider-find-var (&optional arg var line)
"Jump to the definition of VAR, optionally at a specific LINE.
Prompts for the symbol to use, or uses the symbol at point, depending on
the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
opposite of what that option dictates."
the value of `cider-prompt-for-symbol'. A `C-u` or `M--` prefix inverts the meaning.
A prefix of `M--` or `M-0` causes the results to be displayed in other window. The
default value is thing at point."
(interactive "P")
(cider-ensure-op-supported "info")
(if var
(cider--find-var var line)
(funcall (cider-prompt-for-symbol-function arg)
"Symbol: "
#'cider--find-var)))
(if (cider--open-other-window? arg)
#'cider--find-var-other-window
#'cider--find-var))))

(define-obsolete-function-alias 'cider-jump-to-resource 'cider-find-resource "0.9.0")
(define-obsolete-function-alias 'cider-jump-to-var 'cider-find-var "0.9.0")
Expand Down Expand Up @@ -1855,11 +1908,8 @@ On failure, read a symbol name using PROMPT and call CALLBACK with that."
(condition-case nil (funcall callback (cider-symbol-at-point))
('error (funcall callback (cider-read-from-minibuffer prompt)))))

(defun cider--should-prompt-for-symbol (&optional invert)
(if invert (not cider-prompt-for-symbol) cider-prompt-for-symbol))

(defun cider-prompt-for-symbol-function (&optional invert)
(if (cider--should-prompt-for-symbol invert)
(if (cider--prompt-for-symbol? invert)
#'cider-read-symbol-name
#'cider-try-symbol-at-point))

Expand Down

0 comments on commit 0992654

Please sign in to comment.