-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
indium-client.el
352 lines (288 loc) · 12.1 KB
/
indium-client.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
350
351
352
;;; indium-client.el --- Indium process client -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Nicolas Petton
;; Author: Nicolas Petton <[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:
;; The Indium process client starts and communicates with an "indium" process.
;;
;; Make sure to install the indium process with:
;; npm install -g indium
;;; Code:
(require 'json)
(require 'map)
(require 'subr-x)
(require 'json-process-client)
(require 'indium-structs)
(defcustom indium-client-closed-hook nil
"Hook called after a client is closed."
:group 'indium-client
:type 'hook)
(defcustom indium-client-connected-hook nil
"Hook called after a client is connected."
:group 'indium-client
:type 'hook)
(defcustom indium-client-log-hook nil
"Hook called when a client receives a log event."
:group 'indium-client
:type 'hook)
(defcustom indium-client-breakpoint-resolved-hook nil
"Hook called upon breakpoint resolution."
:group 'indium-client
:type 'hook)
(defcustom indium-client-debugger-resumed-hook nil
"Hook called when the debugger is resumed."
:group 'indium-client
:type 'hook)
(defcustom indium-client-debugger-paused-hook nil
"Hook called when the debugger is paused."
:group 'indium-client
:type 'hook)
(defvar indium-client-debug nil
"When non-nil, log server output to *indium-client-log*.")
(defun indium-client-find-executable ()
"Return the indium executable file."
(if-let ((lisp-filename (or load-file-name (buffer-file-name))))
(let ((executable (thread-last
(file-name-directory lisp-filename)
(expand-file-name "server")
(expand-file-name "bin")
(expand-file-name "indium"))))
(if (file-executable-p executable)
executable
(indium-client-default-executable)))
(indium-client-default-executable)))
(defun indium-client-default-executable ()
"Return the default process executable."
"indium")
(defcustom indium-client-executable (indium-client-find-executable)
"Process executable."
:group 'indium-client
:type 'file)
(defvar indium-client--application nil
"The client connection as returned by `json-process-client-start'.")
(defvar indium-client--process-port 13840
"The port on which the server should listen.")
(defun indium-client-start (callback)
"Start an Indium process and store it as the client process.
Evaluate CALLBACK once the server is started."
(when (indium-client-process-live-p)
(user-error "An indium process is already running"))
(let ((executable (executable-find indium-client-executable)))
(unless executable
(user-error "Cannot find the indium executable. Please run \"npm install -g indium\""))
(setq indium-client--application
(json-process-client-start-with-id
:name "indium"
:executable executable
:port indium-client--process-port
:started-regexp "server listening"
:tcp-started-callback callback
:exec-callback #'indium-client--handle-message
:debug "*indium-debug-log*"
:args (list (number-to-string indium-client--process-port))))))
(defun indium-client-stop ()
"Stop the indium process."
(json-process-client-stop indium-client--application)
(setq indium-client--application nil)
(run-hooks 'indium-client-closed-hook))
(defun indium-client-send (message &optional callback)
"Send MESSAGE to the Indium process.
When CALLBACK is non-nil, evaluate it with the process response."
(json-process-client-send indium-client--application message callback))
(defun indium-client-list-configurations (directory &optional callback)
"Request the list of configurations found in DIRECTORY.
Evaluate CALLBACK with the result."
(indium-client-send `((type . "configurations")
(payload . ((action . "list")
(directory . ,directory))))
callback))
(defun indium-client-connect (directory name)
"Connect to a runtime.
DIRECTORY is the path of the directory where the project file can be found.
NAME is the name of the configuration to use for connecting.
Once the client is connected, run the hook `indium-client-connected-hook'."
(indium-client-send `((type . "connection")
(payload . ((action . "connect")
(directory . ,directory)
(name . ,name))))
(lambda (&rest _)
(run-hooks 'indium-client-connected-hook))))
(defun indium-client-disconnect (&optional callback)
"Disconnect from the runtime, but do not stop the indium process.
When non-nil, evaluate CALLBACK with the result."
(indium-client-send `((type . "connection")
(payload . ((action . "disconnect"))))
callback))
(defun indium-client-evaluate (expression &optional frame callback)
"Evaluate EXPRESSION in the context of FRAME.
When non-nil, evaluate CALLBACK with the result."
(indium-client-send
`((type . "runtime")
(payload . ((action . "evaluate")
(expression . ,expression)
(frameId . ,(when frame (indium-frame-id frame))))))
(lambda (obj)
(when callback
(funcall callback (indium-remote-object-from-alist obj))))))
(defun indium-client-get-completion (expression &optional frame callback)
"Request the list of completion for EXPRESSION in the context of FRAME.
When non-nil, evaluate CALLBACK with the result."
(indium-client-send `((type . "runtime")
(payload . ((action . "getCompletion")
(expression . ,expression)
(frameId . ,(when frame (indium-frame-id frame))))))
callback))
(defun indium-client-get-properties (id &optional callback)
"Request the list of properties for the remote object with ID.
When non-nil, evaluate CALLBACK with the result."
(indium-client-send
`((type . "runtime")
(payload . ((action . "getProperties")
(id . ,id))))
(lambda (properties)
(when callback
(funcall callback (seq-map #'indium-property-from-alist
properties))))))
(defun indium-client-activate-breakpoints ()
"Activate all breakpoints."
(indium-client-send `((type . "runtime")
(payload . ((action . "activateBreakpoints"))))))
(defun indium-client-deactivate-breakpoints ()
"Deactivate all breakpoints."
(indium-client-send `((type . "runtime")
(payload . ((action . "deactivateBreakpoints"))))))
(defun indium-client-add-breakpoint (breakpoint)
"Request the addition of BREAKPOINT."
(let* ((id (indium-breakpoint-id breakpoint))
(location (indium-breakpoint-location breakpoint))
(file (indium-location-file location))
(line (indium-location-line location))
(column (indium-location-column location)))
(indium-client-send `((type . "runtime")
(payload . ((action . "addBreakpoint")
(id . ,id)
(file . ,(indium-client--convert-path file))
(line . ,line)
(column . ,column)))))))
(defun indium-client-remove-breakpoint (breakpoint)
"Request the removal of BREAKPOINT."
(let ((id (indium-breakpoint-id breakpoint)))
(indium-client-send `((type . "runtime")
(payload . ((action . "removeBreakpoint")
(id . ,id)))))))
(defun indium-client-resume ()
"Resume the runtime execution."
(indium-client-send `((type . "runtime")
(payload . ((action . "resume"))))))
(defun indium-client-step-into ()
"Request a step into."
(indium-client-send `((type . "runtime")
(payload . ((action . "stepInto"))))))
(defun indium-client-step-out ()
"Request a step out."
(indium-client-send `((type . "runtime")
(payload . ((action . "stepOut"))))))
(defun indium-client-step-over ()
"Request a step over."
(indium-client-send `((type . "runtime")
(payload . ((action . "stepOver"))))))
(defun indium-client-continue-to-location (location)
"Request the runtime to resume until LOCATION is reached."
(indium-client-send
`((type . "runtime")
(payload . ((action . "continueToLocation")
(location . ((file . ,(indium-client--convert-path
(indium-location-file location)))
(line . ,(indium-location-line location))
(column . ,(indium-location-column location)))))))))
(defun indium-client-get-frame-source (frame &optional callback)
"Request the source of FRAME.
When CALLBACK is non-nil, evaluate it with the source"
(indium-client-send
`((type . "runtime")
(payload . ((action . "getSource")
(id . ,(indium-frame-script-id frame)))))
callback))
(defun indium-client-get-sourcemap-sources (&optional callback)
"Request the all the sourcemap source paths.
When CALLBACK is non-nil, evaluate it with the list of sources."
(indium-client-send
`((type . "runtime")
(payload . ((action . "getSourcemapSources"))))
callback))
(defun indium-client-get-script-sources (&optional callback)
"Request the all the script source paths.
When CALLBACK is non-nil, evaluate it with the list of sources."
(indium-client-send
`((type . "runtime")
(payload . ((action . "getScriptSources"))))
callback))
(defun indium-client-process-live-p ()
"Return non-nil if the indium process is running."
(json-process-client-process-live-p indium-client--application))
(defun indium-client--handle-message (data callback)
"Handle a server message with DATA.
If DATA is a successful response to a previously-sent message,
evaluate CALLBACK with the payload."
(let-alist data
(pcase .type
("error" (indium-client--handle-error .payload))
("success" (indium-client--handle-response .payload callback))
("notification" (indium-client--handle-notification .payload))
("log" (indium-client--handle-log .payload)))))
(defun indium-client--handle-error (payload)
"Handle an error from the server.
PAYLOAD is an alist containing the details of the error."
(let-alist payload
(message "Indium server error: %s" .error)))
(defun indium-client--handle-response (payload callback)
"Handle a response to a client request.
PAYLOAD contains the data of the response.
If CALLBACK is non-nil, evaluate it with the PAYLOAD."
(when callback
(unwind-protect
(funcall callback payload))))
(defun indium-client--handle-log (payload)
"Handle a log event from the server.
PAYLOAD is an alist with the details of the log event.
If has the following keys:
type type of message
url url of the message origin
line line number in the resource that generated this message
result object to be logged."
(setf (map-elt payload 'result nil #'equal) (indium-remote-object-from-alist
(map-elt payload 'result)))
(run-hook-with-args 'indium-client-log-hook
payload))
(defun indium-client--handle-notification (payload)
"Handle a notification event sent from the server.
PAYLOAD is an alist with the details of the notification."
(let-alist payload
(pcase .type
("breakpointResolved"
(progn
(run-hook-with-args 'indium-client-breakpoint-resolved-hook .id .line)))
("paused"
(run-hook-with-args 'indium-client-debugger-paused-hook
(seq-map #'indium-frame-from-alist .frames)
.reason
.description))
("resumed"
(run-hooks 'indium-client-debugger-resumed-hook))
(_ (message "Indium notification %s" payload)))))
(defun indium-client--convert-path (path)
"Convert PATH to a system path that the server component understands."
(when (eq system-type 'windows-nt)
(setq path (replace-regexp-in-string "/" "\\" path nil t)))
path)
(provide 'indium-client)
;;; indium-client.el ends here