Skip to content

Commit 623a669

Browse files
arichiardibbatsov
authored andcommitted
Add cider-ns-reload and cider-ns-reload-all commands
The C-c M-n l has been assigned to cider-ns-reload while C-c M-n M-l has been assigned to cider-ns-reload-all.
1 parent a1f86b3 commit 623a669

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#2373](https://github.com/clojure-emacs/cider/issues/2373): Make it possible to configure the welcome message displayed in scratch buffers via `cider-scratch-initial-message`.
1919
* Add the ability to jump to the profiler buffer using `cider-selector`.
2020
* [#1980](https://github.com/clojure-emacs/cider/issues/1980): Echo back missing namespace name on interactive eval (requires nREPL 0.4.3+).
21+
* [#2314](https://github.com/clojure-emacs/cider/pull/2314): Add `cider-ns-reload` and `cider-ns-reload-all` interactive commands.
2122

2223
### Bugs fixed
2324

cider-mode.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ If invoked with a prefix ARG eval the expression after inserting it."
352352
["Recursively load all files in directory" cider-load-all-files]
353353
["Load all project files" cider-load-all-project-ns]
354354
["Refresh loaded code" cider-ns-refresh]
355+
["Require and reload" cider-ns-reload]
356+
["Require and reload all" cider-ns-reload-all]
355357
["Run project (-main function)" cider-run])
356358
"Menu for CIDER mode eval commands.")
357359

@@ -410,6 +412,8 @@ If invoked with a prefix ARG eval the expression after inserting it."
410412

411413

412414
(declare-function cider-ns-refresh "cider-ns")
415+
(declare-function cider-ns-reload "cider-ns")
416+
(declare-function cider-ns-reload-all "cider-ns")
413417
(declare-function cider-browse-ns "cider-browse-ns")
414418
(declare-function cider-eval-ns-form "cider-eval")
415419
(declare-function cider-repl-set-ns "cider-repl")
@@ -427,6 +431,8 @@ If invoked with a prefix ARG eval the expression after inserting it."
427431
(define-key map (kbd "M-n") #'cider-repl-set-ns)
428432
(define-key map (kbd "r") #'cider-ns-refresh)
429433
(define-key map (kbd "M-r") #'cider-ns-refresh)
434+
(define-key map (kbd "l") #'cider-ns-reload)
435+
(define-key map (kbd "M-l") #'cider-ns-reload-all)
430436
map)
431437
"CIDER NS keymap.")
432438

cider-ns.el

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,38 @@
2222

2323
;;; Commentary:
2424

25-
;; Smart code refresh functionality based on ideas
26-
;; fromhttp://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded
25+
;; Smart code refresh functionality based on ideas from:
26+
;; http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded
27+
;;
28+
;; Note that refresh with clojure.tools.namespace.repl is a smarter way to
29+
;; reload code: the traditional way to reload Clojure code without restarting
30+
;; the JVM is (require ... :reload) or an editor/IDE feature that does the same
31+
;; thing.
32+
;;
33+
;; This has several problems:
34+
;;
35+
;; If you modify two namespaces which depend on each other, you must remember to
36+
;; reload them in the correct order to avoid compilation errors.
37+
;;
38+
;; If you remove definitions from a source file and then reload it, those
39+
;; definitions are still available in memory. If other code depends on those
40+
;; definitions, it will continue to work but will break the next time you
41+
;; restart the JVM.
42+
;;
43+
;; If the reloaded namespace contains defmulti, you must also reload all of the
44+
;; associated defmethod expressions.
45+
;;
46+
;; If the reloaded namespace contains defprotocol, you must also reload any
47+
;; records or types implementing that protocol and replace any existing
48+
;; instances of those records/types with new instances.
49+
;;
50+
;; If the reloaded namespace contains macros, you must also reload any
51+
;; namespaces which use those macros.
52+
;;
53+
;; If the running program contains functions which close over values in the
54+
;; reloaded namespace, those closed-over values are not updated (This is common
55+
;; in web applications which construct the "handler stack" as a composition of
56+
;; functions.)
2757

2858
;;; Code:
2959

@@ -145,6 +175,37 @@ Its behavior is controlled by `cider-save-files-on-cider-ns-refresh'."
145175
(file-truename default-directory)
146176
(eq system-type 'windows-nt))))))))
147177

178+
;;;###autoload
179+
(defun cider-ns-reload (&optional prompt)
180+
"Send a (require 'ns :reload) to the REPL.
181+
182+
With an argument PROMPT, it prompts for a namespace name. This is the
183+
Clojure out of the box reloading experience and does not rely on
184+
org.clojure/tools.namespace. See Commentary of this file for a longer list
185+
of differences. From the Clojure doc: \":reload forces loading of all the
186+
identified libs even if they are already loaded\"."
187+
(interactive "P")
188+
(let ((ns (if prompt
189+
(string-remove-prefix "'" (read-from-minibuffer "Namespace: " (clojure-find-ns)))
190+
(clojure-find-ns))))
191+
(cider-interactive-eval (format "(require '%s :reload)" ns))))
192+
193+
;;;###autoload
194+
(defun cider-ns-reload-all (&optional prompt)
195+
"Send a (require 'ns :reload-all) to the REPL.
196+
197+
With an argument PROMPT, it prompts for a namespace name. This is the
198+
Clojure out of the box reloading experience and does not rely on
199+
org.clojure/tools.namespace. See Commentary of this file for a longer list
200+
of differences. From the Clojure doc: \":reload-all implies :reload and
201+
also forces loading of all libs that the identified libs directly or
202+
indirectly load via require\"."
203+
(interactive "P")
204+
(let ((ns (if prompt
205+
(string-remove-prefix "'" (read-from-minibuffer "Namespace: " (clojure-find-ns)))
206+
(clojure-find-ns))))
207+
(cider-interactive-eval (format "(require '%s :reload-all)" ns))))
208+
148209
;;;###autoload
149210
(defun cider-ns-refresh (&optional mode)
150211
"Reload modified and unloaded namespaces on the classpath.

cider-util.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ through a stack of help buffers. Variables `help-back-label' and
772772
"Exploring CIDER's menu-bar entries is a great way to discover features."
773773
"Keep in mind that some commands don't have a keybinding by default. Explore CIDER!"
774774
"Tweak `cider-repl-prompt-function' to customize your REPL prompt."
775-
"Tweak `cider-eldoc-ns-function' to customize the way namespaces are displayed by eldoc.")
775+
"Tweak `cider-eldoc-ns-function' to customize the way namespaces are displayed by eldoc."
776+
"For no middleware, low-tech and reliable namespace reloading use <\\[cider-ns-reload]>.")
776777
"Some handy CIDER tips."
777778
)
778779

doc/miscellaneous_features.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ and
148148
* By default, all modified Clojure buffers are prompted to be saved. This
149149
behaviour can be customized using `cider-ns-save-files-on-refresh`.
150150

151+
In case `cider-ns-refresh` does not work for you the
152+
`cider-ns-reload`|`cider-ns-reload-all` commands can be used instead: they will
153+
evaluate the Clojure's out of the box `(require ... :reload|:reload-all)` at
154+
the REPL.
155+
151156
## Tracing function execution
152157

153158
You can trace the results produced by functions using <kbd>C-c M-t v</kbd>. The

0 commit comments

Comments
 (0)