/
languageContext.ts
271 lines (240 loc) · 7.87 KB
/
languageContext.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
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
import { posix as path } from 'path';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { createVirtualFiles, forEachEmbeddedFile } from './documentRegistry';
import { LanguageModule, LanguageServiceHost, FileKind } from './types';
export type LanguageContext = ReturnType<typeof createLanguageContext>;
export function createLanguageContext(
host: LanguageServiceHost,
languageModules: LanguageModule[],
) {
for (const languageModule of languageModules.reverse()) {
if (languageModule.proxyLanguageServiceHost) {
const proxyApis = languageModule.proxyLanguageServiceHost(host);
host = new Proxy(host, {
get(target, key: keyof ts.LanguageServiceHost) {
if (key in proxyApis) {
return proxyApis[key];
}
return target[key];
},
});
}
}
let lastProjectVersion: string | undefined;
let tsProjectVersion = 0;
const virtualFiles = createVirtualFiles(languageModules);
const ts = host.getTypeScriptModule();
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const sourceTsFileVersions = new Map<string, string>();
const sourceFileVersions = new Map<string, string>();
const virtualFileVersions = new Map<string, { value: number, virtualFileSnapshot: ts.IScriptSnapshot, sourceFileSnapshot: ts.IScriptSnapshot; }>();
const _tsHost: Partial<ts.LanguageServiceHost> = {
fileExists: host.fileExists
? fileName => {
// .vue.js -> .vue
// .vue.ts -> .vue
// .vue.d.ts -> [ignored]
const vueFileName = fileName.substring(0, fileName.lastIndexOf('.'));
if (!virtualFiles.hasSourceFile(vueFileName)) {
// create virtual files
const scriptSnapshot = host.getScriptSnapshot(vueFileName);
if (scriptSnapshot) {
virtualFiles.update(vueFileName, scriptSnapshot);
}
}
if (virtualFiles.getSourceByVirtualFileName(fileName)) {
return true;
}
return !!host.fileExists?.(fileName);
}
: undefined,
getProjectVersion: () => {
return tsProjectVersion.toString();
},
getScriptFileNames,
getScriptVersion,
getScriptSnapshot,
readDirectory: (_path, extensions, exclude, include, depth) => {
const result = host.readDirectory?.(_path, extensions, exclude, include, depth) ?? [];
for (const [, [fileName]] of virtualFiles.all) {
const vuePath2 = path.join(_path, path.basename(fileName));
if (path.relative(_path.toLowerCase(), fileName.toLowerCase()).startsWith('..')) {
continue;
}
if (!depth && fileName.toLowerCase() === vuePath2.toLowerCase()) {
result.push(vuePath2);
}
else if (depth) {
result.push(vuePath2); // TODO: depth num
}
}
return result;
},
getScriptKind(fileName) {
if (virtualFiles.hasSourceFile(fileName))
return ts.ScriptKind.Deferred;
switch (path.extname(fileName)) {
case '.js': return ts.ScriptKind.JS;
case '.jsx': return ts.ScriptKind.JSX;
case '.ts': return ts.ScriptKind.TS;
case '.tsx': return ts.ScriptKind.TSX;
case '.json': return ts.ScriptKind.JSON;
default: return ts.ScriptKind.Unknown;
}
},
};
return {
typescript: {
languageServiceHost: new Proxy(_tsHost as ts.LanguageServiceHost, {
get: (target, property: keyof ts.LanguageServiceHost) => {
update();
return target[property] || host[property];
},
}),
},
virtualFiles: new Proxy(virtualFiles, {
get: (target, property) => {
update();
return target[property as keyof typeof virtualFiles];
},
}),
};
function update() {
const newProjectVersion = host.getProjectVersion?.();
const shouldUpdate = newProjectVersion === undefined || newProjectVersion !== lastProjectVersion;
lastProjectVersion = newProjectVersion;
if (!shouldUpdate)
return;
let shouldUpdateTsProject = false;
let virtualFilesUpdatedNum = 0;
const remainRootFiles = new Set(host.getScriptFileNames());
// .vue
for (const [_, [fileName]] of virtualFiles.all) {
remainRootFiles.delete(fileName);
const snapshot = host.getScriptSnapshot(fileName);
if (!snapshot) {
// delete
virtualFiles.delete(fileName);
shouldUpdateTsProject = true;
virtualFilesUpdatedNum++;
continue;
}
const newVersion = host.getScriptVersion(fileName);
if (sourceFileVersions.get(fileName) !== newVersion) {
// update
sourceFileVersions.set(fileName, newVersion);
virtualFiles.update(fileName, snapshot);
virtualFilesUpdatedNum++;
}
}
// no any vue file version change, it mean project version was update by ts file change at this time
if (!virtualFilesUpdatedNum) {
shouldUpdateTsProject = true;
}
// add
for (const fileName of [...remainRootFiles]) {
const snapshot = host.getScriptSnapshot(fileName);
if (snapshot) {
const virtualFile = virtualFiles.update(fileName, snapshot);
if (virtualFile) {
remainRootFiles.delete(fileName);
}
}
}
// .ts / .js / .d.ts / .json ...
for (const [oldTsFileName, oldTsFileVersion] of [...sourceTsFileVersions]) {
const newVersion = host.getScriptVersion(oldTsFileName);
if (oldTsFileVersion !== newVersion) {
if (!remainRootFiles.has(oldTsFileName) && !host.getScriptSnapshot(oldTsFileName)) {
// delete
sourceTsFileVersions.delete(oldTsFileName);
}
else {
// update
sourceTsFileVersions.set(oldTsFileName, newVersion);
}
shouldUpdateTsProject = true;
}
}
for (const nowFileName of remainRootFiles) {
if (!sourceTsFileVersions.has(nowFileName)) {
// add
const newVersion = host.getScriptVersion(nowFileName);
sourceTsFileVersions.set(nowFileName, newVersion);
shouldUpdateTsProject = true;
}
}
for (const [_, [_1, _2, virtualFile]] of virtualFiles.all) {
if (!shouldUpdateTsProject) {
forEachEmbeddedFile(virtualFile, embedded => {
if (embedded.kind === FileKind.TypeScriptHostFile) {
if (virtualFileVersions.has(embedded.fileName) && virtualFileVersions.get(embedded.fileName)?.virtualFileSnapshot !== embedded.snapshot) {
shouldUpdateTsProject = true;
}
}
});
}
}
if (shouldUpdateTsProject) {
tsProjectVersion++;
}
}
function getScriptFileNames() {
const tsFileNames = new Set<string>();
for (const [_, [_1, _2, sourceFile]] of virtualFiles.all) {
forEachEmbeddedFile(sourceFile, embedded => {
if (embedded.kind === FileKind.TypeScriptHostFile) {
tsFileNames.add(embedded.fileName); // virtual .ts
}
});
}
for (const fileName of host.getScriptFileNames()) {
if (!virtualFiles.hasSourceFile(fileName)) {
tsFileNames.add(fileName); // .ts
}
}
return [...tsFileNames];
}
function getScriptVersion(fileName: string) {
let source = virtualFiles.getSourceByVirtualFileName(fileName);
if (source) {
let version = virtualFileVersions.get(source[2].fileName);
if (!version) {
version = {
value: 0,
virtualFileSnapshot: source[2].snapshot,
sourceFileSnapshot: source[1],
};
virtualFileVersions.set(source[2].fileName, version);
}
else if (
version.virtualFileSnapshot !== source[2].snapshot
|| (host.isTsc && version.sourceFileSnapshot !== source[1]) // fix https://github.com/johnsoncodehk/volar/issues/1082
) {
version.value++;
version.virtualFileSnapshot = source[2].snapshot;
version.sourceFileSnapshot = source[1];
}
return version.value.toString();
}
return host.getScriptVersion(fileName);
}
function getScriptSnapshot(fileName: string) {
const version = getScriptVersion(fileName);
const cache = scriptSnapshots.get(fileName.toLowerCase());
if (cache && cache[0] === version) {
return cache[1];
}
const source = virtualFiles.getSourceByVirtualFileName(fileName);
if (source) {
const snapshot = source[2].snapshot;
scriptSnapshots.set(fileName.toLowerCase(), [version, snapshot]);
return snapshot;
}
let tsScript = host.getScriptSnapshot(fileName);
if (tsScript) {
scriptSnapshots.set(fileName.toLowerCase(), [version, tsScript]);
return tsScript;
}
}
}