-
Notifications
You must be signed in to change notification settings - Fork 1
/
ide.rkt
507 lines (424 loc) · 17.8 KB
/
ide.rkt
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
;; The MIT License (MIT)
;;
;; Copyright (c) 2013 Matthew C. Jadud
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
#lang racket
(require yaml
racket/gui
framework
racket/runtime-path
)
(require "tabbed-texts.rkt"
"model-plumb.rkt"
"menu-examples.rkt"
"debug.rkt"
"mvc.rkt"
"util.rkt"
"util-gui.rkt"
"version.rkt"
;;
"config.rkt"
)
;; To reference client.yaml
;; (define-runtime-path here (build-path "."))
(define NUMBER-OF-ERROR-LINES 3)
(define ide%
(class view%
(field [f false]
[menu false]
[ribbon false]
[err-msg-text false]
[tabbed-texts false]
[canvas false]
[contents (make-vector 255 false)]
[feedback false]
[compile-driver false]
[hardware false]
[network false]
[show-debug? false]
)
;; ---------------------------------------------------------------------------
; ;;;;; ;; ;;;;;; ;;;;;; ;;;; ;; ;;
; ;;;;;;; ;; ;; ;; ;; ;; ;;;;;;;; ;;; ;;
; ;; ;; ;; ;; ;;; ;; ;;; ;;; ;;; ;;;; ;;
; ;; ;; ;; ;; ;;; ;; ;;; ;; ;; ;;;;; ;;
; ;; ;; ;; ;; ;; ;; ;; ; ; ;; ;; ;;
; ;;;;;; ;; ;;;;;;; ;;;;;;; ; ; ;; ;; ;;
; ;;;;;; ;; ;; ;;; ;; ;;; ; ; ;; ;; ;;
; ;; ;; ;; ;; ;;; ;; ;;; ;; ;; ;; ;;;;;
; ;; ;; ;; ;; ;;; ;; ;;; ;;; ;; ;; ;;;;
; ;; ;; ;; ;;;;;;; ;;;;;;; ;;;;;;;; ;; ;;;
; ;; ;;;;; ;;;;;; ;;;;;; ;;;; ;; ;;
;
;
;
;
;; ---------------------------------------------------------------------------
(define (build-ribbon rent)
(define RIBBON-HEIGHT 50)
(set! ribbon (new group-box-panel%
[parent rent]
[label ""]
[horiz-margin 5]
[stretchable-height false]
[min-height RIBBON-HEIGHT]
))
(define h1 (new horizontal-panel%
[parent ribbon]))
(define (compile-check-callback kind)
(λ (b e)
(send hardware set-error-message "")
(send err-msg-text erase)
(update)
(let ([current-file (send tabbed-texts get-filename)])
(cond
[current-file
;; Save file before compiling.
(send tabbed-texts save-file)
(debug 'IDE-COMPILE "COMPILE FILE: ~a" current-file)
;; Set the main file
(send hardware set-main-file current-file)
(debug 'IDE-COMPILE "Main file: ~a" (send hardware get-main-file))
(case kind
[(compile)
(send hardware compile)]
[(check)
(send hardware check-syntax)])]
[else
(case kind
[(compile)
(send f set-status-text
"File must be saved before it can be compiled.")]
[(check)
(send f set-status-text
"File must be saved before it can be compiled.")])]
))))
(define check-code (new button%
[parent h1]
[label "Check Code"]
[stretchable-height true]
[stretchable-width true]
[callback (compile-check-callback 'check)]))
(define compile-code (new button%
[parent h1]
[label
(let ([boards (send hardware get-board-choices)])
(cond
[(empty? boards)
"No board configurations found."]
[else (format "Send code to ~a" (first boards))]))]
[stretchable-width true]
[callback (compile-check-callback 'compile)]))
(define h2 (new horizontal-panel%
[parent ribbon]))
(define board (new choice%
[parent h2]
[label "Board"]
[stretchable-width true]
[choices (send hardware get-board-choices)]
[callback (λ (v e)
(let ([selection-string
(send board get-string
(send board get-selection))])
(send compile-code set-label
(format "Send code to ~a" selection-string))
(send hardware set-board-type selection-string)
(update)))]
))
(define serial-port (new choice%
[parent h2]
[label "Port"]
[stretchable-width true]
[choices
(send hardware get-arduino-ports)]
[callback (λ (v e)
(send hardware set-arduino-port
(send serial-port get-string
(send serial-port get-selection)))
(update))]))
(define refresh (new button%
[parent h2]
[label "Refresh Ports"]
[callback (λ (b e)
(send serial-port clear)
(send hardware enumerate-arduinos)
(let ([arduinos
(send hardware get-arduino-ports)])
(map (λ (i)
(send serial-port append i))
arduinos)
(unless (zero? (send serial-port get-number))
(send hardware set-arduino-port
(send serial-port get-string
(send serial-port
get-selection))))
(update)
))]))
;; Need to update the model with
;; the current board, port
(unless (zero? (send board get-number))
(send hardware set-board-type
(send board get-string
(send board get-selection))))
(unless (zero? (send serial-port get-number))
(send hardware set-arduino-port
(send serial-port get-string
(send serial-port get-selection))))
)
(define/public (close-tab)
(send tabbed-texts close-tab))
;; ---------------------------------------------------------------------------
; ;; ;; ;;;;;;; ;; ;; ;; ;;
; ;;; ;;; ;;;;;;; ;;; ;; ;; ;;
; ;;;; ;;;; ;; ;;;; ;; ;; ;;
; ;; ;; ;; ;; ;; ;;;;; ;; ;; ;;
; ;; ;; ;; ;; ;;;;;; ;; ;; ;; ;; ;;
; ;; ;;; ;; ;;;;;; ;; ;; ;; ;; ;;
; ;; ; ;; ;; ;; ;; ;; ;; ;;
; ;; ;; ;; ;; ;;;;; ;; ;;
; ;; ;; ;; ;; ;;;; ;; ;;
; ;; ;; ;; ;; ;;; ;;;;;;;;
; ;; ;; ;;;;;;; ;; ;; ;;;;
;
;
;
;
;; ---------------------------------------------------------------------------
(define (build-menu)
(define menu-bar (new menu-bar%
[parent f]))
(define file (new menu%
[label "&File"]
[parent menu-bar]))
(new menu-item%
[label "&New Tab"]
[parent file]
[callback (λ (m e)
(send tabbed-texts new-document))])
(new menu-item%
[label "Open"]
[parent file]
[callback
(λ (m e)
(let ([f (get-file "Open File")])
(when (and f (file-exists? f))
(send tabbed-texts open-file f)
)))])
(new menu-item%
[label "Save"]
[parent file]
[callback (λ (m e)
(send tabbed-texts save))])
(new menu-item%
[label "Save As"]
[parent file]
[callback
(λ (m e)
(send tabbed-texts save-as))]
)
(new menu-item%
[label "Close Tab"]
[parent file]
[callback
(λ (m e)
(send tabbed-texts close-tab))]
)
;; EXAMPLES MENU
(define examples (new menu%
[label "&Examples"]
[parent menu-bar]))
;; Loads stuff from servers
(define (replace-tags-in-code conf)
(let ([code (send hardware get-static #:as 'text "ide" "plumbing-examples" (hash-ref conf 'path))]
[result '()])
;; Append a standard header
(when (and (hash-has-key? conf 'name)
(hash-has-key? conf 'tweet)
(hash-has-key? conf 'author)
(hash-has-key? conf 'email))
(set! code
(cons
"-- {{name}}\n-- {{tweet}}\n-- {{author}} ({{email}})\n\n"
code)))
(for ([line code])
(for ([key (hash-keys conf)])
(set! line (regexp-replace* (format "{{~a}}" key)
line
(hash-ref conf key))))
(set! result (snoc result (format "~a~n" line))))
(apply string-append result)))
(define example-submenus
(new menu-examples%
[model hardware]
[main this]
[menu examples]
[callback (λ (s)
(λ (m e)
(send tabbed-texts new-document
#:content
(replace-tags-in-code s))))]
))
;; 20150609 MCJ
;; This no longer exists... we don't upload things separately.
#|
(define arduino (new menu%
[label "Hardware"]
[parent menu-bar]))
(define vm (new menu-item%
[label "Upload Virtual Machine"]
[parent arduino]
[callback
(λ (m e)
(send hardware user-upload-firmware))]))
|#
(define help (new menu%
[label "&Help"]
[parent menu-bar]))
(new menu-item%
[label "Show Debug Window"]
[parent help]
[callback
(λ (m e)
(toggle-debug-window))])
(new menu-item%
[label (format "Version: ~a" VERSION)]
[parent help]
[callback (λ (m e) '...)])
'DONEWITHMENU
)
(define debug-frame%
(class frame%
(define/augment (on-close)
(set! show-debug? false))
(super-new)))
(define gui-debug-window
(new debug-frame%
[width 600]
[label "Debug Messages"]
))
(define debug-msg-canvas (new editor-canvas%
(parent gui-debug-window)
(label "")
(stretchable-width true)
(stretchable-height true)
(line-count 30)
))
(define keymap (keymap:get-editor))
(define debug-msg-text (new text%
(auto-wrap true)
))
(send debug-msg-text set-keymap keymap)
(send debug-msg-canvas set-editor debug-msg-text)
(define (toggle-debug-window)
(set! show-debug? (not show-debug?))
;; Kill the old thread
(when debug-thread
(kill-thread debug-thread)
(set-debug-thread! false))
(if show-debug?
;; Start a new one piping to the editor component
(set-gui-debug)
(set-textual-debug))
(send gui-debug-window show show-debug?)
)
(define (set-gui-debug)
(let ([c (make-channel)])
(set-debug-channel! c)
(when debug-thread
(kill-thread debug-thread))
(set-debug-thread! (thread (λ ()
(let loop ()
(send debug-msg-text insert
(channel-get c)
(send debug-msg-text last-position))
(loop)))))
))
;; ---------------------------------------------------------------------------
;; BUILD-IDE
;; ---------------------------------------------------------------------------
(define (build-ide)
(set! f (new frame%
[width 600]
[height 800]
[label "Plumb"]))
(send f create-status-line)
(set! tabbed-texts (new tabbed-texts%
[parent f]))
(build-ribbon f)
(define err-msg-canvas (new editor-canvas%
(parent f)
(label "")
(stretchable-width true)
(stretchable-height false)
(line-count NUMBER-OF-ERROR-LINES)
))
(set! err-msg-text (new text% (auto-wrap true)))
(send err-msg-canvas set-editor err-msg-text)
(build-menu)
'OK
)
(define/public (create)
;; Interaction
;; Need this to build the IDE
(set! hardware (new plumb%))
;; (send hardware load-config)
;; Load our configuration (client.yaml by default)
(load-additional-client-config)
(config-file (build-path (conf-get 'CLIENT-CONFIG) "client.yaml"))
(debug 'CONFIGLOAD "~s" (config-file))
(load-config)
(conf-add "boards"
(string->yaml
(read-url (format "http://~a:~a/ide/boards.yaml"
(conf-get 'server)
(conf-get 'port)))))
(send hardware enumerate-arduinos)
(send hardware compilation-server-config)
(send hardware add-view this)
(send hardware say-hello)
(enable-debug! 'ALL)
(set-textual-debug)
(build-ide)
(send f show true)
(check-version hardware f)
)
(define/public (show bool)
(send f show bool))
;; ---------------------------------------------------------------------------
;; UPDATE-MODEL
;; ---------------------------------------------------------------------------
(define (update-model)
'DoNothing
)
(define/override (update)
;; On update, update the status text
(send f set-status-text (send hardware get-message))
;; Clear the last status message
(let ([err-msg (send hardware get-error-message)])
(when (string? err-msg)
(send err-msg-text insert err-msg)
(send tabbed-texts highlight-line (send hardware get-error-line))
)))
(super-new)
))
(define ide (new ide%))
(set-textual-debug)
(enable-debug! 'ALL)
(send ide create)