Skip to content

Commit 75755b4

Browse files
ericdallobbatsov
authored andcommitted
Add before and after eval hooks
1 parent b160616 commit 75755b4

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

cider-client.el

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ resulting value are used to compute completions."
7070
:group 'cider
7171
:package-version '(cider . "0.23.0"))
7272

73+
(defcustom cider-before-eval-hook nil
74+
"List of functions to call before eval request is sent to nrepl."
75+
:type 'hook
76+
:group 'cider
77+
:package-version '(cider . "1.2.0"))
78+
79+
(defcustom cider-after-eval-done-hook nil
80+
"List of functions to call after eval was responded by nrepl with done status."
81+
:type 'hook
82+
:group 'cider
83+
:package-version '(cider . "1.2.0"))
84+
7385
(defun cider-spinner-start (buffer)
7486
"Start the evaluation spinner in BUFFER.
7587
Do nothing if `cider-show-eval-spinner' is nil."
@@ -78,21 +90,19 @@ Do nothing if `cider-show-eval-spinner' is nil."
7890
(spinner-start cider-eval-spinner-type nil
7991
cider-eval-spinner-delay))))
8092

81-
(defun cider-eval-spinner-handler (eval-buffer original-callback)
82-
"Return a response handler to stop the spinner and call ORIGINAL-CALLBACK.
93+
(defun cider-eval-spinner (eval-buffer response)
94+
"Handle RESPONSE stopping the spinner.
8395
EVAL-BUFFER is the buffer where the spinner was started."
84-
(lambda (response)
85-
;; buffer still exists and
86-
;; we've got status "done" from nrepl
87-
;; stop the spinner
88-
(when (and (buffer-live-p eval-buffer)
89-
(let ((status (nrepl-dict-get response "status")))
90-
(or (member "done" status)
91-
(member "eval-error" status)
92-
(member "error" status))))
93-
(with-current-buffer eval-buffer
94-
(when spinner-current (spinner-stop))))
95-
(funcall original-callback response)))
96+
;; buffer still exists and
97+
;; we've got status "done" from nrepl
98+
;; stop the spinner
99+
(when (and (buffer-live-p eval-buffer)
100+
(let ((status (nrepl-dict-get response "status")))
101+
(or (member "done" status)
102+
(member "eval-error" status)
103+
(member "error" status))))
104+
(with-current-buffer eval-buffer
105+
(when spinner-current (spinner-stop)))))
96106

97107

98108
;;; Evaluation helpers
@@ -198,10 +208,14 @@ define the position of INPUT in its buffer. ADDITIONAL-PARAMS is a plist
198208
to be appended to the request message. CONNECTION is the connection
199209
buffer, defaults to (cider-current-repl)."
200210
(let ((connection (or connection (cider-current-repl nil 'ensure))))
211+
(run-hooks 'cider-before-eval-hook)
201212
(nrepl-request:eval input
202-
(if cider-show-eval-spinner
203-
(cider-eval-spinner-handler connection callback)
204-
callback)
213+
(lambda (response)
214+
(when cider-show-eval-spinner
215+
(cider-eval-spinner connection response))
216+
(when (member "done" (nrepl-dict-get response "status"))
217+
(run-hooks 'cider-after-eval-done-hook))
218+
(funcall callback response))
205219
connection
206220
ns line column additional-params)
207221
(cider-spinner-start connection)))

doc/modules/ROOT/pages/usage/code_evaluation.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ file has been evaluated.
123123
TIP: `cider-auto-test-mode` is implemented in terms of this hook -
124124
tests are re-run every time you evaluate a file.
125125

126+
You can use `cider-before-eval-hook` to trigger some action before eval
127+
request is sent to nrepl server.
128+
129+
You can use `cider-after-eval-done-hook` to trigger some action after the
130+
eval response was received from nrepl server with status "done".
131+
126132
== Synchronous vs Asynchronous Evaluation
127133

128134
nREPL has an asynchronous evaluation model, where eval requests

0 commit comments

Comments
 (0)