Skip to content

Commit fef392b

Browse files
yuhan0bbatsov
authored andcommitted
Guess evaluation context
1 parent cfea755 commit fef392b

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

cider-eval.el

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,33 +1013,61 @@ If invoked with OUTPUT-TO-CURRENT-BUFFER, output the result to current buffer."
10131013
"The previous evaluation context if any.
10141014
That's set by commands like `cider-eval-last-sexp-in-context'.")
10151015

1016-
(defun cider--eval-in-context (code)
1016+
1017+
(defun cider--guess-eval-context ()
1018+
"Return the context suitable for input to `cider--eval-in-context'
1019+
by extracting all parent let bindings."
1020+
(save-excursion
1021+
(let ((res ""))
1022+
(condition-case er
1023+
(while t
1024+
(backward-up-list)
1025+
(when (looking-at (rx "(" (or "when-let" "if-let" "let") (opt "*")
1026+
symbol-end (* space)
1027+
(group "["))) ;; binding vector
1028+
(let ((beg (match-end 1))
1029+
(end (save-excursion
1030+
(goto-char (match-beginning 1))
1031+
(forward-sexp 1)
1032+
(1- (point)))))
1033+
(setq res (concat (buffer-substring-no-properties beg end) ", " res)))))
1034+
(scan-error res)))))
1035+
1036+
(defun cider--eval-in-context (code &optional guess)
10171037
"Evaluate CODE in user-provided evaluation context."
10181038
(let* ((code (string-trim-right code))
1019-
(eval-context (read-string
1020-
(format "Evaluation context (let-style) for `%s': " code)
1021-
cider-previous-eval-context))
1039+
(eval-context
1040+
(minibuffer-with-setup-hook (when guess #'beginning-of-buffer)
1041+
(read-string "Evaluation context (let-style): "
1042+
(if guess (cider--guess-eval-context)
1043+
cider-previous-eval-context))))
10221044
(code (concat "(let [" eval-context "]\n " code ")")))
1045+
(setq-local cider-previous-eval-context eval-context)
10231046
(cider-interactive-eval code
10241047
nil
10251048
nil
1026-
(cider--nrepl-pr-request-map))
1027-
(setq-local cider-previous-eval-context eval-context)))
1049+
(cider--nrepl-pr-request-map))))
10281050

1029-
(defun cider-eval-last-sexp-in-context ()
1051+
(defun cider-eval-last-sexp-in-context (guess)
10301052
"Evaluate the preceding sexp in user-supplied context.
10311053
The context is just a let binding vector (without the brackets).
1032-
The context is remembered between command invocations."
1033-
(interactive)
1034-
(cider--eval-in-context (cider-last-sexp)))
1054+
The context is remembered between command invocations.
10351055
1036-
(defun cider-eval-sexp-at-point-in-context ()
1037-
"Evaluate the preceding sexp in user-supplied context.
1056+
When GUESS is non-nil, or called interactively with \\[universal-argument],
1057+
attempt to guess the context from parent let bindings."
1058+
(interactive "P")
1059+
(cider--eval-in-context (cider-last-sexp) guess))
1060+
1061+
(defun cider-eval-sexp-at-point-in-context (guess)
1062+
"Evaluate the sexp around point in user-supplied context.
10381063
10391064
The context is just a let binding vector (without the brackets).
1040-
The context is remembered between command invocations."
1041-
(interactive)
1042-
(cider--eval-in-context (cider-sexp-at-point)))
1065+
The context is remembered between command invocations.
1066+
1067+
When GUESS is non-nil, or called interactively with \\[universal-argument],
1068+
attempt to guess the context from parent let bindings."
1069+
(interactive "P")
1070+
(cider--eval-in-context (cider-sexp-at-point) guess))
10431071

10441072
(defun cider-eval-defun-to-comment (&optional insert-before)
10451073
"Evaluate the \"top-level\" form and insert result as comment.

0 commit comments

Comments
 (0)