-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdp.el
349 lines (300 loc) · 12 KB
/
pdp.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
;;; pdp.el --- Piglet Dev Protocol, interactive programming over websocket -*- lexical-binding: t -*-
;; Author: Arne Brasseur <[email protected]>
;; Filename: pdp.el
;; Package-Requires: ((websocket))
;; Keywords: piglet languages repl
;;; Commentary:
;;; Code:
(require 'websocket)
(require 'seq)
(require 'xref)
(require 'piglet-cbor)
(defcustom pdp-result-buffer-name "*piglet-result*"
"Buffer name to use when showing evaluation results in a separate buffer."
:type 'string
:safe #'stringp
:group 'piglet)
(defcustom pdp-result-destination 'minibuffer
"Where should, by default, evaluation results be shown"
:type '(choice (const :tag "Minibuffer" minibuffer)
(const :tag "Result buffer" result-buffer)
(const :tag "REPL buffer" repl-buffer))
:safe #'symbolp
:group 'piglet)
(defcustom pdp-pretty-print-result-p nil
"Should evaluation results by default be pretty printed?"
:type 'boolean
:safe #'booleanp
:group 'piglet)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Internals
(defvar pdp--connections
()
"List of active PDP connections.")
(defvar pdp--server
nil
"PDP websocket server, see `pdp-start-server!'")
(defvar pdp--message-counter
0
"Incrementing value used to match replies to handlers.")
(defvar pdp--handlers
()
"Association list from message number to handler function.")
;; To debug issues set this variable
;; (setq websocket-callback-debug-on-error t)
(defun pdp--msg-get (msg k)
(alist-get k msg nil nil #'equal))
(defun pdp--on-open (ws)
(add-to-list #'pdp--connections ws)
(message "[Piglet] PDP conn opened, %d active connections" (length pdp--connections)))
(defun pdp--on-message (ws frame)
(let* ((msg (piglet-cbor->elisp (websocket-frame-payload frame)))
;; (_ (message "RECEIVED %S" msg))
(op (pdp--msg-get msg :op))
(to (pdp--msg-get msg :to))
(handler (and to (alist-get to pdp--handlers))))
(when handler
(funcall handler msg))))
(defun pdp--on-close (ws)
(setq pdp--connections
(seq-remove (lambda (conn) (eq ws conn))
pdp--connections))
(message "[Piglet] PDP conn closed, %d active connections" (length pdp--connections)))
(defun pdp-start-server! ()
(interactive)
(if (not pdp--server)
(progn
(setq pdp--server
(websocket-server
17017
:host 'local
:on-open #'pdp--on-open
:on-message #'pdp--on-message
:on-close #'pdp--on-close))
(message "[Piglet] PDP server started on port: 17017"))
(message "[Piglet] PDP server already running.")))
(defun pdp-stop-server! (prefix)
"Stop the PDP server. With PREFIX, force closes the zombie server on 17017.
Force closing the server may be needed for default webscoket server when the
pointer to the existing connection is lost, mostly only useful during
development or if it got in a bad state somehow."
(interactive "P")
(when pdp--server
(websocket-server-close pdp--server))
(setq pdp--server nil)
(setq pdp--connections nil)
(when prefix
(delete-process "websocket server on port 17017"))
(message "[Piglet] PDP server stopped."))
(defun pdp-msg (kvs)
(append
kvs
(seq-remove
(lambda (pair)
(not (cdr pair)))
`((:location . ,buffer-file-name)
(:module . ,(piglet--module-name))
(:package . ,(if (boundp 'piglet-package-name)
piglet-package-name
nil))))))
(defun pdp-add-handler (msg handler)
(setq pdp--message-counter (+ pdp--message-counter 1))
(setq pdp--handlers (cons (cons pdp--message-counter handler)
pdp--handlers))
(append msg
`((:reply-to . ,pdp--message-counter))))
(defun pdp-send (msg)
;; (message "[PDP] -> %S" msg)
;; (message (apply 'concat (mapcar (lambda (x) (format "%02x" x))
;; (piglet-cbor<-elisp msg))))
(let ((payload (piglet-cbor<-elisp msg)))
(seq-do (lambda (client)
(when (websocket-openp client)
(websocket-send
client
(make-websocket-frame
:opcode 'binary
:payload payload
:completep t))))
pdp--connections)))
(defun pdp--eval-minibuffer-handler (result)
(message "=> %s" result))
(defun pdp--eval-insert-handler (result)
(when (not (and (bolp) (eolp)))
(end-of-line)
(insert "\n"))
(insert "=> ")
(insert result))
(defun pdp--eval-to-buffer-handler (result)
(with-current-buffer (get-buffer-create pdp-result-buffer-name)
(erase-buffer)
(insert result)
(goto-char (point-min))
(display-buffer (current-buffer))
(piglet-mode)))
(defun pdp--get-eval-handler (opts)
(let* ((destination (if-let ((dest (assoc 'destination opts)))
(cdr dest)
pdp-result-destination))
(pretty-print (if-let ((pp (assoc 'pretty-print opts)))
(cdr pp)
pdp-pretty-print-result-p))
(format-handler (if pretty-print
(lambda (msg) (message "Not implemented"))
(lambda (msg) (pdp--msg-get msg :result))))
(insert-handler (cl-case destination
(minibuffer #'pdp--eval-minibuffer-handler)
(result-buffer #'pdp--eval-to-buffer-handler)
(insert #'pdp--eval-insert-handler)
(repl (lambda (_) (message "Not implemented"))))))
(lambda (msg)
(funcall
insert-handler
(funcall format-handler msg)))))
(defun pdp-op-eval (code-str start line opts)
(set-text-properties 0 (length code-str) nil code-str)
(let ((msg (pdp-msg
`((:op . "eval")
(:code . ,code-str)
(:line . ,line)
(:start . ,start)))))
(pdp-send (pdp-add-handler msg (pdp--get-eval-handler opts)))))
(setq pdp--start-query
(treesit-query-compile
'piglet
"(source . (dict (keyword) @kw . (number) @val) (#equal @kw \":start\"))"))
(setq pdp--file-query
(treesit-query-compile
'piglet
"(source . (dict (keyword) @kw . (string) @val) (#equal @kw \":file\"))"))
(defun pdp-op-resolve-meta (var-sym handler)
(pdp-send
(pdp-add-handler
(pdp-msg
`((:op . "resolve-meta")
(:var . ,var-sym)))
handler)))
;; When used with a browser it's best to enable `url-handler-mode'
(defun pdp-jump-to-definition ()
(interactive)
(let ((node (treesit-node-at (point))))
(when (equal "symbol" (treesit-node-type node))
(pdp-op-resolve-meta
(treesit-node-text node)
(lambda (reply)
(xref-push-marker-stack)
(with-temp-buffer
(insert (pdp--msg-get reply :result))
(treesit-parser-create 'piglet)
(let ((file (treesit-node-text
(cdr (assoc 'val (treesit-query-capture 'piglet pdp--file-query)))))
(char (treesit-node-text
(cdr (assoc 'val (treesit-query-capture 'piglet pdp--start-query))))))
(with-current-buffer (find-file (json-parse-string file))
(goto-char (string-to-number char))))))))))
(defun pdp-eval-node (node opts)
(pdp-op-eval
(treesit-node-text node)
(treesit-node-start node)
(line-number-at-pos (treesit-node-start node))
opts))
(defun pdp--eval-prefix-to-opts (prefix-arg)
(cl-case prefix-arg
(4 '((destination . insert))) ;; C-u
(2 '((destination . result-buffer))) ;; C-u 2
(16 '((destination . result-buffer))) ;; C-u C-u
(t '())))
(defun pdp--eval (opts)
(cl-case (cdr (assoc 'form opts))
(last-sexp (let* ((start (scan-sexps (point) -1))
(node (treesit-node-at start)))
(pdp-eval-node
(if (treesit-node-check node 'named)
node
(treesit-node-parent node))
opts)))
(outer-sexp (pdp-eval-node
(alist-get
'expr
(or (treesit-query-capture 'piglet
'((list) @expr) (point) (+ (point) 1))
(treesit-query-capture 'piglet
'((dict) @expr) (point) (+ (point) 1))))
opts))
(buffer (pdp-op-eval
(buffer-substring (point-min) (point-max)) 0 0
opts))
(region (pdp-op-eval
(buffer-substring (mark) (point))
(mark)
(line-number-at-pos (mark))
opts))))
(defun pdp-eval-last-sexp (prefix)
"Evaluate the last sexp at point. If PREFIX exists or is 1 then insert
the result into the current buffer. If PREFIX is 2 then insert the result
into a *pdp-result* buffer."
(interactive "p")
(pdp--eval (cons '(form . last-sexp) (pdp--eval-prefix-to-opts prefix))))
(defun pdp-eval-outer-sexp (prefix)
"Evaluate the outermost sexp at point. If PREFIX exists or is 1 then insert
the result into the current buffer. If PREFIX is 2 then insert the result
into a *pdp-result* buffer."
(interactive "p")
(pdp--eval (cons '(form . outer-sexp) (pdp--eval-prefix-to-opts prefix))))
(defun pdp-eval-buffer (prefix)
"Evaluate the entire buffer"
(interactive "p")
(pdp--eval (cons '(form . buffer) (pdp--eval-prefix-to-opts prefix))))
(defun pdp-eval-region (prefix)
"Evaluate the currently selected region."
(interactive "p")
(pdp--eval (cons '(form . region) (pdp--eval-prefix-to-opts prefix))))
(defun pdp-toggle-result-destination ()
"Change the default location where evalation results are shown."
(interactive)
(let ((next (cl-case pdp-result-destination
(minibuffer 'result-buffer)
(result-buffer 'repl-buffer)
(repl-buffer 'minibuffer))))
(customize-save-variable 'pdp-result-destination next)
(message (concat "[PDP] Evaluation results will go to "
(cl-case next
(minibuffer "the minibuffer")
(repl-buffer "the piglet REPL buffer")
(result-buffer "a separate result buffer"))))))
(defun pdp-toggle-pretty-printing ()
"Toggle pretty printing of results on/off"
(interactive)
(let ((next (not pdp-pretty-print-result-p)))
(customize-save-variable 'pdp-pretty-print-result-p next)
(message (concat "[PDP] Pretty-printing has been turned "
(if next "on." "off.")))))
;; Generate all permutations, for direct access that overrides the toggles
(dolist (form '((last-sexp . "the last expression before point")
(outer-sexp . "the top-level expression at point")
(buffer . "the entire buffer")
(region . "the currently selected region")))
(dolist (destination '((minibuffer . "in the minibuffer")
(insert . "inserted into the current buffer")
(result-buffer . "in a separate result buffer")
(repl . "inserted into the REPL buffer")))
(dolist (pretty-print '(nil t))
(defalias (intern (concat "pdp-eval-"
(symbol-name (car form))
"-to-"
(symbol-name (car destination))
(if pretty-print
"-pretty-print"
"")))
`(lambda ()
,(concat "Evaluate " (cdr form) " and show the result\n" (cdr destination)
(if pretty-print
", pretty-printed."
"."))
(interactive)
(pdp--eval '((form . ,(car form))
(destination . ,(car destination))
(pretty-print . ,pretty-print))))))))
(provide 'pdp)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; pdp.el ends here