Skip to content

Commit 519f6c7

Browse files
committed
Enhance cider-find-file and use stacktrace file data for navigation
1 parent 27e4e07 commit 519f6c7

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

cider-interaction.el

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ If no local or remote file exists, return nil."
588588
(cond ((equal local-path "") "")
589589
((and cider-prefer-local-resources (file-exists-p local-path))
590590
local-path)
591-
((file-exists-p tramp-path)
591+
((and tramp-path (file-exists-p tramp-path))
592592
tramp-path)
593-
((file-exists-p local-path)
593+
((and local-path (file-exists-p local-path))
594594
local-path))))
595595

596596
(defun cider--url-to-file (url)
@@ -604,10 +604,19 @@ create a valid path."
604604
(match-string 1 filename)
605605
filename)))
606606

607+
(defun cider--tooling-file-p (file-name)
608+
"Return t if FILE-NAME is not a 'real' source file.
609+
Currently, only check if the relative file name starts with 'form-init'
610+
which nREPL uses for temporary evaluation file names."
611+
(string-match-p "\\bform-init" (file-name-nondirectory file-name)))
612+
607613
(defun cider-find-file (url)
608614
"Return a buffer visiting the file URL if it exists, or nil otherwise.
609-
The argument should have a scheme prefix, and represent a fully-qualified file
610-
path or an entry within a zip/jar archive."
615+
If URL has a scheme prefix, it must represent a fully-qualified file path
616+
or an entry within a zip/jar archive. If URL doesn't contain a scheme
617+
prefix and is an absolute path, it is treated as such. Finally, if URL is
618+
relative, it is expanded within each of the open Clojure buffers till an
619+
existing file ending with URL has been found."
611620
(cond ((string-match "^file:\\(.+\\)" url)
612621
(-when-let* ((file (cider--url-to-file (match-string 1 url)))
613622
(path (cider--file-path file)))
@@ -639,7 +648,15 @@ path or an entry within a zip/jar archive."
639648
(setq-local buffer-read-only t)
640649
(set-buffer-modified-p nil)
641650
(set-auto-mode)
642-
(current-buffer))))))))
651+
(current-buffer))))))
652+
(t (-if-let (path (cider--file-path url))
653+
(find-file-noselect path)
654+
(unless (file-name-absolute-p url)
655+
(cl-loop for bf in (cider-util--clojure-buffers)
656+
for path = (with-current-buffer bf
657+
(expand-file-name url))
658+
if (and path (file-exists-p path))
659+
return (find-file-noselect path)))))))
643660

644661
(defun cider-find-var-file (var)
645662
"Return the buffer visiting the file in which VAR is defined, or nil if
@@ -686,9 +703,10 @@ When called interactively, this operates on point."
686703
"Jump to location give by INFO.
687704
INFO object is returned by `cider-var-info' or `cider-member-info'.
688705
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)))
706+
(-if-let* ((line (cadr (assoc "line" info)))
707+
(file (cadr (assoc "file" info)))
708+
(buffer (unless (cider--tooling-file-p file)
709+
(cider-find-file file))))
692710
(cider-jump-to buffer (cons line nil) other-buffer)
693711
;; var was created interactively and has no file info
694712
(-if-let* ((ns (cadr (assoc "ns" info)))
@@ -1092,7 +1110,7 @@ until we find a delimiters that's not inside a string."
10921110
(backward-char)))
10931111

10941112
(defun cider--find-last-error-location (buffer message)
1095-
"Return the location (begin . end) in BUFFER from the clojure error MESSAGE.
1113+
"Return the location (begin . end) in BUFFER from the Clojure error MESSAGE.
10961114
If location could not be found, return nil."
10971115
(save-excursion
10981116
(with-current-buffer buffer

cider-stacktrace.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ it wraps to 0."
402402
(class (button-get button 'class))
403403
(method (button-get button 'method))
404404
(info (or (and var (cider-var-info var))
405-
(and class method (cider-member-info class method))))
405+
(and class method (cider-member-info class method))
406+
`(("file" ,(button-get button 'file)))))
406407
;; stacktrace returns more accurate line numbers
407408
(info (cons `("line" ,(button-get button 'line))
408409
info)))
@@ -461,8 +462,8 @@ This associates text properties to enable filtering and source navigation."
461462
(if (member 'clj flags) ns class)
462463
(if (member 'clj flags) fn method))
463464
'var var 'class class 'method method
464-
'name name 'line line 'flags flags
465-
'follow-link t
465+
'name name 'file file 'line line
466+
'flags flags 'follow-link t
466467
'action 'cider-stacktrace-navigate
467468
'help-echo "View source at this location"
468469
'face 'cider-stacktrace-face)

0 commit comments

Comments
 (0)