/
fileSystem.ts
107 lines (100 loc) · 3.26 KB
/
fileSystem.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
import * as shared from '@volar/shared';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { createUriMap } from '../common/utils/uriMap';
import { FileSystem, FileSystemHost } from '../types';
let currentCwd = '';
export function createNodeFileSystemHost(
ts: typeof import('typescript/lib/tsserverlibrary'),
capabilities: vscode.ClientCapabilities,
): FileSystemHost {
const instances = createUriMap<[FileSystem, Map<string, any>[]]>();
const onDidChangeWatchedFilesCb = new Set<(params: vscode.DidChangeWatchedFilesParams) => void>();
return {
ready(connection) {
connection.onDidChangeWatchedFiles(async params => {
if (params.changes.some(change => change.type === vscode.FileChangeType.Created || change.type === vscode.FileChangeType.Deleted)) {
for (const sys of instances.values()) {
sys[1].forEach(cache => cache.clear());
}
}
for (const cb of [...onDidChangeWatchedFilesCb]) {
if (onDidChangeWatchedFilesCb.has(cb)) {
await cb(params);
}
}
});
},
reload() {
for (const sys of instances.values()) {
sys[1].forEach(cache => cache.clear());
}
},
getWorkspaceFileSystem(rootUri: URI) {
let sys = instances.uriGet(rootUri.toString());
if (!sys) {
sys = createWorkspaceFileSystem(ts, capabilities, rootUri);
instances.uriSet(rootUri.toString(), sys);
}
return sys[0];
},
onDidChangeWatchedFiles: cb => {
onDidChangeWatchedFilesCb.add(cb);
return () => onDidChangeWatchedFilesCb.delete(cb);
},
};
function createWorkspaceFileSystem(
ts: typeof import('typescript/lib/tsserverlibrary'),
capabilities: vscode.ClientCapabilities,
rootUri: URI,
): [FileSystem, Map<string, any>[]] {
const rootPath = shared.getPathOfUri(rootUri.toString());
const workspaceSys = new Proxy(ts.sys, {
get(target, prop) {
const fn = target[prop as keyof typeof target];
if (typeof fn === 'function') {
return new Proxy(fn, {
apply(target, thisArg, args) {
if (currentCwd !== rootPath) {
if (ts.sys.directoryExists(rootPath)) { // #2234, #2039
process.chdir(rootPath);
}
currentCwd = rootPath;
}
return (target as any).apply(thisArg, args);
}
});
}
return fn;
},
});
const fileExistsCache = new Map<string, boolean>();
const directoryExistsCache = new Map<string, boolean>();
// don't cache fs result if client did not supports file watcher
const sys = capabilities.workspace?.didChangeWatchedFiles
? new Proxy<Partial<ts.System>>({
fileExists(path: string) {
if (!fileExistsCache.has(path)) {
fileExistsCache.set(path, workspaceSys.fileExists(path));
}
return fileExistsCache.get(path)!;
},
directoryExists(path: string) {
if (!directoryExistsCache.has(path)) {
directoryExistsCache.set(path, workspaceSys.directoryExists(path));
}
return directoryExistsCache.get(path)!;
},
}, {
get(target, prop) {
if (prop in target) {
return target[prop as keyof typeof target];
}
return workspaceSys[prop as keyof typeof workspaceSys];
},
}) as ts.System
: workspaceSys;
return [sys, [fileExistsCache, directoryExistsCache]];
}
}