Repository navigation
Expand file tree
/
Copy pathusermon-class.ts
More file actions
987 lines (831 loc) · 33.4 KB
/
Copy pathusermon-class.ts
File metadata and controls
987 lines (831 loc) · 33.4 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
/* TODOs:
- maybe start calculating diffs from the most recent point where there's
a complete piece of code rather than from the very beginning of the
trace, so as to be more robust?!?
- why are there sometimes learners with NO EVENTS being rendered? did
they just connect and not issue any additional events?
- the language selector doesn't get updated right away; only when
something gets executed, etc.
- when visualizing, return lines don't show up properly, and prevline
(green) covers up curline (red)
*/
declare var diff_match_patch : any; // dummy to shut up compiler
function insertNoDup(arr, e) {
if (arr.indexOf(e) < 0) {
arr.push(e);
}
}
// same as htmlspecialchars except don't worry about expanding spaces or
// tabs since we want proper word wrapping in divs.
function htmlsanitize(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str;
}
var AceRange = ace.require("ace/range").Range;
//var AceSelection = ace.require("ace/selection").Selection;
var AceDocument = ace.require("ace/document").Document;
var dmp = new diff_match_patch(); // TODO: is it OK for this to be a global?
function isEditCodeEvent(evt) {
return (evt.eventType === 'opt-client-event' &&
evt.data.type === 'editCode');
}
// User monitor
class UserMon {
events: any[] = []; // a list of filtered user events in codeopticon format
// ONLY opt-client-event events will be added!!!
idx = 0; // current index of display, refers to events array
curAceMode: string = 'python'; // default
initialCod: string = undefined;
editor;
session;
renderedDocument;
trueInternalDocument;
gutterDecorations = [];
stepperGutterDecorations = []; // for rendering updateOutput calls
animationTimerId: number;
loopTimerId: number;
loopStartIdx: number;
loopEndIdx: number;
loopGutterDecorations = [];
domRoot: JQuery;
sliderDiv: JQuery;
uniqueID: number;
constructor(domRoot, uniqueID, myWidth='500px', myHeight='300px') {
this.uniqueID = uniqueID;
var editorDivName = 'editor' + String(uniqueID);
var userMonHTML = '\
<div class="usermon">\
<div class="header">\
<span class="mode"></span>\
<span class="inputLst"></span>\
<span class="lang"></span>\
</div>\
<div id="' + editorDivName + '" class="aceEditor"></div>\
<div class="slider"></div>\
<div class="footer" style="margin-top: 5px;"></div>\
</div>';
this.domRoot = domRoot;
// try to clear first to (maybe) prevent memory leak:
this.domRoot.empty().html(userMonHTML);
// set these BEFORE calling ace.edit()
this.domRoot.find('.aceEditor')
.css('width', myWidth)
.css('height', myHeight);
// Ace's containment relationship: Editor -> EditSession -> Document
this.editor = ace.edit(editorDivName);
this.session = this.editor.getSession();
this.renderedDocument = this.session.getDocument();
// contains the true contents, without any fanciness for rendering
this.trueInternalDocument = new AceDocument('');
this.editor.setReadOnly(true);
this.editor.setHighlightActiveLine(false); // to avoid gray highlight over active line
this.editor.setHighlightGutterLine(false); // to avoid gray highlight over gutter of active line
this.editor.setDisplayIndentGuides(false); // to avoid annoying gray vertical lines
this.editor.setFontSize(11);
this.editor.$blockScrolling = Infinity; // kludgy to shut up weird warnings
this.session.setMode("ace/mode/" + this.curAceMode);
// initialize the slider
this.sliderDiv = this.domRoot.find('.slider');
// set widths ...
this.domRoot.find('.header').css('width', myWidth);
this.sliderDiv.width(myWidth);
this.sliderDiv.slider({min: 0, max: 1, step: 1});
//disable keyboard actions on the slider itself (to prevent double-firing of events)
this.sliderDiv.find(".ui-slider-handle")
.unbind('keydown')
.css('width', '0.7em')
.css('height', '1.4em');
$(".ui-widget-content").css('font-size', '0.8em');
this.sliderDiv.on('slide', (evt, ui) => {
// this is SUPER subtle. if this value was changed programmatically,
// then evt.originalEvent will be undefined. however, if this value
// was changed by a user-initiated event, then this code should be
// executed ...
if (evt.originalEvent) {
this.jumpToStep(ui.value);
}
});
}
getEditCodeIndices() {
// indices in events where editCode events occurred
var editCodeIndices: number[] = [];
this.events.forEach((e, i) => {
if (isEditCodeEvent(e)) {
editCodeIndices.push(i);
}
});
return editCodeIndices;
}
_addEventInternal(obj) {
if (obj.eventType === 'opt-client-event') {
if (obj.data.type === 'updateAppDisplay' || obj.data.type === 'doneExecutingCode') {
// initialize this ASAP!
if (this.initialCod === undefined) {
this.initialCod = obj.data.appState.code;
//this.editor.setValue(this.initialCod); // redundant with initDocuments
this.initDocuments(this.initialCod);
this.editor.clearSelection(); // or else we get a weird selection
}
// don't push updateAppDisplay to 'display' mode since that
// signal is often redundant and uninformative (usually subsumed
// by doneExecutingCode, and subsequent updateOutput events)
if (!(obj.data.type === 'updateAppDisplay' && obj.data.mode === 'display')) {
this.events.push(obj);
}
} else if (obj.data.type === 'editCode') {
// only start picking up edit code events AFTER initialCod has
// been hooked; otherwise we can't reconstruct the code from
// incomplete deltas :(
if (this.initialCod !== undefined) {
this.events.push(obj);
}
} else if (obj.data.type === 'updateOutput') {
this.events.push(obj);
} else if (obj.data.type === 'opt-client-chat') {
// IGNORE these events!
} else {
console.assert(false);
}
} else {
// nix this ... ONLY ADD opt-client-events to the list
/*
console.assert(obj.eventType === 'opt-client-connect' ||
obj.eventType === 'opt-client-disconnect');
this.events.push(obj);
*/
}
}
_eventsLstSanityChecks() {
// sanity checks
if (this.events.length > 0) {
// first event should NOT be editCode since we can't reconstruct the
// original code from the incomplete delta without initialCod
// (nix this since things might be reordered for chronological ordering)
//console.assert(!isEditCodeEvent(this.events[0]));
// everything should be in chronological order
for (var i = 0; i < this.events.length - 1; i++) {
var cur = this.events[i];
var next = this.events[i+1];
console.assert(cur.data.clientTime < next.data.clientTime);
}
}
// indices in events where editCode events occurred
var editCodeIndices = this.getEditCodeIndices();
var prevIdx = undefined;
editCodeIndices.forEach((idx) => {
console.assert(isEditCodeEvent(this.events[idx]));
if (prevIdx !== undefined) {
console.assert(prevIdx < idx);
}
prevIdx = idx;
});
}
// TODO: this is kinda inefficient since it loops over everything in
// this.events, but it's easier to get right than an incremental approach:
_populateDiffObjs() {
// add a diffObj field to every editCode event by applying each
// delta over all prior ones (tricky tricky)
var deltas = [];
var cur = this.initialCod;
//console.log('cur', cur);
$.each(this.events, (i, obj) => {
if (isEditCodeEvent(obj)) {
// assert chronological order
if (deltas.length) {
console.assert(_.last(deltas).t <= obj.data.delta.t);
}
var curDelta = obj.data.delta.d;
//console.log('curDelta', curDelta);
// make this failure oblivious ... only initialize diffObj if
// the diff properly goes through ... otherwise skip it and just
// try again using the next diff. might be a dumb idea, but
// let's try it for now :0
try {
var diff = dmp.diff_fromDelta(cur, curDelta); // might CROAK if cur and curDelta don't match up!
obj.data.diffObj = diff; // add new field! (or replace existing one, i guess)
var patch = dmp.patch_make(diff);
var newCur = dmp.patch_apply(patch, cur);
console.assert(newCur.length === 2);
cur = newCur[0];
deltas.push(obj.data.delta);
} catch (e) {
//console.warn(e); // RE-ENABLE sometime -- could be important
}
}
});
}
// MODIFY UserMon by pushing a new event to it
//
// TODO: look into forcing chronological order of events based on
// clientTime (if available)
addEvent(obj /* event object in codeopticon format */) {
this._addEventInternal(obj);
// SORT this.events by clientTime to keep everything in chronological order!
this.events.sort(function(a, b) {return a.data.clientTime - b.data.clientTime;});
this._eventsLstSanityChecks();
// TODO: this is kinda inefficient since it loops over everything in
// this.events, but it's easier to get right than an incremental approach:
if (this.initialCod !== undefined // tricky!!! initialCod can be '' but defined
&& isEditCodeEvent(obj)) {
this._populateDiffObjs();
}
// expand the slider's width
this.sliderDiv
.slider() // add in an extra call to prevent weird "jquery-1.8.2.min.js:2 Uncaught Error: cannot call methods on slider prior to initialization" errors, but is that right?!?
.slider('option', 'max', this.events.length - 1)
.slider('value', this.idx); // move it back to its original value
// only jump to end if you're currently at the penultimate entry --
// otherwise just stay at the original value
if (this.idx === this.events.length - 2) {
this.jumpToEnd();
}
}
addEventsLst(events: any[]) {
events.forEach((e, i) => {
this._addEventInternal(e);
});
// SORT this.events by clientTime to keep everything in chronological order!
this.events.sort(function(a, b) {return a.data.clientTime - b.data.clientTime;});
this._eventsLstSanityChecks();
/*
this.events.forEach((e, i) => {
console.log(e.data);
});
console.log('------');
*/
this._populateDiffObjs();
this.sliderDiv.slider('option', 'max', this.events.length - 1);
this.jumpToEnd();
}
jumpToEnd() {
// jump to the most recent event
if (this.events.length > 0) {
this.jumpToStep(this.events.length - 1);
console.assert(this.events.length - 1 === this.idx);
this.sliderDiv.slider('value', this.idx); // programmatically update the slider
}
}
initDocuments(startingText) {
this.renderedDocument.setValue(startingText); // initialize
this.trueInternalDocument.setValue(startingText); // initialize
}
clearAllFrills() {
// clear exceptions
this.setException('');
this.session.clearAnnotations()
// clear all animations
if (this.animationTimerId) {
clearInterval(this.animationTimerId);
this.animationTimerId = undefined;
}
// clear all gutters
while (this.gutterDecorations.length > 0) {
var gd = this.gutterDecorations.pop();
this.session.removeGutterDecoration(gd[0], gd[1]);
}
// clear all stepper gutters
while (this.stepperGutterDecorations.length > 0) {
var gd = this.stepperGutterDecorations.pop();
this.session.removeGutterDecoration(gd[0], gd[1]);
}
// clear all previously-set markers
var backMarkers = this.session.getMarkers(false); // back
var frontMarkers = this.session.getMarkers(true); // front
$.each(backMarkers, (k, v) => {
if (v.type === 'insertedText' || v.type === 'deletedText') {
console.assert(Number(k) === Number(v.id));
this.session.removeMarker(k);
}
});
// TODO: copy-pasta
$.each(frontMarkers, (k, v) => {
if (v.type === 'insertedText' || v.type === 'deletedText') {
console.assert(Number(k) === Number(v.id));
this.session.removeMarker(k);
}
});
}
applyDiff(diff,
color, /* should we try to color and animate relevant changes? */
isReverse, /* should we reverse the effects of diff? */
autoScroll /* should the viewport auto-scroll? */) {
var isAllWhitespace;
this.clearAllFrills();
// color the gutters to make it easier to see at a glance which lines have changed
// (VERY important to do this at the VERY BEGINNING before the editor gets modified)
if (color) {
var ar = this.getAffectedRows(diff);
var bothInsertionAndDeletionRows = _.intersection(ar.rowsWithInsertions,
ar.rowsWithDeletions);
// remove redundancies
var allInsertionRows = _.difference(ar.rowsWithInsertions, bothInsertionAndDeletionRows);
var allDeletionRows = _.difference(ar.rowsWithDeletions, bothInsertionAndDeletionRows);
allInsertionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'greenGutter');
this.gutterDecorations.push([r, 'greenGutter']);
});
allDeletionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'redGutter');
this.gutterDecorations.push([r, 'redGutter']);
});
bothInsertionAndDeletionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'redGreenStripeGutter');
this.gutterDecorations.push([r, 'redGreenStripeGutter']);
});
}
// each diff consists of N chunks, starting edits at offset 0
var offset = 0;
// list of deletion events in this particular diff:
var deletions = [];
// see http://stackoverflow.com/questions/25083183/how-can-i-get-and-patch-diffs-in-ace-editor
diff.forEach((chunk) => {
var op = chunk[0];
var text = chunk[1];
if (op === 0) { // equality ... just advance forward
offset += text.length;
} else if (op === (isReverse ? 1 : -1)) { // -1 means delete
// sync up the document's contents from trueInternalDocument before
// making any further changes to it.
// (TODO: is this the right place to put this sync function?)
this.renderedDocument.setValue(this.trueInternalDocument.getValue());
//console.log(String(this.idx), 'sync renderedDocument with trueInternalDocument');
var startPos = this.renderedDocument.indexToPosition(offset);
var endPos = this.renderedDocument.indexToPosition(offset + text.length);
var deleteRange = AceRange.fromPoints(startPos, endPos);
var txtToDelete = this.session.getTextRange(deleteRange);
isAllWhitespace = (txtToDelete.trim() === '');
this.renderedDocument.remove(deleteRange);
this.trueInternalDocument.remove(deleteRange);
deletions.push({deletedText: txtToDelete, deleteRange: deleteRange});
// move the editor's cursor to offset to look aesthetically sharp
this.editor.clearSelection(); // or else we get a weird selection
this.editor.moveCursorToPosition(startPos);
if (autoScroll) {
this.editor.scrollToLine(startPos.row, true /* vertically center it */);
}
} else if (op === (isReverse ? -1 : 1)) { // 1 means insert
// sync up the document's contents from trueInternalDocument before
// making any further changes to it.
// (TODO: is this the right place to put this sync function?)
this.renderedDocument.setValue(this.trueInternalDocument.getValue());
//console.log(String(this.idx), 'sync renderedDocument with trueInternalDocument');
var startPos = this.renderedDocument.indexToPosition(offset);
// make sure both are synced
this.renderedDocument.insert(startPos, text);
this.trueInternalDocument.insert(startPos, text);
isAllWhitespace = (text.trim() === '');
offset += text.length;
var endPos = this.renderedDocument.indexToPosition(offset); // use the new offset value
var insertRange = new AceRange(startPos.row, startPos.column,
endPos.row, endPos.column);
if (color) {
// all whitespace AND spanning multiple lines ...
if (isAllWhitespace && insertRange.isMultiLine()) {
// render a gutter marker on all rows and also, for the
// final row, highlight up until the cursor
// nix this since it should already be covered by getAffectedRows
/*
for (var r = insertRange.start.row; r <= insertRange.end.row; r++) {
this.session.addGutterDecoration(r, 'greenGutter');
this.gutterDecorations.push([r, 'greenGutter']);
}
*/
// for the final row, highlight up until the cursor ...
this.session.addMarker(new AceRange(insertRange.end.row, 0,
insertRange.end.row, insertRange.end.column),
'greenMarker', 'insertedText', false /* inFront */);
} else {
this.session.addMarker(insertRange,
'greenMarker',
'insertedText' /* for selecting later! */,
false /* inFront -- definitely false! */);
}
}
// move the editor's cursor to offset to look aesthetically sharp
this.editor.clearSelection(); // or else we get a weird selection
this.editor.moveCursorToPosition(endPos);
if (autoScroll) {
this.editor.scrollToLine(endPos.row, true /* vertically center it */);
}
} else {
console.assert(false);
}
});
// queue up all deletions objects to color them:
if (color && deletions.length > 0) {
var txtHidden = true;
this.animationTimerId = setInterval(() => {
$.each(deletions, (i, e) => {
if (txtHidden) {
this.renderedDocument.insert(e.deleteRange.start, e.deletedText);
// all whitespace AND spanning multiple lines ...
if (isAllWhitespace && e.deleteRange.isMultiLine()) {
// render a gutter marker on all rows and also, for the
// final row, highlight up until the cursor
// nix this since it should already be covered by getAffectedRows
/*
for (var r = e.deleteRange.start.row; r <= e.deleteRange.end.row; r++) {
this.session.addGutterDecoration(r, 'redGutter');
this.gutterDecorations.push([r, 'redGutter']);
}
*/
// for the final row, highlight up until the cursor ...
this.session.addMarker(new AceRange(e.deleteRange.end.row, 0,
e.deleteRange.end.row, e.deleteRange.end.column),
'redMarker', 'deletedText', true);
} else {
this.session.addMarker(e.deleteRange,
'redMarker',
'deletedText',
true);
}
} else {
// remove all deletedText markers:
var frontMarkers = this.session.getMarkers(true);
$.each(frontMarkers, (k, v) => {
if (v.type === 'deletedText') {
console.assert(Number(k) === Number(v.id));
this.session.removeMarker(k);
}
});
this.renderedDocument.remove(e.deleteRange);
}
});
txtHidden = !txtHidden;
}, 750);
}
}
reverseDiff(diff, color, autoScroll) {
this.applyDiff(diff, color, true, autoScroll);
}
// TODO: is there a way to do a direct jump -- i.e., squishing a bunch
// of diffs together -- rather than this hacky
// multiple-single-step-jumps business? if we can compress a bunch of
// diffs together into one, then that will make for a more meaningful
// overall diff rather than a bunch of single diffs.
jumpToStep(s, autoScroll=true, color=true /* render in color? */) {
console.assert(0 <= s && s < this.events.length);
// OK this is the super-simple algorithm -- just go from 0 until s
// and apply all diffs from scratch. it's inefficient but reliable.
// SUPER DUPER optimization: if there's no editCode event between
// this.idx and s, then don't apply any deltas!!!
var startIdx, endIdx;
if (this.idx > s) {
startIdx = s;
endIdx = this.idx;
} else {
startIdx = this.idx;
endIdx = s;
}
console.assert(startIdx <= endIdx);
var editCodeIndices = this.getEditCodeIndices();
var i;
var needToApplyDiffs = true;
for (i = 0; i < editCodeIndices.length - 1; i++) {
var cur = editCodeIndices[i];
var next = editCodeIndices[i+1];
if (cur < startIdx && endIdx < next) { // fully open interval
needToApplyDiffs = false;
}
}
// edge case (literally!)
var lastIdx = _.last(editCodeIndices);
if (lastIdx < startIdx) {
console.assert(lastIdx < endIdx);
needToApplyDiffs = false;
}
// shit this is annoyingly tricky!!!
var firstIdx = _.first(editCodeIndices);
if (s < firstIdx) {
// then let's CLEAR EVERYTHING since there are no diffs to apply!!!
this.clearAllFrills();
this.initDocuments(this.initialCod);
this.editor.clearSelection(); // or else we get a weird selection
needToApplyDiffs = false;
}
if (needToApplyDiffs) {
// reset first ...
this.initDocuments(this.initialCod);
this.editor.clearSelection(); // or else we get a weird selection
var lastDiffIdx = undefined;
editCodeIndices.forEach((idx) => {
if (idx <= s) {
lastDiffIdx = idx;
}
});
if (lastDiffIdx !== undefined) {
console.assert(isEditCodeEvent(this.events[lastDiffIdx]));
}
//console.log(s, lastDiffIdx, editCodeIndices);
// apply all steps from 0 to s, inclusive
for (i = 0; i <= s; i++) {
var curEvent = this.events[i];
if (curEvent.eventType === 'opt-client-event') {
if (isEditCodeEvent(curEvent)) {
var curDiff = curEvent.data.diffObj;
if (curDiff !== undefined) { // soften this constraint
this.applyDiff(curDiff,
color ? (i === lastDiffIdx) : false,
false,
autoScroll);
} else {
//console.warn('this.events[' + String(i) + '] has no diff (uniqueID=' + String(this.uniqueID) + ')'); // RE-ENABLE sometime -- could be important
}
}
// TODO: handle other cases
}
}
} else {
// !needToApplyDiffs
}
// now do a full-on sweep from 0 to s, inclusive, and update all
// other status messages as necessary using the "most recent" value
var lastLang = undefined;
var lastInputJSON = undefined;
var lastMode = undefined;
var lastKillerException = undefined; // an Exception object
var lastUpdateOutputObj = undefined;
for (i = 0; i <= s; i++) {
var evt = this.events[i];
if (evt.eventType === 'opt-client-event') {
var d = evt.data;
var t = d.type;
if (t === 'updateOutput') {
lastUpdateOutputObj = d;
lastMode = 'display';
} else if (t === 'doneExecutingCode') {
lastLang = d.appState.py;
lastInputJSON = d.appState.rawInputLstJSON;
lastKillerException = d.killerException; // an exception object
lastMode = 'execute';
} else if (t === 'updateAppDisplay') {
// we're not tracking 'display' mode changes since that signal
// is often redundant and uninformative (usually subsumed
// by doneExecutingCode, and subsequent updateOutput events)
console.assert(d.mode === 'edit');
lastMode = 'edit';
lastLang = d.appState.py;
lastInputJSON = d.appState.rawInputLstJSON;
lastUpdateOutputObj = undefined; // since we're editing code now, we're no longer displaying the results of updateOutput
} else if (t === 'editCode') {
lastMode = 'edit';
}
} else {
//console.log(s, evt);
}
}
this.setLang(lastLang);
this.setVizMode(lastMode);
this.setInputJSON(lastInputJSON);
// careful! don't do both since the gutter errors might clobber one another
if (lastMode === 'display') {
this.renderUpdateOutputEvent(lastUpdateOutputObj);
} else if (lastMode === 'edit') {
// ugh, this is super ugly ...
// clear exceptions
this.setException('');
this.session.clearAnnotations()
// clear all stepper gutters
while (this.stepperGutterDecorations.length > 0) {
var gd = this.stepperGutterDecorations.pop();
this.session.removeGutterDecoration(gd[0], gd[1]);
}
} else {
console.assert(lastMode === 'execute');
this.clearAllFrills(); // reset everything!
if (lastKillerException) {
this.handleKillerException(lastKillerException);
}
}
this.idx = s; // finish!
}
// return an object containing lists of rows affected by the given diff
getAffectedRows(diff) : {rowsWithInsertions: number[]; rowsWithDeletions: number[]} {
var ret = {rowsWithInsertions: [], rowsWithDeletions: []};
// each diff consists of N chunks, starting edits at offset 0
var offset = 0;
// see http://stackoverflow.com/questions/25083183/how-can-i-get-and-patch-diffs-in-ace-editor
diff.forEach((chunk) => {
var op = chunk[0];
var text = chunk[1];
var activeRange = undefined;
var isInsertion = false;
if (op === 0) { // equality ... just advance forward
offset += text.length;
} else if (op === -1) { // -1 means delete
var startPos = this.renderedDocument.indexToPosition(offset);
var endPos = this.renderedDocument.indexToPosition(offset + text.length);
activeRange = AceRange.fromPoints(startPos, endPos);
} else if (op === 1) { // 1 means insert
var startPos = this.renderedDocument.indexToPosition(offset);
offset += text.length;
var endPos = this.renderedDocument.indexToPosition(offset); // use the new offset value
activeRange = new AceRange.fromPoints(startPos, endPos);
isInsertion = true;
} else {
console.assert(false);
}
if (activeRange !== undefined) {
console.assert(activeRange.start.row <= activeRange.end.row);
for (var i = activeRange.start.row; i <= activeRange.end.row; i++) {
insertNoDup(isInsertion ? ret.rowsWithInsertions : ret.rowsWithDeletions, i);
}
}
});
return ret;
}
setLang(lang) {
var langSpan = this.domRoot.find(".lang");
var newMode;
if (!lang) {
langSpan.html('');
} else if (lang === "2") {
langSpan.html("Python 2");
newMode = 'python';
} else if (lang === "3") {
langSpan.html("Python 3");
newMode = 'python';
} else if (lang === "java") {
langSpan.html("Java");
newMode = 'java';
} else if (lang === "js") {
langSpan.html("JavaScript");
newMode = 'javascript';
} else if (lang === "ts") {
langSpan.html("TypeScript");
newMode = 'typescript';
} else if (lang === "c") {
langSpan.html("C");
newMode = 'c_cpp';
} else if (lang === "cpp") {
langSpan.html("C++");
newMode = 'c_cpp';
} else {
console.assert(false);
}
if (newMode !== undefined && newMode !== this.curAceMode) {
this.session.setMode("ace/mode/" + newMode);
this.curAceMode = newMode;
}
}
setVizMode(mode) {
var modeSpan = this.domRoot.find(".mode");
if (!mode) {
modeSpan.html('');
} else if (mode === 'edit') {
modeSpan.html('Editing');
} else if (mode === 'display') {
modeSpan.html('Stepping');
} else if (mode === 'execute') {
modeSpan.html('Compiling');
} else {
console.assert(false);
}
}
setInputJSON(s) {
var rot = this.domRoot.find(".inputLst");
if (s === undefined || s === "[]" /* empty list */) {
rot.html('');
} else {
rot.html("Input: " + htmlsanitize(s));
}
}
setException(s) {
var exc = this.domRoot.find(".footer");
if (!s) {
exc.html('');
} else {
exc.html(htmlsanitize(s));
}
}
renderUpdateOutputEvent(obj) {
// always clear all stepper gutters
while (this.stepperGutterDecorations.length > 0) {
var gd = this.stepperGutterDecorations.pop();
this.session.removeGutterDecoration(gd[0], gd[1]);
}
if (!obj) {
return;
}
// and clear all diff displays as well ONLY if we actually want to render
// something here ...
this.clearAllFrills();
if (obj.exception) {
this.setException(obj.exception);
if (obj.curline) {
this.session.setAnnotations([{row: obj.curline - 1,
type: 'error',
text: obj.exception}]);
}
} else {
this.setException('');
this.session.clearAnnotations();
}
var r, p;
// draw curline first since that apparently goes on top (?!?)
// TODO: this doesn't seem to work, though :(
if (obj.curline) {
r = obj.curline - 1;
console.assert(r >= 0);
this.session.addGutterDecoration(r, 'curLineStepGutter');
this.stepperGutterDecorations.push([r, 'curLineStepGutter']);
this.editor.scrollToLine(r, true /* vertically center it */);
}
if (obj.prevline) {
p = obj.prevline - 1;
console.assert(p >= 0);
this.session.addGutterDecoration(p, 'prevLineStepGutter');
this.stepperGutterDecorations.push([p, 'prevLineStepGutter']);
if (!r) {
this.editor.scrollToLine(p, true /* vertically center it */);
}
}
}
handleKillerException(e) {
if (!e) {
this.setException('');
this.session.clearAnnotations()
return;
}
this.setException(e.exception_msg);
var r = e.line - 1;
if (r >= 0) { // there are weird cases of an exception on line 0, weird
this.session.setAnnotations([{row: r,
type: 'error',
text: e.exception_msg}]);
this.editor.scrollToLine(r, true /* vertically center it */);
}
}
startLoop(startStep: number, endStep: number,
msPerIteration: number,
callback /* function that takes the current step as param after jump to step */) {
console.assert(startStep <= endStep);
if (startStep < 0) {
startStep = 0;
}
this.loopStartIdx = startStep;
this.loopEndIdx = endStep;
var allInsertionRows = [];
var allDeletionRows = [];
for (var i = startStep; i < endStep; i++) {
var ar = null //this.getAffectedRows(this.allDiffs[i]); // TODO: this is now busted
allInsertionRows.push.apply(allInsertionRows, ar.rowsWithInsertions);
allDeletionRows.push.apply(allDeletionRows, ar.rowsWithDeletions);
}
// dedup
allInsertionRows = _.uniq(allInsertionRows);
allDeletionRows = _.uniq(allDeletionRows);
var bothInsertionAndDeletionRows = _.intersection(allInsertionRows, allDeletionRows);
// remove redundancies
allInsertionRows = _.difference(allInsertionRows, bothInsertionAndDeletionRows);
allDeletionRows = _.difference(allDeletionRows, bothInsertionAndDeletionRows);
console.log('startLoop', startStep, endStep,
allInsertionRows, allDeletionRows, bothInsertionAndDeletionRows);
allInsertionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'greenGutter');
this.loopGutterDecorations.push([r, 'greenGutter']);
});
allDeletionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'redGutter');
this.loopGutterDecorations.push([r, 'redGutter']);
});
bothInsertionAndDeletionRows.forEach((r, i) => {
this.session.addGutterDecoration(r, 'redGreenStripeGutter');
this.loopGutterDecorations.push([r, 'redGreenStripeGutter']);
});
var curStep = startStep;
this.loopTimerId = setInterval(() => {
this.jumpToStep(curStep,
false /* don't autoscroll in loop; it's disconcerting */,
(curStep !== startStep) /* don't color the FIRST STEP
since it's disconcerting;
the first step should be the
"baseline", thus uncolored */);
callback(curStep);
if (curStep === endStep) {
curStep = startStep;
} else {
curStep++;
}
}, msPerIteration);
}
stopLoop() {
console.assert(this.loopEndIdx !== undefined);
clearInterval(this.loopTimerId);
this.jumpToStep(this.loopEndIdx, true);
// clear all gutters
while (this.loopGutterDecorations.length > 0) {
var gd = this.loopGutterDecorations.pop();
this.session.removeGutterDecoration(gd[0], gd[1]);
}
console.assert(this.loopGutterDecorations.length === 0);
// reset everything
this.loopTimerId = undefined;
this.loopStartIdx = undefined;
this.loopEndIdx = undefined;
}
}