-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathscriptInfo.ts
207 lines (180 loc) · 7.08 KB
/
scriptInfo.ts
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
/// <reference path="scriptVersionCache.ts"/>
namespace ts.server {
export class ScriptInfo {
/**
* All projects that include this file
*/
readonly containingProjects: Project[] = [];
private formatCodeSettings: ts.FormatCodeSettings;
readonly path: Path;
private fileWatcher: FileWatcher;
private svc: ScriptVersionCache;
// TODO: allow to update hasMixedContent from the outside
constructor(
private readonly host: ServerHost,
readonly fileName: NormalizedPath,
content: string,
readonly scriptKind: ScriptKind,
public isOpen = false,
public hasMixedContent = false) {
this.path = toPath(fileName, host.getCurrentDirectory(), createGetCanonicalFileName(host.useCaseSensitiveFileNames));
this.svc = ScriptVersionCache.fromString(host, content);
this.scriptKind = scriptKind
? scriptKind
: getScriptKindFromFileName(fileName);
}
getFormatCodeSettings() {
return this.formatCodeSettings;
}
attachToProject(project: Project): boolean {
const isNew = !this.isAttached(project);
if (isNew) {
this.containingProjects.push(project);
}
return isNew;
}
isAttached(project: Project) {
// unrolled for common cases
switch (this.containingProjects.length) {
case 0: return false;
case 1: return this.containingProjects[0] === project;
case 2: return this.containingProjects[0] === project || this.containingProjects[1] === project;
default: return contains(this.containingProjects, project);
}
}
detachFromProject(project: Project) {
// unrolled for common cases
switch (this.containingProjects.length) {
case 0:
return;
case 1:
if (this.containingProjects[0] === project) {
this.containingProjects.pop();
}
break;
case 2:
if (this.containingProjects[0] === project) {
this.containingProjects[0] = this.containingProjects.pop();
}
else if (this.containingProjects[1] === project) {
this.containingProjects.pop();
}
break;
default:
removeItemFromSet(this.containingProjects, project);
break;
}
}
detachAllProjects() {
for (const p of this.containingProjects) {
// detach is unnecessary since we'll clean the list of containing projects anyways
p.removeFile(this, /*detachFromProjects*/ false);
}
this.containingProjects.length = 0;
}
getDefaultProject() {
if (this.containingProjects.length === 0) {
return Errors.ThrowNoProject();
}
return this.containingProjects[0];
}
setFormatOptions(formatSettings: FormatCodeSettings): void {
if (formatSettings) {
if (!this.formatCodeSettings) {
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
}
mergeMaps(this.formatCodeSettings, formatSettings);
}
}
setWatcher(watcher: FileWatcher): void {
this.stopWatcher();
this.fileWatcher = watcher;
}
stopWatcher() {
if (this.fileWatcher) {
this.fileWatcher.close();
this.fileWatcher = undefined;
}
}
getLatestVersion() {
return this.svc.latestVersion().toString();
}
reload(script: string) {
this.svc.reload(script);
this.markContainingProjectsAsDirty();
}
saveTo(fileName: string) {
const snap = this.snap();
this.host.writeFile(fileName, snap.getText(0, snap.getLength()));
}
reloadFromFile(tempFileName?: NormalizedPath) {
if (this.hasMixedContent) {
this.reload("");
}
else {
this.svc.reloadFromFile(tempFileName || this.fileName);
this.markContainingProjectsAsDirty();
}
}
snap() {
return this.svc.getSnapshot();
}
getLineInfo(line: number) {
const snap = this.snap();
return snap.index.lineNumberToInfo(line);
}
editContent(start: number, end: number, newText: string): void {
this.svc.edit(start, end - start, newText);
this.markContainingProjectsAsDirty();
}
markContainingProjectsAsDirty() {
for (const p of this.containingProjects) {
p.markAsDirty();
}
}
/**
* @param line 1 based index
*/
lineToTextSpan(line: number) {
const index = this.snap().index;
const lineInfo = index.lineNumberToInfo(line + 1);
let len: number;
if (lineInfo.leaf) {
len = lineInfo.leaf.text.length;
}
else {
const nextLineInfo = index.lineNumberToInfo(line + 2);
len = nextLineInfo.offset - lineInfo.offset;
}
return ts.createTextSpan(lineInfo.offset, len);
}
/**
* @param line 1 based index
* @param offset 1 based index
*/
lineOffsetToPosition(line: number, offset: number): number {
const index = this.snap().index;
const lineInfo = index.lineNumberToInfo(line);
// TODO: assert this offset is actually on the line
return (lineInfo.offset + offset - 1);
}
/**
* @param position 0-based position index
* @return ILineInfo with 1-based indexing for line numbers and offsets.
*/
positionToOneIndexedLineOffset(position: number): ILineInfo {
const index = this.snap().index;
const lineOffset = index.charOffsetToLineNumberAndPos(position);
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
}
/**
* @param position 0-based position index
* @return ILineInfo with 0-based indexing for line numbers and offsets.
*/
positionToZeroIndexedLineOffset(position: number): ILineInfo {
const index = this.snap().index;
const lineOffset = index.charOffsetToLineNumberAndPos(position);
return { line: lineOffset.line - 1, offset: lineOffset.offset };
}
}
}