Skip to content

Commit 77a44c7

Browse files
committed
Refactor the interactive evaluation API
- There is now one API entry point - `cider-interactive-eval`. All other commands are based on it. - Simplify the code by removing unused or unnecessary API functions: `cider-interactive-eval-print`, `cider-popup-eval-print-handler`, `cider-popup-eval-print`, `cider-interactive-eval-to-repl` - Rename `cider-interactive-eval-print-handler` -> `cider-eval-print-handler`. - Bring all interactive eval functionality into one place and arrange functions in a logical order. - `buffer` argument to interactive eval handlers is optional
1 parent 7512b83 commit 77a44c7

File tree

2 files changed

+94
-114
lines changed

2 files changed

+94
-114
lines changed

cider-client.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,17 @@ unless ALL is truthy."
154154

155155
;;; Requests
156156

157-
(defun cider-request:load-file (file-contents file-path file-name)
157+
(defun cider-request:load-file (file-contents file-path file-name &optional callback)
158158
"Perform the nREPL \"load-file\" op.
159159
FILE-CONTENTS, FILE-PATH and FILE-NAME are details of the file to be
160-
loaded."
160+
loaded. If CALLBACK is nil, use `cider-load-file-handler'."
161161
(nrepl-send-request (list "op" "load-file"
162162
"session" (nrepl-current-session)
163163
"file" file-contents
164164
"file-path" file-path
165165
"file-name" file-name)
166-
(cider-load-file-handler (current-buffer))))
166+
(or callback
167+
(cider-load-file-handler (current-buffer)))))
167168

168169

169170
;;; Sync Requests

cider-interaction.el

Lines changed: 90 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -449,19 +449,20 @@ reading input."
449449
(interactive)
450450
(cider-interactive-eval (cider-read-from-minibuffer "CIDER Eval: ")))
451451

452-
;;; Eval
453-
(defun cider-eval-region (start end)
454-
"Evaluate the region.
455-
The two arguments START and END are character positions;
456-
they can be in either order."
457-
(interactive "r")
458-
(let ((code (buffer-substring-no-properties start end)))
459-
(cider-interactive-eval code start)))
452+
453+
;;; Utilities
460454

461-
(defun cider-eval-buffer ()
462-
"Evaluate the current buffer."
463-
(interactive)
464-
(cider-eval-region (point-min) (point-max)))
455+
(defun cider--clear-compilation-highlights ()
456+
"Remove compilation highlights."
457+
(remove-overlays (point-min) (point-max) 'cider-note-p t))
458+
459+
(defun cider-clear-compilation-highlights (&optional arg)
460+
"Remove compilation highlights.
461+
462+
When invoked with a prefix ARG the command doesn't prompt for confirmation."
463+
(interactive "P")
464+
(when (or arg (y-or-n-p "Are you sure you want to clear the compilation highlights? "))
465+
(cider--clear-compilation-highlights)))
465466

466467
(defun cider-defun-at-point ()
467468
"Return the text of the top-level sexp at point."
@@ -477,15 +478,9 @@ they can be in either order."
477478
(beginning-of-defun)
478479
(list (point) end)))))
479480

480-
(defun cider-eval-defun-at-point (&optional prefix)
481-
"Evaluate the current toplevel form, and print result in the minibuffer.
482-
With a PREFIX argument, print the result in the current buffer."
483-
(interactive "P")
484-
(let ((form (cider-defun-at-point))
485-
(start-pos (car (cider--region-for-defun-at-point))))
486-
(if prefix
487-
(cider-interactive-eval-print form)
488-
(cider-interactive-eval form start-pos))))
481+
(defun cider-defun-at-point-start-pos ()
482+
"Return the starting position of the current defun."
483+
(car (cider--region-for-defun-at-point)))
489484

490485
(defun cider-ns-form ()
491486
"Retrieve the ns form."
@@ -494,14 +489,6 @@ With a PREFIX argument, print the result in the current buffer."
494489
(goto-char (match-beginning 0))
495490
(cider-defun-at-point))))
496491

497-
(defun cider-eval-ns-form ()
498-
"Evaluate the current buffer's namespace form."
499-
(interactive)
500-
(when (clojure-find-ns)
501-
(save-excursion
502-
(goto-char (match-beginning 0))
503-
(cider-eval-defun-at-point))))
504-
505492
(defun cider-bounds-of-sexp-at-point ()
506493
"Return the bounds sexp at point as a pair (or nil)."
507494
(or (and (equal (char-after) ?\()
@@ -843,9 +830,9 @@ in the buffer."
843830
(interactive "P")
844831
(cider-read-symbol-name "Javadoc for: " 'cider-javadoc-handler query))
845832

846-
(defun cider-stdin-handler (buffer)
833+
(defun cider-stdin-handler (&optional buffer)
847834
"Make a stdin response handler for BUFFER."
848-
(nrepl-make-response-handler buffer
835+
(nrepl-make-response-handler (or buffer (current-buffer))
849836
(lambda (buffer value)
850837
(cider-repl-emit-result buffer value t))
851838
(lambda (buffer out)
@@ -854,10 +841,10 @@ in the buffer."
854841
(cider-repl-emit-output buffer err))
855842
nil))
856843

857-
(defun cider-insert-eval-handler (buffer)
844+
(defun cider-insert-eval-handler (&optional buffer)
858845
"Make a nREPL evaluation handler for the BUFFER.
859846
The handler simply inserts the result value in BUFFER."
860-
(nrepl-make-response-handler buffer
847+
(nrepl-make-response-handler (or buffer (current-buffer))
861848
(lambda (_buffer value)
862849
(with-current-buffer buffer
863850
(insert value)))
@@ -896,9 +883,9 @@ The output can be send to either a dedicated output buffer or the current REPL b
896883
This is controlled via `cider-interactive-eval-output-destination'."
897884
(cider--emit-interactive-eval-output output 'cider-repl-emit-interactive-err-output))
898885

899-
(defun cider-interactive-eval-handler (buffer)
886+
(defun cider-interactive-eval-handler (&optional buffer)
900887
"Make an interactive eval handler for BUFFER."
901-
(nrepl-make-response-handler buffer
888+
(nrepl-make-response-handler (or buffer (current-buffer))
902889
(lambda (_buffer value)
903890
(message "%s%s"
904891
cider-interactive-eval-result-prefix
@@ -911,9 +898,9 @@ This is controlled via `cider-interactive-eval-output-destination'."
911898
(cider-jump-to-error-maybe buffer err))
912899
'()))
913900

914-
(defun cider-load-file-handler (buffer)
901+
(defun cider-load-file-handler (&optional buffer)
915902
"Make a load file handler for BUFFER."
916-
(nrepl-make-response-handler buffer
903+
(nrepl-make-response-handler (or buffer (current-buffer))
917904
(lambda (buffer value)
918905
(message "%s" value)
919906
(with-current-buffer buffer
@@ -930,9 +917,9 @@ This is controlled via `cider-interactive-eval-output-destination'."
930917
(funcall nrepl-err-handler
931918
buffer ex root-ex session))))
932919

933-
(defun cider-interactive-eval-print-handler (buffer)
920+
(defun cider-eval-print-handler (&optional buffer)
934921
"Make a handler for evaluating and printing result in BUFFER."
935-
(nrepl-make-response-handler buffer
922+
(nrepl-make-response-handler (or buffer (current-buffer))
936923
(lambda (buffer value)
937924
(with-current-buffer buffer
938925
(insert
@@ -945,21 +932,11 @@ This is controlled via `cider-interactive-eval-output-destination'."
945932
(cider-emit-interactive-eval-output err))
946933
'()))
947934

948-
(defun cider-popup-eval-print-handler (buffer)
949-
"Make a handler for evaluating and printing result in popup BUFFER."
950-
(nrepl-make-response-handler buffer
951-
(lambda (buffer str)
952-
(cider-emit-into-popup-buffer buffer str))
953-
'()
954-
(lambda (buffer str)
955-
(cider-emit-into-popup-buffer buffer str))
956-
'()))
957-
958-
(defun cider-popup-eval-out-handler (buffer)
935+
(defun cider-popup-eval-out-handler (&optional buffer)
959936
"Make a handler for evaluating and printing stdout/stderr in popup BUFFER.
960937
961938
This is used by pretty-printing commands and intentionally discards their results."
962-
(nrepl-make-response-handler buffer
939+
(nrepl-make-response-handler (or buffer (current-buffer))
963940
'()
964941
(lambda (buffer str)
965942
(cider-emit-into-popup-buffer buffer str))
@@ -1250,37 +1227,29 @@ search for and read a `ns' form."
12501227

12511228

12521229
;;; Evaluation
1253-
(defun cider--clear-compilation-highlights ()
1254-
"Remove compilation highlights."
1255-
(remove-overlays (point-min) (point-max) 'cider-note-p t))
1256-
1257-
(defun cider-clear-compilation-highlights (&optional arg)
1258-
"Remove compilation highlights.
1259-
1260-
When invoked with a prefix ARG the command doesn't prompt for confirmation."
1261-
(interactive "P")
1262-
(when (or arg (y-or-n-p "Are you sure you want to clear the compilation highlights? "))
1263-
(cider--clear-compilation-highlights)))
1264-
1265-
(defun cider-popup-eval-print (form)
1266-
"Evaluate the given FORM and print value in current buffer."
1267-
(let ((buffer (current-buffer)))
1268-
(cider-eval form
1269-
(cider-popup-eval-print-handler buffer)
1270-
(cider-current-ns))))
12711230

1272-
(defun cider-interactive-eval-print (form)
1273-
"Evaluate the given FORM and print value in current buffer."
1274-
(let ((buffer (current-buffer)))
1275-
(cider-eval form
1276-
(cider-interactive-eval-print-handler buffer)
1277-
(cider-current-ns))))
1231+
(defun cider-interactive-eval (form &optional start-pos callback)
1232+
"Evaluate FORM and dispatch the response to CALLBACK.
1233+
START-POS is a starting position of the form in the original context. This
1234+
function is the main entry point in CIDER's interactive evaluation API. All
1235+
other interactive eval functions should rely on this function. If CALLBACK
1236+
is nil use `cider-interactive-eval-handler'."
1237+
(cider--clear-compilation-highlights)
1238+
(let ((filename (buffer-file-name)))
1239+
(cider-request:load-file
1240+
(cider--dummy-file-contents form start-pos)
1241+
(funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1242+
(file-name-nondirectory filename)
1243+
(or callback (cider-interactive-eval-handler)))))
12781244

12791245
(defun cider--dummy-file-contents (form start-pos)
1246+
"Wrap FORM to make it suitable for `cider-request:load-file'.
1247+
START-POS is a starting position of the form in the original context."
12801248
(let* ((ns-form (if (cider-ns-form-p form)
12811249
""
12821250
(cider-ns-form)))
12831251
(ns-form-lines (length (split-string ns-form "\n")))
1252+
(start-pos (or start-pos 1))
12841253
(start-line (line-number-at-pos start-pos))
12851254
(start-column (save-excursion (goto-char start-pos) (current-column))))
12861255
(concat
@@ -1289,79 +1258,86 @@ When invoked with a prefix ARG the command doesn't prompt for confirmation."
12891258
(make-string start-column ? )
12901259
form)))
12911260

1292-
(defun cider-interactive-eval (form &optional start-pos)
1293-
"Evaluate the given FORM and print value in minibuffer."
1294-
(cider--clear-compilation-highlights)
1295-
(let ((buffer (current-buffer))
1296-
(filename (buffer-file-name)))
1297-
(if buffer-file-name
1298-
(nrepl-send-request (list "op" "load-file"
1299-
"session" (nrepl-current-session)
1300-
"file" (cider--dummy-file-contents form start-pos)
1301-
"file-path" (funcall cider-to-nrepl-filename-function (cider--server-filename filename))
1302-
"file-name" (file-name-nondirectory filename))
1303-
(cider-interactive-eval-handler buffer))
1304-
(cider-eval form
1305-
(cider-interactive-eval-handler buffer)
1306-
(cider-current-ns)))))
1307-
1308-
(defun cider-interactive-eval-to-repl (form)
1309-
"Evaluate the given FORM and print it's value in REPL buffer."
1310-
(let ((buffer (cider-current-repl-buffer)))
1311-
(cider-eval form
1312-
(cider-insert-eval-handler buffer)
1313-
(cider-current-ns))))
1261+
(defun cider-eval-region (start end)
1262+
"Evaluate the region between START and END."
1263+
(interactive "r")
1264+
(let ((code (buffer-substring-no-properties start end)))
1265+
(cider-interactive-eval code start)))
1266+
1267+
(defun cider-eval-buffer ()
1268+
"Evaluate the current buffer."
1269+
(interactive)
1270+
(cider-eval-region (point-min) (point-max)))
13141271

13151272
(defun cider-eval-last-sexp (&optional prefix)
13161273
"Evaluate the expression preceding point.
13171274
If invoked with a PREFIX argument, print the result in the current buffer."
13181275
(interactive "P")
1319-
(if prefix
1320-
(cider-interactive-eval-print (cider-last-sexp))
1321-
(cider-interactive-eval (cider-last-sexp) (cider-last-sexp-start-pos))))
1276+
(cider-interactive-eval (cider-last-sexp) (cider-last-sexp-start-pos)
1277+
(when prefix (cider-eval-print-handler))))
13221278

13231279
(defun cider-eval-last-sexp-and-replace ()
13241280
"Evaluate the expression preceding point and replace it with its result."
13251281
(interactive)
1326-
(let ((last-sexp (cider-last-sexp)))
1282+
(let ((last-sexp (cider-last-sexp))
1283+
(start-pos (cider-last-sexp-start-pos)))
13271284
;; we have to be sure the evaluation won't result in an error
13281285
(nrepl-sync-request:eval last-sexp)
13291286
;; seems like the sexp is valid, so we can safely kill it
13301287
(backward-kill-sexp)
1331-
(cider-interactive-eval-print last-sexp)))
1288+
(cider-interactive-eval last-sexp start-pos (cider-eval-print-handler))))
13321289

13331290
(defun cider-eval-last-sexp-to-repl (&optional prefix)
13341291
"Evaluate the expression preceding point and insert its result in the REPL.
13351292
If invoked with a PREFIX argument, switch to the REPL buffer."
13361293
(interactive "P")
1337-
(cider-interactive-eval-to-repl (cider-last-sexp))
1294+
(cider-interactive-eval (cider-last-sexp) (cider-last-sexp-start-pos)
1295+
(cider-insert-eval-handler (cider-current-repl-buffer)))
13381296
(when prefix
13391297
(cider-switch-to-repl-buffer)))
13401298

13411299
(defun cider-eval-print-last-sexp ()
13421300
"Evaluate the expression preceding point.
13431301
Print its value into the current buffer."
13441302
(interactive)
1345-
(cider-interactive-eval-print (cider-last-sexp)))
1303+
(cider-interactive-eval (cider-last-sexp) (cider-last-sexp-start-pos)
1304+
(cider-eval-print-handler)))
13461305

1347-
(defun cider--pprint-form (form)
1348-
"Pretty print FORM in popup buffer."
1306+
(defun cider--pprint-eval-form (form start-pos)
1307+
"Pretty print FORM starting at START-POS in popup buffer."
13491308
(let* ((result-buffer (cider-popup-buffer cider-result-buffer nil 'clojure-mode))
13501309
(right-margin (max fill-column
13511310
(1- (window-width (get-buffer-window result-buffer))))))
1352-
(cider-eval (cider-format-pprint-eval form right-margin)
1353-
(cider-popup-eval-out-handler result-buffer)
1354-
(cider-current-ns))))
1311+
(cider-interactive-eval (cider-format-pprint-eval form right-margin) start-pos
1312+
(cider-popup-eval-out-handler result-buffer))))
13551313

13561314
(defun cider-pprint-eval-last-sexp ()
13571315
"Evaluate the sexp preceding point and pprint its value in a popup buffer."
13581316
(interactive)
1359-
(cider--pprint-form (cider-last-sexp)))
1317+
(cider--pprint-eval-form (cider-last-sexp) (cider-last-sexp-start-pos)))
1318+
1319+
(defun cider-eval-defun-at-point (&optional prefix)
1320+
"Evaluate the current toplevel form, and print result in the minibuffer.
1321+
With a PREFIX argument, print the result in the current buffer."
1322+
(interactive "P")
1323+
(cider-interactive-eval (cider-defun-at-point) (cider-defun-at-point-start-pos)
1324+
(when prefix (cider-eval-print-handler))))
13601325

13611326
(defun cider-pprint-eval-defun-at-point ()
13621327
"Evaluate the top-level form at point and pprint its value in a popup buffer."
13631328
(interactive)
1364-
(cider--pprint-form (cider-defun-at-point)))
1329+
(cider--pprint-eval-form (cider-defun-at-point) (cider-defun-at-point-start-pos)))
1330+
1331+
(defun cider-eval-ns-form ()
1332+
"Evaluate the current buffer's namespace form."
1333+
(interactive)
1334+
(when (clojure-find-ns)
1335+
(save-excursion
1336+
(goto-char (match-beginning 0))
1337+
(cider-eval-defun-at-point))))
1338+
1339+
1340+
;; Connection and REPL
13651341

13661342
(defun cider-insert-in-repl (form eval)
13671343
"Insert FORM in the REPL buffer and switch to it.
@@ -1466,6 +1442,9 @@ See command `cider-mode'."
14661442
For example \"foo.bar.tar\" -> \"foo.bar\"."
14671443
(cider-string-join (butlast (split-string ns "\\.")) "."))
14681444

1445+
1446+
;;; Completion
1447+
14691448
(defun cider-completing-read-var-select (prompt callback ns selected targets)
14701449
"Peform completing read using SELECTED and TARGETS.
14711450
If SELECTED is \"..\" then another selection is made for vars in the parent namespace of

0 commit comments

Comments
 (0)