Skip to content

cider-ns-refresh: jump to the relevant file/line on errors #3627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Changes

- [#3626](https://github.com/clojure-emacs/cider/issues/3626): `cider-ns-refresh`: jump to the relevant file/line on errors.
- Bump the injected nREPL to [1.1.1](https://github.com/nrepl/nrepl/blob/v1.1.1/CHANGELOG.md#111-2024-02-20).
- Bump the injected `cider-nrepl` to [0.46.0](https://github.com/clojure-emacs/cider-nrepl/blob/1cc9b2/CHANGELOG.md#0460-2024-0305).
- Updates [Orchard](https://github.com/clojure-emacs/orchard/blob/v0.23.0/CHANGELOG.md#0230-2024-03-03).
Expand Down
31 changes: 27 additions & 4 deletions cider-ns.el
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ namespace-qualified function of zero arity."
:group 'cider
:package-version '(cider . "0.10.0"))

(defun cider-ns--present-error (error)
"Render the `ERROR' stacktrace,
and jump to the adequate file/line location."
(let* ((buf)
(jump-args (seq-some (lambda (cause-dict) ;; a dict representing an exception cause
(nrepl-dbind-response cause-dict (file-url line column)
(when (and file-url
;; jars are unlikely sources of user errors, so we favor the next `cause-dict':
(not (string-prefix-p "jar:" file-url))
line)
(setq buf (cider--find-buffer-for-file file-url))
(list buf (cons line column)))))
error)))
(when jump-args
(apply #'cider-jump-to jump-args))
(cider--render-stacktrace-causes error)
;; Select the window displaying the 'culprit' buffer so that the user can immediately fix it,
;; as most times the displayed stacktrace doesn't need much inspection:
(when buf
(select-window (get-buffer-window buf)))))

(defun cider-ns-refresh--handle-response (response log-buffer)
"Refresh LOG-BUFFER with RESPONSE."
(nrepl-dbind-response response (out err reloading status error error-ns after before)
Expand Down Expand Up @@ -168,8 +189,9 @@ namespace-qualified function of zero arity."
(with-current-buffer cider-ns-refresh-log-buffer
(goto-char (point-max))))

(when (member "error" status)
(cider--render-stacktrace-causes error))))
(when (and (member "error" status)
error)
(cider-ns--present-error error))))

(defun cider-ns-refresh--save-modified-buffers ()
"Ensure any relevant modified buffers are saved before refreshing.
Expand Down Expand Up @@ -219,10 +241,11 @@ indirectly load via require\"."

;;;###autoload
(defun cider-ns-refresh (&optional mode)
"Reload modified and unloaded namespaces on the classpath.
"Reload modified and unloaded namespaces, using the Reloaded Workflow.
Uses the configured 'refresh dirs' \(defaults to the classpath dirs).

With a single prefix argument, or if MODE is `refresh-all', reload all
namespaces on the classpath unconditionally.
namespaces on the classpath dirs unconditionally.

With a double prefix argument, or if MODE is `clear', clear the state of
the namespace tracker before reloading. This is useful for recovering from
Expand Down