Skip to content

Commit

Permalink
Support new cider.clj-reload/reload cider-nrepl middlewarex
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and vemv committed Mar 10, 2024
1 parent 6403a8d commit c4fa1a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- [#3622](https://github.com/clojure-emacs/cider/pull/3461): Basic support for using CIDER from [clojure-ts-mode](https://github.com/clojure-emacs/clojure-ts-mode).
- The `clojure-mode` dependency is still required for CIDER to function.
- Some features like `cider-dynamic-indentation` and `cider-font-lock-dynamically` do not work with `clojure-ts-mode` (yet).
- [#3624](https://github.com/clojure-emacs/cider/pull/3624): Support new `cider.clj-reload/reload` cider-nrepl middleware.
- adds `cider-ns-refresh-tool` defcustom, defaulting to `'tools.namespace`.
- you can change it to `'clj-reload` to use [clj-reload](https://github.com/tonsky/clj-reload) instead of [tools.namespace](https://github.com/clojure/tools.namespace).

### Changes

Expand Down
33 changes: 29 additions & 4 deletions cider-ns.el
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ namespace-qualified function of zero arity."
:group 'cider
:package-version '(cider . "0.10.0"))

(defcustom cider-ns-code-reload-tool 'tools.namespace
"Which tool to use for ns refresh.
Current options: tools.namespace and clj-reload."
:group 'cider
:type '(choice (const :tag "tools.namespace https://github.com/clojure/tools.namespace" tools.namespace)
(const :tag "clj-reload https://github.com/tonsky/clj-reload" clj-reload))
:package-version '(cider . "1.14.0"))

(defun cider-ns--present-error (error)
"Render the `ERROR' stacktrace,
and jump to the adequate file/line location,
Expand Down Expand Up @@ -156,7 +164,7 @@ presenting the error message as an overlay."

(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)
(nrepl-dbind-response response (out err reloading progress status error error-ns after before)
(cl-flet* ((log (message &optional face)
(cider-emit-into-popup-buffer log-buffer message face t))

Expand Down Expand Up @@ -184,6 +192,9 @@ presenting the error message as an overlay."
(reloading
(log-echo (format "Reloading %s\n" reloading) 'font-lock-string-face))

(progress
(log-echo progress 'font-lock-string-face))

((member "reloading" (nrepl-dict-keys response))
(log-echo "Nothing to reload\n" 'font-lock-string-face))

Expand Down Expand Up @@ -223,6 +234,19 @@ Its behavior is controlled by `cider-ns-save-files-on-refresh' and
(file-in-directory-p buffer-file-name dir))
dirs)))))))

(defun cider-ns--reload-op (op-name)
"Return the reload operation to use.
Based on OP-NAME and the value of cider-ns-code-reload-tool defcustom."
(list "op"
(if (eq cider-ns-code-reload-tool 'tools.namespace)
(cond ((string= op-name "reload") "refresh")
((string= op-name "reload-all") "refresh-all")
((string= op-name "reload-clear") "refresh-clear"))

(cond ((string= op-name "reload") "cider.clj-reload/reload")
((string= op-name "reload-all") "cider.clj-reload/reload-all")
((string= op-name "reload-clear") "cider.clj-reload/reload-clear")))))

;;;###autoload
(defun cider-ns-reload (&optional prompt)
"Send a (require 'ns :reload) to the REPL.
Expand Down Expand Up @@ -275,9 +299,10 @@ refresh functions (defined in `cider-ns-refresh-before-fn' and
(interactive "p")
(cider-ensure-connected)
(cider-ensure-op-supported "refresh")
(cider-ensure-op-supported "cider.clj-reload/reload")
(cider-ns-refresh--save-modified-buffers)
(let ((clear? (member mode '(clear 16)))
(refresh-all? (member mode '(refresh-all 4)))
(all? (member mode '(refresh-all 4)))
(inhibit-refresh-fns (member mode '(inhibit-fns -1))))
(cider-map-repls :clj
(lambda (conn)
Expand All @@ -292,11 +317,11 @@ refresh functions (defined in `cider-ns-refresh-before-fn' and
nil
t))
(when clear?
(cider-nrepl-send-sync-request '("op" "refresh-clear") conn))
(cider-nrepl-send-sync-request (cider-ns--reload-op "reload-clear") conn))
(cider-nrepl-send-request
(thread-last
(map-merge 'list
`(("op" ,(if refresh-all? "refresh-all" "refresh")))
`(,(cider-ns--reload-op (if all? "reload-all" "reload")))
(cider--nrepl-print-request-map fill-column)
(when (and (not inhibit-refresh-fns) cider-ns-refresh-before-fn)
`(("before" ,cider-ns-refresh-before-fn)))
Expand Down
17 changes: 17 additions & 0 deletions doc/modules/ROOT/pages/usage/misc_features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ and `cider-ns-reload-all` commands can be used instead. These commands
invoke Clojure's `+(require ... :reload)+` and `+(require
... :reload-all)+` commands at the REPL.

You can also use https://github.com/tonsky/clj-reload[clj-reload] instead.
It provides support for
https://github.com/tonsky/clj-reload/blob/469da68/README.md#usage-keeping-vars-between-reloads[keeping vars between reloads]
among some
https://github.com/tonsky/clj-reload/blob/469da68/README.md#comparison-toolsnamespace[other differences]
from `tools.namespace`.

[source,lisp]
----
(setq cider-ns-code-reload-tool 'clj-reload)
----

With `clj-reload` you should set the source dirs as described in
https://github.com/tonsky/clj-reload/blob/469da68/README.md##usage[the usage docs]
. If you don't set them manually, it will default to the current project's resource dirs in the same
way `tools.namespace` does.

== CIDER Selector

The `cider-selector` (kbd:[C-c M-s]) command allows you to quickly navigate to
Expand Down

0 comments on commit c4fa1a8

Please sign in to comment.