-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
sys.ts
417 lines (371 loc) Β· 10.4 KB
/
sys.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import type { FileChangeType, FileType, ServiceEnvironment, Disposable, FileStat } from '@volar/language-service';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path-browserify';
import { matchFiles } from './typescript/utilities';
interface File {
text?: string;
stat?: FileStat;
requestedText?: boolean;
requestedStat?: boolean;
}
interface Dir {
dirs: Map<string, Dir>;
files: Map<string, File>;
exists?: boolean;
requestedRead?: boolean;
}
let currentCwd = '';
export function createSys(
ts: typeof import('typescript/lib/tsserverlibrary'),
env: ServiceEnvironment,
): ts.System & {
version: number;
sync(): Promise<number>;
} & Disposable {
let version = 0;
const rootPath = env.uriToFileName(env.workspaceUri.toString());
const sys = ts.sys as ts.System | undefined;
const root: Dir = {
dirs: new Map(),
files: new Map(),
requestedRead: false,
};
const promises = new Set<Thenable<any>>();
const fileWatcher = env.onDidChangeWatchedFiles?.(({ changes }) => {
for (const change of changes) {
const fileName = env.uriToFileName(change.uri);
const dirName = path.dirname(fileName);
const baseName = path.basename(fileName);
const dir = getDir(dirName);
if (dir.files.has(baseName)) { // is requested file
version++;
if (change.type === 1 satisfies typeof FileChangeType.Created || change.type === 2 satisfies typeof FileChangeType.Changed) {
dir.files.set(baseName, {
stat: {
type: 1 satisfies FileType.File,
ctime: Date.now(),
mtime: Date.now(),
size: -1,
},
requestedStat: false,
});
}
else if (change.type === 3 satisfies typeof FileChangeType.Deleted) {
dir.files.set(baseName, {
stat: undefined,
text: undefined,
requestedStat: true,
requestedText: true,
});
}
}
}
});
return {
get version() {
return version;
},
dispose() {
fileWatcher?.dispose();
},
args: sys?.args ?? [],
newLine: sys?.newLine ?? '\n',
useCaseSensitiveFileNames: sys?.useCaseSensitiveFileNames ?? false,
realpath: sys?.realpath,
write: sys?.write ?? (() => { }),
writeFile: sys?.writeFile ?? (() => { }),
createDirectory: sys?.createDirectory ?? (() => { }),
exit: sys?.exit ?? (() => { }),
getExecutingFilePath: sys?.getExecutingFilePath ?? (() => rootPath + '/__fake__.js'),
getCurrentDirectory: () => rootPath,
getModifiedTime,
readFile,
readDirectory,
getDirectories,
resolvePath,
fileExists,
directoryExists,
async sync() {
while (promises.size) {
await Promise.all(promises);
}
return version;
},
};
function resolvePath(fsPath: string) {
if (sys) {
if (currentCwd !== rootPath) {
currentCwd = rootPath;
// https://github.com/vuejs/language-tools/issues/2039
// https://github.com/vuejs/language-tools/issues/2234
if (sys.directoryExists(rootPath)) {
// https://github.com/vuejs/language-tools/issues/2480
try {
// @ts-expect-error
process.chdir(rootPath);
} catch { }
}
}
return sys.resolvePath(fsPath).replace(/\\/g, '/');
}
return path.resolve(fsPath).replace(/\\/g, '/');
}
function readFile(fileName: string, encoding?: string) {
fileName = resolvePath(fileName);
const dirPath = path.dirname(fileName);
const dir = getDir(dirPath);
const name = path.basename(fileName);
readFileWorker(fileName, encoding, dir);
return dir.files.get(name)?.text;
}
function directoryExists(dirName: string): boolean {
dirName = resolvePath(dirName);
const dir = getDir(dirName);
if (dir.exists === undefined) {
dir.exists = false;
const result = env.fs?.stat(env.fileNameToUri(dirName));
if (typeof result === 'object' && 'then' in result) {
const promise = result;
promises.add(promise);
result.then(result => {
promises.delete(promise);
dir.exists = result?.type === 2 satisfies FileType.Directory;
if (dir.exists) {
version++;
}
});
}
else {
dir.exists = result?.type === 2 satisfies FileType.Directory;
}
}
return dir.exists;
}
function getModifiedTime(fileName: string): Date {
fileName = resolvePath(fileName);
const file = getFile(fileName);
if (!file.requestedStat) {
file.requestedStat = true;
handleStat(fileName, file);
}
return file.stat ? new Date(file.stat.mtime) : new Date(0);
}
function fileExists(fileName: string): boolean {
fileName = resolvePath(fileName);
const file = getFile(fileName);
const exists = () => file.text !== undefined || file.stat?.type === 1 satisfies FileType.File;
if (exists()) {
return true;
}
if (!file.requestedStat) {
file.requestedStat = true;
handleStat(fileName, file);
}
return exists();
}
function handleStat(fileName: string, file: File) {
const result = env.fs?.stat(env.fileNameToUri(fileName));
if (typeof result === 'object' && 'then' in result) {
const promise = result;
promises.add(promise);
result.then(result => {
promises.delete(promise);
if (file.stat?.type !== result?.type || file.stat?.mtime !== result?.mtime) {
version++;
}
file.stat = result;
});
}
else {
file.stat = result;
}
}
function getFile(fileName: string) {
fileName = resolvePath(fileName);
const dirPath = path.dirname(fileName);
const baseName = path.basename(fileName);
const dir = getDir(dirPath);
let file = dir.files.get(baseName);
if (!file) {
dir.files.set(baseName, file = {});
}
return file;
}
// for import path completion
function getDirectories(dirName: string) {
dirName = resolvePath(dirName);
readDirectoryWorker(dirName);
const dir = getDir(dirName);
return [...dir.dirs.entries()].filter(([_, dir]) => dir.exists).map(([name]) => name);
}
function readDirectory(
dirName: string,
extensions?: readonly string[],
excludes?: readonly string[],
includes?: readonly string[],
depth?: number,
) {
dirName = resolvePath(dirName);
const matches = matchFiles(
dirName,
extensions,
excludes,
includes,
sys?.useCaseSensitiveFileNames ?? false,
rootPath,
depth,
(dirPath) => {
dirPath = resolvePath(dirPath);
readDirectoryWorker(dirPath);
const dir = getDir(dirPath);
return {
files: [...dir.files.entries()].filter(([_, file]) => file.stat?.type === 1 satisfies FileType.File).map(([name]) => name),
directories: [...dir.dirs.entries()].filter(([_, dir]) => dir.exists).map(([name]) => name),
};
},
sys?.realpath ? (path => sys.realpath!(path)) : (path => path),
);
return [...new Set(matches)];
}
function readFileWorker(fileName: string, encoding: string | undefined, dir: Dir) {
const name = path.basename(fileName);
let file = dir.files.get(name);
if (!file) {
dir.files.set(name, file = {});
}
if (file.requestedText) {
return;
}
file.requestedText = true;
const uri = env.fileNameToUri(fileName);
const result = env.fs?.readFile(uri, encoding);
if (typeof result === 'object' && 'then' in result) {
const promise = result;
promises.add(promise);
result.then(result => {
promises.delete(promise);
if (result !== undefined) {
file!.text = result;
if (file!.stat) {
file!.stat.mtime++;
}
version++;
}
});
}
else if (result !== undefined) {
file.text = result;
}
}
function readDirectoryWorker(dirName: string) {
const dir = getDir(dirName);
if (dir.requestedRead) {
return;
}
dir.requestedRead = true;
const result = env.fs?.readDirectory(env.fileNameToUri(dirName || '.'));
if (typeof result === 'object' && 'then' in result) {
const promise = result;
promises.add(promise);
result.then((result) => {
promises.delete(promise);
if (onReadDirectoryResult(dirName, dir, result)) {
version++;
}
});
}
else {
onReadDirectoryResult(dirName, dir, result ?? []);
}
}
function onReadDirectoryResult(dirName: string, dir: Dir, result: [string, FileType][]) {
// See https://github.com/microsoft/TypeScript/blob/e1a9290051a3b0cbdfbadc3adbcc155a4641522a/src/compiler/sys.ts#L1853-L1857
result = result.filter(([name]) => name !== '.' && name !== '..');
let updated = false;
for (const [name, _fileType] of result) {
let fileType = _fileType;
if (fileType === 64 satisfies FileType.SymbolicLink) {
const stat = env.fs?.stat(env.fileNameToUri(dirName + '/' + name));
if (typeof stat === 'object' && 'then' in stat) {
const promise = stat;
promises.add(promise);
stat.then((stat) => {
promises.delete(promise);
if (stat?.type === 1 satisfies FileType.File) {
let file = dir.files.get(name);
if (!file) {
dir.files.set(name, file = {});
}
if (stat.type !== file.stat?.type || stat.mtime !== file.stat?.mtime) {
version++;
}
file.stat = stat;
file.requestedStat = true;
}
else if (stat?.type === 2 satisfies FileType.Directory) {
const childDir = getDirFromDir(dir, name);
if (!childDir.exists) {
childDir.exists = true;
version++;
}
}
});
}
else if (stat) {
fileType = stat.type;
}
}
if (fileType === 1 satisfies FileType.File) {
let file = dir.files.get(name);
if (!file) {
dir.files.set(name, file = {});
}
if (!file.stat) {
file.stat = {
type: 1 satisfies FileType.File,
mtime: 0,
ctime: 0,
size: 0,
};
updated = true;
}
}
else if (fileType === 2 satisfies FileType.Directory) {
const childDir = getDirFromDir(dir, name);
if (!childDir.exists) {
childDir.exists = true;
updated = true;
}
}
}
return updated;
}
function getDir(dirName: string) {
const dirNames: string[] = [];
let currentDirPath = dirName;
let currentDirName = path.basename(currentDirPath);
let lastDirPath: string | undefined;
while (lastDirPath !== currentDirPath) {
lastDirPath = currentDirPath;
dirNames.push(currentDirName);
currentDirPath = path.dirname(currentDirPath);
currentDirName = path.basename(currentDirPath);
}
let currentDir = root;
for (let i = dirNames.length - 1; i >= 0; i--) {
const nextDirName = dirNames[i];
currentDir = getDirFromDir(currentDir, nextDirName);
}
return currentDir;
}
function getDirFromDir(dir: Dir, name: string) {
let target = dir.dirs.get(name);
if (!target) {
dir.dirs.set(name, target = {
dirs: new Map(),
files: new Map(),
});
}
return target;
}
}