-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharei-evaluation.el
238 lines (197 loc) · 7.96 KB
/
arei-evaluation.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;;; arei-evaluation.el --- Evaluation for Arei -*- lexical-binding: t; -*-
;; Copyright © 2023, 2024 Andrew Tropin <[email protected]>
;; Copyright © 2024 Nikita Domnitskii
;; Author: Andrew Tropin <[email protected]>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Evaluation for Arei
;;; Code:
(require 'arei-client)
(require 'arei-syntax)
(require 'arei-nrepl)
(require 'arei-ui)
(eval-when-compile (require 'map))
(eval-when-compile (require 'pcase))
(defun arei--send-stdin ()
(arei-client-send-request
(arei-nrepl-dict
"op" "stdin"
"stdin"
(condition-case nil
(concat (read-from-minibuffer "Stdin: " nil) "\n")
(quit nil)))
#'ignore
(arei--user-evaluation-session-id)))
(defun arei--process-user-eval-response-callback
(connection-buffer &optional expression-end)
"Set up a handler for eval request responses."
(let ((vals nil))
(lambda (response)
(pcase response
((map status value out err)
;; Jump to specific position only when there is something to print
(when (or out err value)
(goto-char (point-max)))
(when (member "need-input" status)
(arei--send-stdin))
(when out
(insert out))
(when err
(insert (propertize err 'face
'((t (:inherit font-lock-warning-face))))))
(when value
(unless (= 0 (current-column))
(insert "\n"))
(insert (propertize value 'face
'((t (:inherit font-lock-string-face)))))
(insert "\n"))
(when (and (member "multiple-values" status)
(not (member "done" status)))
(push value vals))
(when (member "done" status)
(with-current-buffer connection-buffer
(let* ((value (or (and (member "multiple-values" status)
(if vals
(mapconcat
(lambda (v) (concat " => " v))
(reverse vals)
"\n")
" => "))
(and value (concat " => " value))))
(fmt (if value "%s" " ;; interrupted")))
(arei-ui-show-result fmt value expression-end))))
(when (get-buffer-window)
(set-window-point (get-buffer-window) (buffer-size))))))))
(defun arei--request-user-eval (code &optional bounds)
(when (arei-connected-p)
(arei-ui-blink-region bounds))
(pcase-let* ((`(,start . ,end) bounds)
(code (or code
(buffer-substring-no-properties start end)))
(request (arei-nrepl-dict
"op" "eval"
"code" code
"file" (buffer-file-name))))
(when-let* ((module (arei-current-module)))
(arei-nrepl-dict-put request "ns" module))
(when-let* ((line (and start (1- (line-number-at-pos start)))))
(arei-nrepl-dict-put request "line" line))
(when-let* ((column (and start (save-excursion
(goto-char start)
(current-column)))))
(arei-nrepl-dict-put request "column" column))
(arei-client-send-request
request
(arei--process-user-eval-response-callback (current-buffer) end)
(arei--user-evaluation-session-id))))
;;;
;;; APIs
;;;
(defun arei-interrupt-evaluation (&optional session-id)
"Interrupt evaluation for a particular SESSION-ID, if no
SESSION-ID specified interrupt default user's evaluation session."
(interactive)
(arei-client-send-request
(arei-nrepl-dict "op" "interrupt")
#'ignore
(or session-id (arei--user-evaluation-session-id))))
(defun arei-interrupt-tooling-evaluation ()
"Interrupt evaluation in tooling session."
(interactive)
(arei-interrupt-evaluation (arei--tooling-session-id)))
(defun arei--sync-eval-timeout-callback (session-id)
"Interrupt evaluation, when request timeouted."
(lambda (final-request)
(arei-client-send-sync-request
(arei-nrepl-dict
"op" "interrupt"
"interrupt-id" (arei-nrepl-dict-get final-request "id"))
session-id)))
(defun arei--sync-eval (exp &optional module session-id)
"Try to syncronously evaluate EXP and if timeout reached, interrupt
evaluation. You can dynamically bind `arei-client-sync-timeout'
to change evaluation timeout.
Example:
(let ((arei-client-sync-timeout 10))
(arei--get-expression-value \"(begin (sleep 7) \\='hi)\"))
If MODULE is set evaluate in its context, otherwise use
`arei-current-module'.
If SESSION-ID is set use it, otherwise call
`arei--tooling-session-id' and use its value."
(let ((request (arei-nrepl-dict
"op" "eval"
"code" exp)))
(when-let* ((module (or module (arei-current-module))))
(arei-nrepl-dict-put request "ns" module))
(let ((session-id (or session-id (arei--tooling-session-id))))
(arei-client-send-sync-request
request
session-id
(arei--sync-eval-timeout-callback session-id)))))
(defun arei--get-expression-value (exp)
(arei-nrepl-dict-get (arei--sync-eval exp) "value"))
(defun arei--eval (exp &optional callback module session-id)
"Asncronously evaluate EXP.
If MODULE is set evaluate in its context, otherwise use
`arei-current-module'.
If SESSION-ID is set use it, otherwise call
`arei--tooling-session-id' and use its value.
"
(let ((request (arei-nrepl-dict
"op" "eval"
"code" exp)))
(when-let* ((module (or module (arei-current-module))))
(arei-nrepl-dict-put request "ns" module))
(let ((session-id (or session-id (arei--tooling-session-id))))
(arei-client-send-request
request
(or callback (lambda (response) (message "response: %s" response)))
session-id))))
;;;
;;; Wrappers for user-eval
;;;
(defun arei-evaluate-region (start end)
(interactive "r")
(arei--request-user-eval nil (cons start end)))
(defun arei-evaluate-sexp (exp)
"Evaluate EXP. When called interactively, read an expression and
evaluate it. It's similiar to Emacs' `eval-expression' by spirit."
(interactive
(list (read-from-minibuffer "Expression: " nil nil nil 'arei-expression)))
(arei--request-user-eval exp))
(defun arei-evaluate-last-sexp ()
(interactive)
(arei--request-user-eval nil (arei-syntax-last-sexp-bounds)))
(defun arei-evaluate-buffer ()
(interactive)
(arei--request-user-eval nil (arei-syntax-buffer-bounds)))
(defun arei-evaluate-enclosing-outer-form ()
"Evaluate top-level form AKA defun.
In the future it may start accepting universal argument to narrow
down to non-top level forms."
(interactive)
(arei--request-user-eval nil (arei-syntax-current-top-level-form)))
(defun arei-evaluate-enclosing-inner-form ()
"Evaluate nearest enclosing form, basically a list under point.
In the future it may start accepting universal argument to widen
up to next enclosing forms."
(interactive)
(arei--request-user-eval nil (arei-syntax-list-at-point)))
(defvar-keymap arei-evaluation-keymap
"C-e" #'arei-evaluate-last-sexp
"C-b" #'arei-evaluate-buffer
"C-r" #'arei-evaluate-region
"C-t" #'arei-evaluate-enclosing-outer-form
":" #'arei-evaluate-sexp
"C-i" #'arei-interrupt-evaluation)
(provide 'arei-evaluation)
;;; arei-evaluation.el ends here