/
lsContext.ts
328 lines (294 loc) · 9.63 KB
/
lsContext.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
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
import { posix as path } from 'path';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { LanguageServiceHost } from './types';
import * as localTypes from './utils/localTypes';
import { createSourceFile, EmbeddedFile, VueLanguagePlugin } from './sourceFile';
import { createDocumentRegistry } from './documentRegistry';
export type LanguageServiceContext = ReturnType<typeof createLanguageServiceContext>;
export function createLanguageServiceContext(
ts: typeof import('typescript/lib/tsserverlibrary'),
host: LanguageServiceHost,
extraPlugins: VueLanguagePlugin[] = [],
exts = ['.vue', '.md', '.html'],
) {
let lastProjectVersion: string | undefined;
let tsProjectVersion = 0;
const documentRegistry = createDocumentRegistry();
const compilerOptions = host.getCompilationSettings();
const vueCompilerOptions = host.getVueCompilationSettings();
const tsFileVersions = new Map<string, string>();
const sharedTypesScript = ts.ScriptSnapshot.fromString(localTypes.getTypesCode(vueCompilerOptions.target ?? 3));
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const fileVersions = new WeakMap<EmbeddedFile, string>();
const _tsHost: Partial<ts.LanguageServiceHost> = {
fileExists: host.fileExists
? fileName => {
// .vue.js -> .vue
// .vue.ts -> .vue
// .vue.d.ts (never)
const vueFileName = fileName.substring(0, fileName.lastIndexOf('.'));
if (exts.some(ext => vueFileName.endsWith(ext))) {
const vueFile = documentRegistry.get(vueFileName);
if (!vueFile) {
const fileExists = !!host.fileExists?.(vueFileName);
if (fileExists) {
// create virtual files
const scriptSnapshot = host.getScriptSnapshot(vueFileName);
if (scriptSnapshot) {
documentRegistry.set(vueFileName, createSourceFile(
vueFileName,
scriptSnapshot.getText(0, scriptSnapshot.getLength()),
compilerOptions,
vueCompilerOptions,
ts,
extraPlugins,
));
}
}
}
}
if (!!documentRegistry.fromEmbeddedFileName(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 vuePath of documentRegistry.getFileNames()) {
const vuePath2 = path.join(_path, path.basename(vuePath));
if (path.relative(_path.toLowerCase(), vuePath.toLowerCase()).startsWith('..')) {
continue;
}
if (!depth && vuePath.toLowerCase() === vuePath2.toLowerCase()) {
result.push(vuePath2);
}
else if (depth) {
result.push(vuePath2); // TODO: depth num
}
}
return result;
},
getScriptKind(fileName) {
if (exts.some(ext => fileName.endsWith(ext)))
return ts.ScriptKind.TSX; // can't use External, Unknown
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;
}
},
};
const tsHost = new Proxy<ts.LanguageServiceHost>(_tsHost as ts.LanguageServiceHost, {
get: (target, property: keyof ts.LanguageServiceHost) => {
update();
return target[property] || host[property];
},
});
return {
typescriptLanguageServiceHost: tsHost,
typescriptLanguageService: ts.createLanguageService(tsHost),
get sourceFiles() {
update();
return documentRegistry;
},
};
function update() {
const newProjectVersion = host.getProjectVersion?.();
const sholdUpdate = newProjectVersion === undefined || newProjectVersion !== lastProjectVersion;
if (!sholdUpdate)
return;
lastProjectVersion = newProjectVersion;
const fileNames = host.getScriptFileNames();
const vueFileNames = new Set(fileNames.filter(file => exts.some(ext => file.endsWith(ext))));
const tsFileNames = new Set(fileNames.filter(file => !exts.some(ext => file.endsWith(ext))));
const fileNamesToRemove: string[] = [];
const fileNamesToCreate: string[] = [];
const fileNamesToUpdate: string[] = [];
let tsFileUpdated = false;
// .vue
for (const vueFile of documentRegistry.getAll()) {
if (!vueFileNames.has(vueFile.fileName) && !host.fileExists?.(vueFile.fileName)) {
// delete
fileNamesToRemove.push(vueFile.fileName);
}
else {
// update
const newSnapshot = host.getScriptSnapshot(vueFile.fileName);
if (vueFile.text !== newSnapshot?.getText(0, newSnapshot.getLength())) {
fileNamesToUpdate.push(vueFile.fileName);
}
}
}
for (const nowFileName of vueFileNames) {
if (!documentRegistry.get(nowFileName)) {
// add
fileNamesToCreate.push(nowFileName);
}
}
// .ts / .js / .d.ts / .json ...
for (const tsFileVersion of tsFileVersions) {
if (!vueFileNames.has(tsFileVersion[0]) && !host.fileExists?.(tsFileVersion[0])) {
// delete
tsFileVersions.delete(tsFileVersion[0]);
tsFileUpdated = true;
}
else {
// update
const newVersion = host.getScriptVersion(tsFileVersion[0]);
if (tsFileVersion[1] !== newVersion) {
tsFileVersions.set(tsFileVersion[0], newVersion);
tsFileUpdated = true;
}
}
}
for (const nowFileName of tsFileNames) {
if (!tsFileVersions.has(nowFileName)) {
// add
const newVersion = host.getScriptVersion(nowFileName);
tsFileVersions.set(nowFileName, newVersion);
tsFileUpdated = true;
}
}
for (const uri of fileNamesToRemove) {
if (documentRegistry.delete(uri)) {
tsFileUpdated = true;
}
}
for (const fileName of [
...fileNamesToCreate,
...fileNamesToUpdate,
]) {
const scriptSnapshot = host.getScriptSnapshot(fileName);
if (!scriptSnapshot) {
continue;
}
const sourceFile = documentRegistry.get(fileName);
const scriptText = scriptSnapshot.getText(0, scriptSnapshot.getLength());
if (!sourceFile) {
documentRegistry.set(fileName, createSourceFile(
fileName,
scriptText,
compilerOptions,
vueCompilerOptions,
ts,
extraPlugins,
));
tsFileUpdated = true;
}
else {
const oldScripts: Record<string, string> = {};
const newScripts: Record<string, string> = {};
if (!tsFileUpdated) {
for (const embedded of sourceFile.getAllEmbeddeds()) {
if (embedded.file.isTsHostFile) {
oldScripts[embedded.file.fileName] = embedded.file.content;
}
}
}
sourceFile.text = scriptText;
if (!tsFileUpdated) {
for (const embedded of sourceFile.getAllEmbeddeds()) {
if (embedded.file.isTsHostFile) {
newScripts[embedded.file.fileName] = embedded.file.content;
}
}
}
if (
Object.keys(oldScripts).length !== Object.keys(newScripts).length
|| Object.keys(oldScripts).some(fileName => oldScripts[fileName] !== newScripts[fileName])
) {
tsFileUpdated = true;
}
}
}
if (tsFileUpdated) {
tsProjectVersion++;
}
}
function getScriptFileNames() {
const tsFileNames = documentRegistry.getDirs().map(dir => path.join(dir, localTypes.typesFileName));
for (const mapped of documentRegistry.getAllEmbeddeds()) {
if (mapped.embedded.file.isTsHostFile) {
tsFileNames.push(mapped.embedded.file.fileName); // virtual .ts
}
}
for (const fileName of host.getScriptFileNames()) {
if (host.isTsPlugin) {
tsFileNames.push(fileName); // .vue + .ts
}
else if (!exts.some(ext => fileName.endsWith(ext))) {
tsFileNames.push(fileName); // .ts
}
}
return tsFileNames;
}
function getScriptVersion(fileName: string) {
const basename = path.basename(fileName);
if (basename === localTypes.typesFileName) {
return '';
}
let mapped = documentRegistry.fromEmbeddedFileName(fileName);
if (mapped) {
if (fileVersions.has(mapped.embedded.file)) {
return fileVersions.get(mapped.embedded.file)!;
}
else {
let version = ts.sys.createHash?.(mapped.embedded.file.content) ?? mapped.embedded.file.content;
if (host.isTsc) {
// fix https://github.com/johnsoncodehk/volar/issues/1082
version = host.getScriptVersion(mapped.vueFile.fileName) + ':' + version;
}
fileVersions.set(mapped.embedded.file, version);
return version;
}
}
return host.getScriptVersion(fileName);
}
function getScriptSnapshot(fileName: string) {
const basename = path.basename(fileName);
if (basename === localTypes.typesFileName) {
return sharedTypesScript;
}
const version = getScriptVersion(fileName);
const cache = scriptSnapshots.get(fileName.toLowerCase());
if (cache && cache[0] === version) {
return cache[1];
}
const mapped = documentRegistry.fromEmbeddedFileName(fileName);
if (mapped) {
const text = mapped.embedded.file.content;
const snapshot = ts.ScriptSnapshot.fromString(text);
scriptSnapshots.set(fileName.toLowerCase(), [version, snapshot]);
return snapshot;
}
let tsScript = host.getScriptSnapshot(fileName);
if (tsScript) {
if (!(vueCompilerOptions.strictTemplates ?? false) && (
// for vue 2.6 and vue 3
basename === 'runtime-dom.d.ts' ||
// for vue 2.7
basename === 'jsx.d.ts'
)) {
// allow arbitrary attributes
let tsScriptText = tsScript.getText(0, tsScript.getLength());
tsScriptText = tsScriptText.replace(
'type ReservedProps = {',
'type ReservedProps = { [name: string]: any',
);
tsScript = ts.ScriptSnapshot.fromString(tsScriptText);
}
scriptSnapshots.set(fileName.toLowerCase(), [version, tsScript]);
return tsScript;
}
}
}