@@ -1013,33 +1013,61 @@ If invoked with OUTPUT-TO-CURRENT-BUFFER, output the result to current buffer."
1013
1013
" The previous evaluation context if any.
1014
1014
That's set by commands like `cider-eval-last-sexp-in-context' ." )
1015
1015
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 )
1017
1037
" Evaluate CODE in user-provided evaluation context."
1018
1038
(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))))
1022
1044
(code (concat " (let [" eval-context " ]\n " code " )" )))
1045
+ (setq-local cider-previous-eval-context eval-context)
1023
1046
(cider-interactive-eval code
1024
1047
nil
1025
1048
nil
1026
- (cider--nrepl-pr-request-map))
1027
- (setq-local cider-previous-eval-context eval-context)))
1049
+ (cider--nrepl-pr-request-map))))
1028
1050
1029
- (defun cider-eval-last-sexp-in-context ()
1051
+ (defun cider-eval-last-sexp-in-context (guess )
1030
1052
" Evaluate the preceding sexp in user-supplied context.
1031
1053
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.
1035
1055
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.
1038
1063
1039
1064
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))
1043
1071
1044
1072
(defun cider-eval-defun-to-comment (&optional insert-before )
1045
1073
" Evaluate the \" top-level\" form and insert result as comment.
0 commit comments