-
Notifications
You must be signed in to change notification settings - Fork 15
/
script-snapshot.js
53 lines (41 loc) · 1.19 KB
/
script-snapshot.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
import ts from "typescript";
import * as jsdiff from "diff";
import logger from "./logger";
export default class StringScriptSnapshot {
constructor(text) {
this.text = text;
}
getText(start, end) {
return this.text.substring(start, end);
}
getLength() {
return this.text.length;
}
getChangeRange(oldSnapshot) {
if (!oldSnapshot) return undefined;
const diffs = jsdiff.diffChars(oldSnapshot.text, this.text);
if (diffs.length) {
let ind = 0;
let changes = [];
for (let i = 0; i < diffs.length; i++) {
const diff = diffs[i];
if (diff.added) {
changes.push(ts.createTextChangeRange(
ts.createTextSpan(ind, 0), diff.count));
ind += diff.count;
continue;
}
if (diff.removed) {
changes.push(ts.createTextChangeRange(
ts.createTextSpan(ind, diff.count), 0));
continue;
}
ind += diff.count;
}
changes = ts.collapseTextChangeRangesAcrossMultipleVersions(changes);
logger.assert("accumulated file changes %j", changes);
return changes;
}
return ts.createTextChangeRange(ts.createTextSpan(0, 0), 0);
}
}