forked from clausreinke/typescript-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathharness.ts
More file actions
128 lines (106 loc) · 4.14 KB
/
harness.ts
File metadata and controls
128 lines (106 loc) · 4.14 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
// Copyright (c) Microsoft, Claus Reinke. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='typings/node/node.d.ts'/>
///<reference path='node_modules/typescript/lib/typescript.d.ts'/>
import ts = require("typescript");
// TODO: avoid pre-computing line starts, can tss use SourceFiles instead?
/* @internal, from scanner.ts */
function computeLineStarts(text: string): number[] {
let result: number[] = new Array();
let pos = 0;
let lineStart = 0;
while (pos < text.length) {
let ch = text.charCodeAt(pos++);
switch (ch) {
case 0x0D:
if (text.charCodeAt(pos) === 0x0A) {
pos++;
}
case 0x0A:
result.push(lineStart);
lineStart = pos;
break;
default:
if (ch > 0x7F && ts.isLineBreak(ch)) {
result.push(lineStart);
lineStart = pos;
}
break;
}
}
result.push(lineStart);
return result;
}
export class ScriptInfo {
public version: number = 1;
public editRanges: { length: number; textChangeRange: ts.TextChangeRange; }[] = [];
constructor(public fileName: string, public content: string, public isOpen = true) {
this.setContent(content);
}
private setContent(content: string): void {
this.content = content;
}
public updateContent(content: string): void {
var old_length = this.content.length;
this.setContent(content);
this.editRanges.push({
length: content.length,
textChangeRange:
// NOTE: no shortcut for "update everything" (null only works in some places, #10)
ts.createTextChangeRange(ts.createTextSpan(0,old_length),content.length)
});
this.version++;
}
public editContent(minChar: number, limChar: number, newText: string): void {
// Apply edits
var prefix = this.content.substring(0, minChar);
var middle = newText;
var suffix = this.content.substring(limChar);
//console.log('editContent', {prefix, middle, suffix, content:this.content, newContent: prefix + middle + suffix});
this.setContent(prefix + middle + suffix);
// Store edit range + new length of script
this.editRanges.push({
length: this.content.length,
textChangeRange:
ts.createTextChangeRange( ts.createTextSpanFromBounds(minChar, limChar)
, newText.length)
});
// Update version #
this.version++;
}
public getTextChangeRangeBetweenVersions(startVersion: number, endVersion: number): ts.TextChangeRange {
if (startVersion === endVersion) {
// No edits!
return ts.unchangedTextChangeRange;
}
var initialEditRangeIndex = this.editRanges.length - (this.version - startVersion);
var lastEditRangeIndex = this.editRanges.length - (this.version - endVersion);
var entries = this.editRanges.slice(initialEditRangeIndex, lastEditRangeIndex);
return ts.collapseTextChangeRangesAcrossMultipleVersions(entries.map(e => e.textChangeRange));
}
}
export class ScriptSnapshot implements ts.IScriptSnapshot {
private lineMap: number[] = null;
private textSnapshot: string;
private version: number;
constructor(private scriptInfo: ScriptInfo) {
this.textSnapshot = scriptInfo.content;
this.version = scriptInfo.version;
}
public getText(start: number, end: number): string {
return this.textSnapshot.substring(start, end);
}
public getLength(): number {
return this.textSnapshot.length;
}
private getLineStartPositions(): number[] {
if (this.lineMap === null) {
this.lineMap = computeLineStarts(this.textSnapshot);
}
return this.lineMap;
}
public getChangeRange(oldSnapshot: ts.IScriptSnapshot): ts.TextChangeRange {
return undefined;
}
}