-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsnippet.gleam
More file actions
1273 lines (1135 loc) · 34.8 KB
/
snippet.gleam
File metadata and controls
1273 lines (1135 loc) · 34.8 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import eyg/analysis/inference/levels_j/contextual
import eyg/analysis/type_/binding
import eyg/analysis/type_/binding/debug
import eyg/analysis/type_/isomorphic as t
import eyg/runtime/break
import eyg/runtime/interpreter/block
import eyg/runtime/interpreter/state as istate
import eyg/runtime/value as v
import eyg/sync/sync
import eyg/website/run
import eygir/annotated
import eygir/decode
import eygir/encode
import gleam/dict
import gleam/dynamicx
import gleam/int
import gleam/io
import gleam/javascript/promise
import gleam/list
import gleam/listx
import gleam/option.{type Option, None, Some}
import gleam/string
import lustre/attribute as a
import lustre/element
import lustre/element/html as h
import lustre/event
import morph/action
import morph/analysis
import morph/editable as e
import morph/input
import morph/lustre/frame
import morph/lustre/render
import morph/navigation
import morph/picker
import morph/projection as p
import morph/transformation
import plinth/browser/clipboard
import plinth/browser/document
import plinth/browser/element as dom_element
import plinth/browser/event as pevent
import plinth/browser/window
import plinth/javascript/console
import website/components/output
type ExternalBlocking =
fn(run.Value) -> Result(promise.Promise(run.Value), run.Reason)
type EffectSpec =
#(binding.Mono, binding.Mono, ExternalBlocking)
pub type Status {
Idle
Editing(Mode)
}
type Path =
Nil
type Value =
v.Value(Path, #(List(#(istate.Kontinue(Path), Path)), istate.Env(Path)))
type Scope =
List(#(String, Value))
pub type History {
History(undo: List(p.Projection), redo: List(p.Projection))
}
pub type Failure {
NoKeyBinding(key: String)
ActionFailed(action: String)
}
pub type Mode {
Command(failure: Option(Failure))
Pick(picker: picker.Picker, rebuild: fn(String) -> p.Projection)
EditText(String, fn(String) -> p.Projection)
EditInteger(Int, fn(Int) -> p.Projection)
}
pub type Snippet {
Snippet(
status: Status,
expanding: Option(List(Int)),
source: #(p.Projection, e.Expression, Option(analysis.Analysis)),
using_mouse: Bool,
history: History,
run: run.Run,
scope: Scope,
effects: List(#(String, EffectSpec)),
cache: sync.Sync,
)
}
pub fn init(editable, scope, effects, cache) {
let editable = e.open_all(editable)
let proj = navigation.first(editable)
Snippet(
Idle,
None,
new_source(proj, editable, scope, effects, cache),
False,
History([], []),
run.start(editable, scope, effects, cache),
scope,
effects,
cache,
)
}
pub fn active(editable, scope, effects, cache) {
let editable = e.open_all(editable)
let proj = navigation.first(editable)
Snippet(
Editing(Command(None)),
None,
new_source(proj, editable, scope, effects, cache),
False,
History([], []),
run.start(editable, scope, effects, cache),
scope,
effects,
cache,
)
}
fn new_source(proj, editable, scope, effects, cache) {
let eff =
effect_types(effects)
|> list.fold(t.Empty, fn(acc, new) {
let #(label, #(lift, reply)) = new
t.EffectExtend(label, #(lift, reply), acc)
})
let analysis =
analysis.do_analyse(
editable,
analysis.within_environment(
scope,
sync.named_types(cache) |> dict.from_list(),
),
eff,
)
#(proj, editable, Some(analysis))
}
fn effect_types(effects: List(#(String, EffectSpec))) {
listx.value_map(effects, fn(details) { #(details.0, details.1) })
}
pub fn run(state) {
let Snippet(run: run, ..) = state
run
}
pub fn source(state) {
let Snippet(source: #(_, source, _), ..) = state
source
}
pub fn set_references(state, cache) {
let run = run.start(source(state), state.scope, state.effects, cache)
Snippet(..state, run: run, cache: cache)
}
pub fn references(state) {
e.to_annotated(source(state), []) |> annotated.list_references()
}
pub type Message {
UserFocusedOnCode
UserClickRunEffects
UserPressedCommandKey(String)
UserClickedPath(List(Int))
UserClickedCode(List(Int))
MessageFromInput(input.Message)
MessageFromPicker(picker.Message)
RuntimeRepliedFromExternalEffect(run.Value)
ClipboardReadCompleted(Result(String, String))
ClipboardWriteCompleted(Result(Nil, String))
}
pub type Effect {
Nothing
FocusOnCode
FocusOnInput
ToggleHelp
MoveAbove
MoveBelow
WriteToClipboard(String)
ReadFromClipboard
AwaitRunningEffect(promise.Promise(Value))
Conclude(Option(Value), List(#(String, #(Value, Value))), Scope)
}
pub fn focus_on_buffer() {
window.request_animation_frame(fn(_) {
case document.query_selector("[autofocus]") {
Ok(el) -> dom_element.focus(el)
Error(Nil) -> Nil
}
})
Nil
}
pub fn focus_on_input() {
window.request_animation_frame(fn(_) {
case document.query_selector("[autofocus]") {
Ok(el) -> {
dom_element.focus(el)
// This can only be done when we move to a new focus
// error is something specifically to do with numbers
dom_element.set_selection_range(el, 0, -1)
}
Error(Nil) -> Nil
}
})
Nil
}
pub fn write_to_clipboard(text) {
promise.map(clipboard.write_text(text), ClipboardWriteCompleted)
}
pub fn read_from_clipboard() {
promise.map(clipboard.read_text(), ClipboardReadCompleted)
// TODO make busy
}
pub fn await_running_effect(promise) {
promise.map(promise, RuntimeRepliedFromExternalEffect)
}
fn navigate_source(proj, state) {
let Snippet(source: #(_, editable, analysis), ..) = state
let source = #(proj, editable, analysis)
let status = Editing(Command(None))
let state = Snippet(..state, status: status, source: source)
#(state, Nothing)
}
fn update_source(proj, state) {
let Snippet(source: #(old, _, _), history: history, ..) = state
let editable = p.rebuild(proj)
let source =
new_source(proj, editable, state.scope, state.effects, state.cache)
let History(undo: undo, ..) = history
let undo = [old, ..undo]
let history = History(undo: undo, redo: [])
let status = Editing(Command(None))
let run = run.start(editable, state.scope, state.effects, state.cache)
Snippet(..state, status: status, source: source, history: history, run: run)
}
fn update_source_from_buffer(proj, state) {
#(update_source(proj, state), Nothing)
}
fn update_source_from_pallet(proj, state) {
#(update_source(proj, state), FocusOnCode)
}
fn return_to_buffer(state) {
let state = Snippet(..state, status: Editing(Command(None)))
#(state, FocusOnCode)
}
fn change_mode(state, mode) {
let status = Editing(mode)
let state = Snippet(..state, status: status)
#(state, FocusOnInput)
}
fn keep_editing(state, mode) {
let state = Snippet(..state, status: Editing(mode))
#(state, Nothing)
}
fn show_error(state, error) {
let status = Editing(Command(Some(error)))
let state = Snippet(..state, status: status)
#(state, Nothing)
}
pub fn update(state, message) {
let Snippet(
status: status,
source: #(proj, editable, _),
run: run,
effects: effects,
..,
) = state
case message, status {
UserFocusedOnCode, Idle -> #(
Snippet(..state, status: Editing(Command(None))),
Nothing,
)
UserFocusedOnCode, Editing(_) -> #(
Snippet(..state, status: Editing(Command(None))),
Nothing,
)
UserPressedCommandKey(key), Editing(Command(_)) -> {
let state = Snippet(..state, using_mouse: False)
case key {
"ArrowRight" -> move_right(state)
"ArrowLeft" -> move_left(state)
"ArrowUp" -> move_up(state)
"ArrowDown" -> move_down(state)
" " -> search_vacant(state)
// Needed for my examples while Gleam doesn't have file embedding
"Q" -> copy_escaped(state)
"w" -> call_with(state)
"E" -> assign_above(state)
"e" -> assign_to(state)
"r" -> insert_record(state)
"t" -> insert_tag(state)
"y" -> copy(state)
"Y" -> paste(state)
// "u" ->
"i" -> insert_mode(state)
"o" -> overwrite_record(state)
"p" -> insert_perform(state)
"a" -> increase(state)
"s" -> insert_string(state)
"d" | "Delete" -> delete(state)
"f" -> insert_function(state)
"g" -> select_field(state)
"h" -> insert_handle(state)
"j" -> insert_builtin(state)
"k" -> toggle_open(state)
"l" -> insert_list(state)
"#" -> insert_reference(state)
"z" -> undo(state)
"Z" -> redo(state)
// "x" ->
"c" -> call_function(state)
"v" -> insert_variable(state)
"b" -> insert_binary(state)
"n" -> insert_integer(state)
"m" -> insert_case(state)
"M" -> insert_open_case(state)
"," -> extend_before(state)
"EXTEND AFTER" -> extend_after(state)
"." -> spread_list(state)
"TOGGLE SPREAD" -> toggle_spread(state)
"TOGGLE OTHERWISE" -> toggle_otherwise(state)
"?" -> #(state, ToggleHelp)
"Enter" -> execute(state)
_ -> show_error(state, NoKeyBinding(key))
}
}
UserPressedCommandKey(_), _ -> panic as "should never get a buffer message"
UserClickedPath(path), _ ->
navigate_source(p.focus_at(editable, path), state)
// This is unhelpful as hard if big blocks are selected
// case listx.starts_with(path, p.path(proj)) && p.path(proj) != [] {
UserClickedCode(path), _ ->
case proj, p.path(proj) == path {
#(p.Assign(p.AssignStatement(_), _, _, _, _), _), True ->
toggle_open(state)
_, _ ->
case
// listx.starts_with(path, p.path(proj))
// path expanding real just means it was the last thing clicked
Some(path) == state.expanding && p.path(proj) != []
{
True -> increase(state)
False -> {
let state = Snippet(..state, expanding: Some(path))
navigate_source(
p.focus_at(editable, path),
Snippet(..state, using_mouse: True),
)
}
}
}
MessageFromInput(message), Editing(EditText(value, rebuild)) ->
case input.update_text(value, message) {
input.Continue(value) -> keep_editing(state, EditText(value, rebuild))
input.Confirmed(value) ->
update_source_from_pallet(rebuild(value), state)
input.Cancelled -> return_to_buffer(state)
}
MessageFromInput(message), Editing(EditInteger(value, rebuild)) ->
case input.update_number(value, message) {
input.Continue(value) ->
keep_editing(state, EditInteger(value, rebuild))
input.Confirmed(value) ->
update_source_from_pallet(rebuild(value), state)
input.Cancelled -> return_to_buffer(state)
}
MessageFromInput(_), _ -> panic as "shouldn't reach input message"
MessageFromPicker(picker.Updated(picker)), Editing(Pick(_, rebuild)) ->
keep_editing(state, Pick(picker, rebuild))
MessageFromPicker(picker.Decided(value)), Editing(Pick(_, rebuild)) ->
update_source_from_pallet(rebuild(value), state)
MessageFromPicker(picker.Dismissed), Editing(Pick(_, _rebuild)) ->
return_to_buffer(state)
MessageFromPicker(_), _ -> panic as "shouldn't reach picker message"
UserClickRunEffects, _ -> run_effects(state)
RuntimeRepliedFromExternalEffect(reply), Editing(Command(_))
| RuntimeRepliedFromExternalEffect(reply), Idle
-> {
let assert run.Run(run.Handling(label, lift, env, k, _), effect_log) = run
let effect_log = [#(label, #(lift, reply)), ..effect_log]
let status = case block.resume(reply, env, k) {
Ok(#(value, env)) -> run.Done(value, env)
Error(debug) -> run.handle_extrinsic_effects(debug, effects)
}
let run = run.Run(status, effect_log)
let state = Snippet(..state, run: run)
case status {
run.Done(_, _) | run.Failed(_) -> #(state, Nothing)
run.Handling(_label, lift, env, k, blocking) ->
case blocking(lift) {
Ok(promise) -> {
let run = run.Run(status, effect_log)
let state = Snippet(..state, run: run)
#(state, AwaitRunningEffect(promise))
}
Error(reason) -> {
let run = run.Run(run.Failed(#(reason, Nil, env, k)), effect_log)
let state = Snippet(..state, run: run)
#(state, Nothing)
}
}
}
}
RuntimeRepliedFromExternalEffect(_), Editing(mode) -> {
io.debug(mode)
panic as "Should never be editing while running effects"
}
ClipboardReadCompleted(return), _ -> {
let assert Editing(Command(_)) = status
case return {
Ok(text) ->
case decode.from_json(text) {
Ok(expression) -> {
let assert #(p.Exp(_), zoom) = proj
let proj = #(p.Exp(e.from_expression(expression)), zoom)
update_source_from_buffer(proj, state)
}
Error(_) -> show_error(state, ActionFailed("paste"))
}
Error(_) -> show_error(state, ActionFailed("paste"))
}
}
ClipboardWriteCompleted(return), _ ->
case return {
Ok(Nil) -> #(state, Nothing)
Error(_) -> show_error(state, ActionFailed("paste"))
}
}
}
fn move_right(state) {
let Snippet(source: #(proj, _, _), ..) = state
navigate_source(navigation.next(proj), state)
}
fn move_left(state) {
let Snippet(source: #(proj, _, _), ..) = state
navigate_source(navigation.previous(proj), state)
}
fn move_up(state) {
let Snippet(source: #(proj, _, _), ..) = state
case navigation.move_up(proj) {
Ok(new) -> navigate_source(navigation.next(new), state)
Error(Nil) -> #(state, MoveAbove)
}
}
fn move_down(state) {
let Snippet(source: #(proj, _, _), ..) = state
case navigation.move_down(proj) {
Ok(new) -> navigate_source(navigation.next(new), state)
Error(Nil) -> #(state, MoveBelow)
}
}
fn copy(state) {
let Snippet(source: #(proj, _, _), ..) = state
case proj {
#(p.Exp(expression), _) -> {
let text = encode.to_json(e.to_expression(expression))
#(state, WriteToClipboard(text))
}
_ -> show_error(state, ActionFailed("copy"))
}
}
fn paste(state) {
#(state, ReadFromClipboard)
}
fn search_vacant(state) {
let Snippet(source: #(proj, _, _), ..) = state
let new = do_search_vacant(proj)
navigate_source(new, state)
}
fn do_search_vacant(proj) {
let next = navigation.next(proj)
case next {
#(p.Exp(e.Vacant("")), _zoom) -> next
// If at the top break, can search again to loop around
#(p.Exp(_), []) -> next
_ -> do_search_vacant(next)
}
}
fn toggle_open(state) {
let Snippet(source: #(proj, _, _), ..) = state
let #(focus, zoom) = proj
let focus = case focus {
p.Exp(e.Block(assigns, then, open)) -> p.Exp(e.Block(assigns, then, !open))
p.Assign(label, e.Block(assigns, inner, open), pre, post, final) ->
p.Assign(label, e.Block(assigns, inner, !open), pre, post, final)
_ -> focus
}
let proj = #(focus, zoom)
navigate_source(proj, state)
}
fn call_with(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.call_with(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("call as argument"))
}
}
fn assign_to(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.assign(proj) {
Ok(rebuild) -> {
let rebuild = fn(new) { rebuild(e.Bind(new)) }
change_mode(state, Pick(picker.new("", []), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("assign to"))
}
}
fn assign_above(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.assign_before(proj) {
Ok(rebuild) -> {
let rebuild = fn(new) { rebuild(e.Bind(new)) }
change_mode(state, Pick(picker.new("", []), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("assign above"))
}
}
fn insert_record(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.make_record(proj, analysis) {
Ok(action.Updated(proj)) -> update_source_from_buffer(proj, state)
Ok(action.Choose(value, hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new(value, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("create record"))
}
}
fn overwrite_record(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.overwrite_record(proj, analysis) {
Ok(#(hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new("", hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("create record"))
}
}
fn insert_tag(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.make_tagged(proj, analysis) {
Ok(action.Updated(new)) -> update_source_from_buffer(new, state)
Ok(action.Choose(value, hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new(value, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("tag expression"))
}
}
fn extend_before(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.extend_before(proj, analysis) {
Ok(action.Updated(new)) -> update_source_from_buffer(new, state)
Ok(action.Choose(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, render_poly)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("extend"))
}
}
fn extend_after(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.extend_after(proj, analysis) {
Ok(action.Updated(new)) -> update_source_from_buffer(new, state)
Ok(action.Choose(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, render_poly)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("extend"))
}
}
fn insert_mode(state) {
let Snippet(source: #(proj, _, _), ..) = state
case proj {
#(p.Exp(e.String(value)), zoom) ->
change_mode(
state,
EditText(value, fn(value) { #(p.Exp(e.String(value)), zoom) }),
)
_ ->
case p.text(proj) {
Ok(#(value, rebuild)) ->
change_mode(state, Pick(picker.new(value, []), rebuild))
Error(Nil) -> show_error(state, ActionFailed("edit"))
}
}
}
fn insert_perform(state) {
let Snippet(source: #(proj, _, _), effects: effects, ..) = state
let hints = effect_types(effects)
case action.perform(proj) {
Ok(#(filter, rebuild)) -> {
let hints = listx.value_map(hints, render_effect)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("perform"))
}
}
fn increase(state) {
let Snippet(source: #(proj, _, _), ..) = state
case navigation.increase(proj) {
Ok(new) -> navigate_source(new, state)
Error(Nil) -> show_error(state, ActionFailed("increase selection"))
}
}
fn insert_string(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.string(proj) {
Ok(#(value, rebuild)) -> change_mode(state, EditText(value, rebuild))
Error(Nil) -> show_error(state, ActionFailed("create text"))
}
}
fn delete(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.delete(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("delete"))
}
}
fn insert_function(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.function(proj) {
Ok(rebuild) -> change_mode(state, Pick(picker.new("", []), rebuild))
Error(Nil) -> show_error(state, ActionFailed("create function"))
}
}
fn select_field(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.select_field(proj, analysis) {
Ok(#(hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new("", hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("select field"))
}
}
fn insert_handle(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.handle(proj, analysis) {
Ok(#(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, render_effect)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("perform"))
}
}
fn insert_builtin(state) {
let Snippet(source: #(proj, _, _), ..) = state
case action.insert_builtin(proj, contextual.builtins()) {
Ok(#(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, render_poly)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("insert builtin"))
}
}
fn insert_list(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.list(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("create list"))
}
}
fn insert_reference(state) {
let Snippet(source: #(proj, _, _), cache: cache, ..) = state
let index =
sync.package_index(cache)
|> listx.value_map(render_poly)
case action.insert_reference(proj) {
Ok(#(filter, rebuild)) -> {
change_mode(state, Pick(picker.new(filter, index), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("insert reference"))
}
}
fn call_function(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.call_function(proj, analysis) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("call function"))
}
}
fn insert_variable(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.insert_variable(proj, analysis) {
Ok(#(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, render_poly)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("create binary"))
}
}
fn insert_binary(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.binary(proj) {
Ok(#(value, rebuild)) -> update_source_from_buffer(rebuild(value), state)
Error(Nil) -> show_error(state, ActionFailed("create binary"))
}
}
fn insert_integer(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.integer(proj) {
Ok(#(value, rebuild)) -> change_mode(state, EditInteger(value, rebuild))
Error(Nil) -> show_error(state, ActionFailed("create number"))
}
}
fn insert_case(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.make_case(proj, analysis) {
Ok(action.Updated(new)) -> update_source_from_buffer(new, state)
Ok(action.Choose(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("create match"))
}
}
fn insert_open_case(state) {
let Snippet(source: #(proj, _, analysis), ..) = state
case action.make_open_case(proj, analysis) {
Ok(#(filter, hints, rebuild)) -> {
let hints = listx.value_map(hints, debug.mono)
change_mode(state, Pick(picker.new(filter, hints), rebuild))
}
Error(Nil) -> show_error(state, ActionFailed("create match"))
}
}
fn spread_list(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.spread_list(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("spread list"))
}
}
fn toggle_spread(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.toggle_spread(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("toggle spread"))
}
}
fn toggle_otherwise(state) {
let Snippet(source: #(proj, _, _), ..) = state
case transformation.toggle_otherwise(proj) {
Ok(new) -> update_source_from_buffer(new, state)
Error(Nil) -> show_error(state, ActionFailed("create match"))
}
}
fn undo(state) {
let Snippet(source: #(proj, _, _), history: history, ..) = state
case history.undo {
[] -> show_error(state, ActionFailed("undo"))
[saved, ..rest] -> {
let source =
new_source(
saved,
p.rebuild(saved),
state.scope,
state.effects,
state.cache,
)
let history = History(undo: rest, redo: [proj, ..history.redo])
let status = Editing(Command(None))
let state =
Snippet(..state, status: status, source: source, history: history)
#(state, Nothing)
}
}
}
fn redo(state) {
let Snippet(source: #(proj, _, _), history: history, ..) = state
case history.redo {
[] -> show_error(state, ActionFailed("redo"))
[saved, ..rest] -> {
let source =
new_source(
saved,
p.rebuild(saved),
state.scope,
state.effects,
state.cache,
)
let history = History(undo: [proj, ..history.undo], redo: rest)
let status = Editing(Command(None))
let state =
Snippet(..state, status: status, source: source, history: history)
#(state, Nothing)
}
}
}
pub fn copy_escaped(state) {
let Snippet(source: #(proj, _, _), ..) = state
case proj {
#(p.Exp(expression), _) -> {
let text =
encode.to_json(e.to_expression(expression))
|> string.replace("\\", "\\\\")
|> string.replace("\"", "\\\"")
#(state, WriteToClipboard(text))
}
_ -> show_error(state, ActionFailed("copy"))
}
}
fn execute(state) {
let Snippet(run: run, ..) = state
case run.status {
run.Done(value, env) -> #(state, Conclude(value, run.effects, env))
run.Failed(_) -> show_error(state, ActionFailed("Execute"))
_ -> run_effects(state)
}
}
fn run_effects(state) {
let Snippet(run: run, ..) = state
let run.Run(status, effect_log) = run
case status {
run.Handling(_label, lift, env, k, blocking) -> {
case blocking(lift) {
Ok(promise) -> {
let run = run.Run(status, effect_log)
let state = Snippet(..state, run: run)
#(state, AwaitRunningEffect(promise))
}
Error(reason) -> {
let run = run.Run(run.Failed(#(reason, Nil, env, k)), effect_log)
let state = Snippet(..state, run: run)
#(state, Nothing)
}
}
}
_ -> #(state, Nothing)
}
}
pub fn finish_editing(state) {
Snippet(..state, status: Idle)
}
pub fn render(state: Snippet) {
h.div(
[
a.class(
"bg-white neo-shadow font-mono mt-2 mb-6 border border-black flex flex-col",
),
// a.style([#("min-height", "18ch")]),
],
bare_render(state),
)
}
pub fn render_sticky(state: Snippet) {
h.div(
[
a.class(
"bg-white neo-shadow font-mono mt-2 sticky bottom-6 mb-6 border border-black flex flex-col",
),
],
bare_render(state),
)
}
pub fn render_editor(state: Snippet) {
h.div(
[
a.class(
"bg-white neo-shadow font-mono mt-2 mb-6 border border-black flex flex-col",
),
a.style([
#("min-height", "15em"),
#("height", "100%"),
#("max-height", "95%"),
]),
],
bare_render(state),
)
}
pub fn bare_render(state) {
let Snippet(
status: status,
source: source,
run: run,
using_mouse: using_mouse,
..,
) = state
let #(proj, _, analysis) = source
let errors = case analysis {
Some(analysis) -> analysis.type_errors(analysis)
None -> []
}
case status {
Editing(mode) ->
case mode {
Command(e) -> {
[
actual_render_projection(proj, True, using_mouse),
case e {