-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileData.js
718 lines (599 loc) · 19.3 KB
/
FileData.js
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
import {helios} from "./helios.js";
import {SPACE, TAB, assert, assertDefined, stringToLines, linesToString, trimSpaces, isWordChar, Vec, FilePos} from "./util.js";
const MAX_HIST = 100;
export class FileData {
constructor(lines, selPos0, selPos1, viewPos, error, share, history, histIdx, histHead) {
assert(!selPos0.isNaN());
assert(!selPos1.isNaN());
assert(!viewPos.isNaN());
this.lines_ = lines;
this.selPos0_ = selPos0;
this.selPos1_ = selPos1; // if selPos0 == selPos1 then nothing is selected, selPos1 is the cursor position
this.viewPos_ = viewPos; // determines scroll position in editor
this.error_ = error; // compile error
this.share_ = share; // share link, or share error (different from compile error)
this.history_ = history; // list of pairs of [lines, caretPos]
this.histIdx_ = histIdx;
this.histHead_ = assertDefined(histHead);
}
static new(raw) {
let lines = stringToLines(raw);
let selPos0 = new FilePos(0, 0);
let selPos1 = new FilePos(0, 0);
let viewPos = new FilePos(0, 0);
let error = null;
let share = null;
let history = [];
let histIdx = 0;
let histHead = null;
return new FileData(lines, selPos0, selPos1, viewPos, error, share, history, histIdx, histHead);
}
get raw() {
return linesToString(this.lines_);
}
getPurposeAndName() {
let raw = this.raw.trim();
if (raw.length == 0) {
return ["", ""];
} else {
let pair = helios.extractScriptPurposeAndName(raw);
if (pair == null) {
return [null, null];
} else {
return pair;
}
}
}
get purpose() {
return this.getPurposeAndName()[0];
}
// returns empty string if file is empty
get name() {
return this.getPurposeAndName()[1];
}
get lines() {
return this.lines_.slice();
}
get nLines() {
return this.lines_.length;
}
get maxLineChars() {
let n = 0;
for (let line of this.lines_) {
n = Math.max(n, line.length)
}
return n;
}
get caretPos() {
return this.selPos1_;
}
get haveSelection() {
return !this.selPos0_.eq(this.selPos1_);
}
get selectionStart() {
if (this.selPos0_.lessThan(this.selPos1_)) {
return this.selPos0_;
} else {
return this.selPos1_;
}
}
get selectionEnd() {
let selStart = this.selectionStart;
if (selStart.eq(this.selPos0_)) {
return this.selPos1_;
} else {
return this.selPos0_;
}
}
get selection() {
let a = this.selectionStart;
let b = this.selectionEnd;
let selLines;
if (a.y == b.y) {
selLines = [this.lines_[a.y].slice(a.x, b.x)];
} else {
selLines = [this.lines_[a.y].slice(a.x)];
selLines = selLines.concat(this.lines_.slice(a.y+1, b.y));
selLines.push(this.lines_[b.y].slice(0, b.x));
}
return linesToString(selLines);
}
get viewPos() {
return this.viewPos_;
}
get error() {
return this.error_;
}
get share() {
return this.share_;
}
findFirstNonSpace(y) {
for (let x = 0; x < this.lines_[y].length; x++) {
if (this.lines_[y][x] != SPACE) {
return new FilePos(x, y);
}
}
return new FilePos(0, y);
}
// don't use findWordBoundary() directly, use findPrevWordBoundary() or findNextWordBoundary() instead
findWordBoundary(p0, d) {
const incr = (p) => {
if (p.x + d < 0 && p.y == 0) {
// don't bound, corrected by findPrevBoundary instead
return new FilePos(p.x + d, p.y);
} else {
return (new FilePos(p.x + d, p.y)).bound(this.lines_);
}
}
let p = p0;
let first = null;
if (p.y < this.lines_.length && p.x < this.lines_[p.y].length) {
first = this.lines_[p.y][p.x];
if (first == SPACE) { // space doesn't count
first = null;
}
}
while (true) {
let prev = p;
p = incr(prev);
if (p.eq(prev)) {
return p;
}
if (p.x == this.lines_[p.y].length && first != null) {
return p;
} else if (p.x == this.lines_[p.y].length) {
p = incr(p);
} else {
let c = this.lines_[p.y][p.x];
if (first == null) {
if (c != SPACE) {
first = c;
}
} else {
if (isWordChar(first) && !isWordChar(c)) {
return p;
} else if (!isWordChar(first) && isWordChar(c)) {
return p;
} else if (c == SPACE) {
return p;
}
}
}
}
}
// compensates for overshoot
findPrevWordBoundary(p0 = null) {
if (p0 == null) {
p0 = (new FilePos(this.caretPos.x - 1, this.caretPos.y)).bound(this.lines_);
}
let p = this.findWordBoundary(p0, -1);
return (new FilePos(p.x + 1, p.y));
}
findNextWordBoundary(p0 = null) {
if (p0 == null) {
p0 = this.caretPos;
}
return this.findWordBoundary(p0, 1);
}
moveCaretInternal(newPos, isSelecting = false) {
// detect no change
if (newPos.x == this.caretPos.x && newPos.y == this.caretPos.y) {
if (isSelecting) {
return this;
} else if (newPos.x == this.selPos0_.x && newPos.y == this.selPos0_.y) {
return this;
}
}
return new FileData(
this.lines_,
isSelecting ? this.selPos0_ : newPos,
newPos,
this.viewPos_,
isSelecting ? null : this.error_,
this.share_,
this.history_,
this.histIdx_,
this.histHead_,
);
}
// doesn't mutate, returns a new FileData
moveCaretTo(x, y, isSelecting = false) {
assert(arguments.length < 4);
let newPos = (new FilePos(x, y)).bound(this.lines_, false);
return this.moveCaretInternal(newPos, isSelecting);
}
// doesn't mutate, returns a new FileData
moveCaretBy(dx, dy, wrap = true, isSelecting = false) {
let newPos = this.caretPos.add(new Vec(dx, dy));
newPos = newPos.bound(this.lines_, wrap);
return this.moveCaretInternal(newPos, isSelecting);
}
selectAll() {
let newPos0 = new FilePos(0, 0);
let nLines = this.lines_.length;
let newPos1 = new FilePos(this.lines_[nLines-1].length, this.lines_.length-1);
return new FileData(
this.lines_,
newPos0,
newPos1,
this.viewPos_,
null,
this.share_,
this.history_,
this.histIdx_,
this.histHead_,
);
}
// doesn't mutate, returns a new FileData
setViewPos(p) {
return new FileData(
this.lines_,
this.selPos0_,
this.selPos1_,
p,
this.error_,
this.share_,
this.history_,
this.histIdx_,
this.histHead_,
);
}
boundViewPos(nVisibleChars, nVisibleLines) {
let x = this.viewPos_.x;
let y = this.viewPos_.y;
if (Number.isNaN(x) || x < 0) {
x = 0;
} else if (this.maxLineChars < nVisibleChars) {
x = 0;
} else if (x > this.maxLineChars - nVisibleChars + 1) {
x = this.maxLineChars - nVisibleChars + 1;
}
if (Number.isNaN(y) || y < 0) {
y = 0;
} else if (this.nLines < nVisibleLines) {
y = 0;
} else if (y > this.nLines - nVisibleLines) {
y = this.nLines - nVisibleLines;
}
return this.setViewPos(new FilePos(x, y));
}
// make sure the caret is in the view area
// doesn't mutate, returns a new FileData
scrollCaretIntoView(nVisibleChars, nVisibleLines) {
let relPos = this.caretPos.sub(this.viewPos);
let x = this.viewPos.x;
if (relPos.x < 0) {
x += relPos.x;
} else if (relPos.x >= nVisibleChars) {
x += relPos.x - nVisibleChars + 1;
} else if (this.maxLineChars < nVisibleChars + x - 1 && x > 0) {
x -= Math.min(nVisibleChars + x - 1 - this.maxLineChars, x);
}
let y = this.viewPos.y;
if (relPos.y < 0) {
y += relPos.y;
} else if (relPos.y >= nVisibleLines) {
y += relPos.y - nVisibleLines + 1;
} else if (this.nLines < nVisibleLines + y && y > 0) {
y -= Math.min(nVisibleLines + y - this.nLines, y);
}
return this.setViewPos(new FilePos(x, y));
}
scrollCaretToCenter(nVisibleChars, nVisibleLines) {
let x = this.caretPos.x - Math.floor(nVisibleChars/2);
let y = this.caretPos.y - Math.floor(nVisibleLines/2);
return this.setViewPos(new FilePos(x, y)).boundViewPos(nVisibleChars, nVisibleLines);
}
setError(error) {
// also updated the caretPos
let that = this;
if (error != null) {
let [x, y] = error.getFilePos();
that = that.moveCaretTo(x, y);
}
return new FileData(
that.lines_,
that.selPos0_,
that.selPos1_,
that.viewPos_,
error,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
setShare(share) {
return new FileData(
this.lines_,
this.selPos0_,
this.selPos1_,
this.viewPos_,
this.error_,
share,
this.history_,
this.histIdx_,
this.histHead_,
);
}
// doesn't mutate, returns a new FileData
pushHistory(lines, caretPos) {
let history = this.history_.slice();
let histIdx = this.histIdx_;
if (history.length >= MAX_HIST) {
history = history.slice(history.length - MAX_HIST + 1, histIdx);
} else {
history = history.slice(0, histIdx);
}
history.push([lines, caretPos]);
histIdx = history.length;
return new FileData(
this.lines_,
this.selPos0_,
this.selPos1_,
this.viewPos_,
this.error_,
this.share_,
history,
histIdx,
null,
);
}
// doesn't mutate, returns a new FileData
undo() {
let histIdx = this.histIdx_;
if (histIdx > 0) {
let histHead = this.histHead_;
if (histHead == null) {
histHead = [this.lines, this.caretPos];
}
histIdx -= 1;
let [newLines, newPos] = this.history_[histIdx];
return new FileData(
newLines,
newPos,
newPos,
this.viewPos_,
null,
null,
this.history_,
histIdx,
histHead,
);
} else {
return this;
}
}
// doesn't mutate, returns a new FileData
redo() {
let histIdx = this.histIdx_;
if (histIdx < this.history_.length) {
histIdx += 1;
let histHead = this.histHead_;
let newLines;
let newPos;
if (histIdx == this.history_.length) {
[newLines, newPos] = histHead;
histHead = null;
} else {
[newLines, newPos] = this.history_[histIdx];
}
return new FileData(
newLines,
newPos,
newPos,
this.viewPos_,
null,
null,
this.history_,
histIdx,
histHead,
);
} else {
return this;
}
}
// doesn't mutate, returns a new FileData
// optionally updates the history stack
deleteSelection(updateHistory = true) {
if (this.haveSelection) {
let oldLines = this.lines_;
let a = this.selectionStart;
let b = this.selectionEnd;
let newLines = oldLines.slice(0, a.y);
newLines.push(oldLines[a.y].slice(0, a.x) + oldLines[b.y].slice(b.x));
newLines = newLines.concat(oldLines.slice(b.y + 1));
let newPos = a;
let that = this;
if (updateHistory) {
that = that.pushHistory(oldLines, this.caretPos);
}
return new FileData(
newLines,
newPos,
newPos,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
} else {
return this;
}
}
// doesn't mutate, returns a new FileData
// updates the history stack
deleteForwards() {
if (this.haveSelection) {
return this.deleteSelection(true);
} else {
let oldLines = this.lines_;
let p = this.caretPos;
let newLines;
// caret doesn't move
if (p.x < oldLines[p.y].length) {
newLines = oldLines.slice(0, p.y);
newLines.push(oldLines[p.y].slice(0, p.x) + oldLines[p.y].slice(p.x+1));
newLines = newLines.concat(oldLines.slice(p.y+1));
} else if (p.y < oldLines.length - 1) {
newLines = oldLines.slice(0, p.y);
newLines.push(oldLines[p.y] + oldLines[p.y + 1]);
newLines = newLines.concat(oldLines.slice(p.y + 2));
} else {
return this;
}
let that = this.pushHistory(oldLines, p);
return new FileData(
newLines,
p,
p,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
}
// doesn't mutate, returns a new FileData
// updates the history stack
deleteBackwards() {
if (this.haveSelection) {
return this.deleteSelection(true);
} else {
let oldLines = this.lines_;
let p = this.caretPos;
let newLines;
let newPos;
if (p.x > 0) {
newLines = oldLines.slice(0, p.y);
newLines.push(oldLines[p.y].slice(0, p.x - 1) + oldLines[p.y].slice(p.x));
newLines = newLines.concat(oldLines.slice(p.y + 1));
newPos = new FilePos(p.x - 1, p.y);
} else if (p.y > 0) {
newLines = oldLines.slice(0, p.y - 1);
newLines.push(oldLines[p.y-1] + oldLines[p.y]);
newLines = newLines.concat(oldLines.slice(p.y+1));
newPos = new FilePos(oldLines[p.y-1].length, p.y-1);
} else {
return this;
}
let that = this.pushHistory(oldLines, p);
return new FileData(
newLines,
newPos,
newPos,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
}
// doesn't mutate, returns a new FileData
// updates the history stack
insert(text) {
let that = this.deleteSelection(false);
let oldLines = that.lines_;
let insLines = stringToLines(text);
let p = that.caretPos;
let newLines = oldLines.slice(0, p.y);
let newPos;
if (insLines.length == 1) {
newLines.push(oldLines[p.y].slice(0, p.x) + insLines[0] + oldLines[p.y].slice(p.x));
newPos = new FilePos(p.x + insLines[0].length, p.y);
} else {
let firstInsLine = insLines.shift();
newLines.push(oldLines[p.y].slice(0, p.x) + firstInsLine);
let lastInsLine = insLines.pop();
newLines = newLines.concat(insLines);
newLines.push(lastInsLine + oldLines[p.y].slice(p.x));
newPos = new FilePos(lastInsLine.length, p.y + insLines.length + 1);
}
newLines = newLines.concat(oldLines.slice(p.y + 1));
that = that.pushHistory(this.lines_, this.caretPos);
return new FileData(
newLines,
newPos,
newPos,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
currentIndent() {
let ind = this.findFirstNonSpace(this.caretPos.y).x;
console.log('current indent:', ind);
return ind;
}
// doesn't mutate, returns a new FileData
// updates the history stack
// TODO: align to TAB boundary
indentSelection() {
let oldLines = this.lines_;
let a = this.selectionStart;
let b = this.selectionEnd;
let newLines = oldLines.slice(0, a.y);
newLines = newLines.concat(oldLines.slice(a.y, b.y+1).map(line => TAB + line));
newLines = newLines.concat(oldLines.slice(b.y+1));
let oldPos0 = this.selPos0_;
let oldPos1 = this.selPos1_;
let newPos0 = new FilePos(oldPos0.x + TAB.length, oldPos0.y);
let newPos1 = new FilePos(oldPos1.x + TAB.length, oldPos1.y);
let that = this.pushHistory(oldLines, this.caretPos);
return new FileData(
newLines,
newPos0,
newPos1,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
// doesn't mutate, returns a new FileData
// updates the history stack
// TODO: align to TAB boundary
unindentSelection() {
let oldLines = this.lines_;
let a = this.selectionStart;
let b = this.selectionEnd;
let newLines = oldLines.slice(0, a.y);
let newPos0 = this.selPos0_;
let newPos1 = this.selPos1_;
for (let y = a.y; y < b.y + 1; y++) {
let [trimmed, trimCount] = trimSpaces(oldLines[y]);
newLines.push(trimmed);
// also update the position
if (y == newPos0.y) {
newPos0 = new FilePos(Math.max(newPos0.x - trimCount, 0), newPos0.y);
}
if (y == newPos1.y) {
newPos1 = new FilePos(Math.max(newPos1.x - trimCount, 0), newPos1.y);
}
}
newLines = newLines.concat(oldLines.slice(b.y+1));
let that = this.pushHistory(oldLines, this.caretPos);
return new FileData(
newLines,
newPos0,
newPos1,
that.viewPos_,
null,
null,
that.history_,
that.histIdx_,
that.histHead_,
);
}
}