Skip to content

Commit a83bd7f

Browse files
committed
Merge pull request #761 from vitoshka/jump-to-var
`cider-jump-to-var` is broken when there is no symbol at point
2 parents dd1c044 + b130451 commit a83bd7f

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

cider-interaction.el

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,15 @@ not found."
649649
(file (cadr (assoc "file" info))))
650650
(cider-find-file file)))
651651

652-
(defun cider-jump-to (buffer &optional pos)
652+
(defun cider-jump-to (buffer &optional pos other-buffer)
653653
"Push current point onto marker ring, and jump to BUFFER to position POS.
654654
POS can be either a cons cell (LINE . COLUMN) or a number representing the
655-
character position in a buffer. "
655+
character position in a buffer. If OTHER-BUFFER is non-nil use
656+
`pop-to-buffer' to jump to the location, otherwise `switch-to-buffer'."
656657
(ring-insert find-tag-marker-ring (point-marker))
658+
(if other-buffer
659+
(pop-to-buffer buffer)
660+
(switch-to-buffer buffer))
657661
(with-current-buffer buffer
658662
(widen)
659663
;; check if we have a (line . column) pair or just a buffer position
@@ -664,8 +668,7 @@ character position in a buffer. "
664668
(if (cdr pos)
665669
(move-to-column (cdr pos))
666670
(back-to-indentation)))
667-
(cider-mode +1))
668-
(switch-to-buffer buffer))
671+
(cider-mode +1)))
669672

670673
(defun cider-jump-to-resource (path &optional line)
671674
"Jump to the resource at the relative PATH, optionally at a specific LINE.
@@ -679,24 +682,34 @@ When called interactively, this operates on point."
679682
(cider-jump-to buffer (cons line nil))
680683
(message "Cannot find resource %s" path)))
681684

682-
(defun cider-jump-to-var (var &optional line)
683-
"Jump to the definition of VAR, optionally at a specific LINE.
684-
When called interactively, this operates on point, or falls back to a prompt."
685-
(interactive (list (cider-read-symbol-name "Symbol: " 'identity)))
686-
(cider-ensure-op-supported "info")
687-
(-if-let (info (cider-var-info var))
688-
(-if-let* ((file (cadr (assoc "file" info)))
689-
(line (or line (cadr (assoc "line" info))))
690-
(buffer (cider-find-file file)))
691-
(cider-jump-to buffer (cons line nil))
685+
(defun cider--jump-to-loc-from-info (info &optional other-buffer)
686+
"Jump to location give by INFO.
687+
INFO object is returned by `cider-var-info' or `cider-member-info'.
688+
OTHER-BUFFER is passed to `cider-jamp-to'."
689+
(-if-let* ((file (cadr (assoc "file" info)))
690+
(line (cadr (assoc "line" info)))
691+
(buffer (cider-find-file file)))
692+
(cider-jump-to buffer (cons line nil) other-buffer)
692693
;; var was created interactively and has no file info
693694
(-if-let* ((ns (cadr (assoc "ns" info)))
694695
(name (cadr (assoc "name" info)))
695696
(buffer (cider-find-buffer ns))
696-
(pos (cider-locate-def buffer name)))
697-
(cider-jump-to buffer pos)
698-
(message "No source available for %s" var)))
699-
(message "Symbol %s not resolved" var)))
697+
(pos (cider-locate-def name buffer line)))
698+
(cider-jump-to buffer pos other-buffer)
699+
(-if-let (name (cadr (assoc "name" info)))
700+
(message "No location found for %s" name)
701+
(message "No source info")))))
702+
703+
(defun cider-jump-to-var (&optional var line)
704+
"Jump to the definition of VAR, optionally at a specific LINE.
705+
When called interactively, this operates on point, or falls back to a prompt."
706+
(interactive)
707+
(cider-ensure-op-supported "info")
708+
(cider-read-symbol-name
709+
"Symbol: " (lambda (var)
710+
(-if-let (info (cider-var-info var))
711+
(cider--jump-to-loc-from-info info)
712+
(message "Symbol %s not resolved" var)))))
700713

701714
(define-obsolete-function-alias 'cider-jump 'cider-jump-to-var "0.7.0")
702715
(defalias 'cider-jump-back 'pop-tag-mark)

cider-stacktrace.el

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,20 +401,12 @@ it wraps to 0."
401401
(let* ((var (button-get button 'var))
402402
(class (button-get button 'class))
403403
(method (button-get button 'method))
404-
(line (button-get button 'line))
405-
(info (if var
406-
(cider-var-info var)
407-
(cider-member-info class method))))
408-
(-if-let* ((file (cadr (assoc "file" info)))
409-
(buffer (cider-find-file file)))
410-
(cider-jump-to buffer (cons line nil))
411-
;; when no file info, find the location by regexp search
412-
(-if-let* ((ns (cadr (assoc "ns" info)))
413-
(name (cadr (assoc "name" info)))
414-
(buffer (cider-find-buffer ns))
415-
(pos (cider-locate-def buffer name)))
416-
(cider-jump-to buffer pos)
417-
(message "No source info")))))
404+
(info (or (and var (cider-var-info var))
405+
(and class method (cider-member-info class method))))
406+
;; stacktrace returns more accurate line numbers
407+
(info (cons `("line" ,(button-get button 'line))
408+
info)))
409+
(cider--jump-to-loc-from-info info t)))
418410

419411
(defun cider-stacktrace-jump ()
420412
"Like `cider-jump', but uses the stack frame source at point, if available."

cider-util.el

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,21 @@ Unless you specify a BUFFER it will default to the current one."
152152
(--filter (equal ns (with-current-buffer it (clojure-find-ns))))
153153
(car)))
154154

155-
(defun cider-locate-def (buffer name)
156-
"Locate in BUFFER the definition of var NAME.
157-
This is a regexp search and currently works only for top level clojure
158-
forms that start with '(def'."
155+
(defun cider-locate-def (name buffer &optional offset-lines)
156+
"Locate the definition of var NAME in BUFFER.
157+
When OFFSET-LINES is non-nil, adjust the returned position by this many
158+
lines. This is a regexp search and currently works only for top level
159+
clojure forms that start with '(def'."
159160
(with-current-buffer buffer
160161
(save-restriction
161162
(widen)
162163
(goto-char (point-min))
163164
(re-search-forward (format "(def.* %s\\( \\|$\\)" name)
164165
nil 'no-error)
165-
(goto-char (match-beginning 0)))))
166+
(goto-char (match-beginning 0))
167+
(when offset-lines
168+
(forward-line (1- offset-lines)))
169+
(point))))
166170

167171
;;; Strings
168172

0 commit comments

Comments
 (0)