-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathworkspaceProjects.ts
268 lines (234 loc) · 7.8 KB
/
workspaceProjects.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
import * as shared from '@volar/shared';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'upath';
import * as vscode from 'vscode-languageserver';
import { createProject, Project } from './project';
import type { createConfigurationHost } from './configurationHost';
import { LanguageConfigs, RuntimeEnvironment, FileSystemHost, ServerInitializationOptions } from '../types';
import { createSnapshots } from './snapshots';
import { getInferredCompilerOptions } from './inferredCompilerOptions';
import { URI } from 'vscode-uri';
export const rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];
export async function createWorkspaceProjects(
runtimeEnv: RuntimeEnvironment,
languageConfigs: LanguageConfigs,
fsHost: FileSystemHost,
rootUri: URI,
ts: typeof import('typescript/lib/tsserverlibrary'),
tsLocalized: ts.MapLike<string> | undefined,
options: ServerInitializationOptions,
documents: ReturnType<typeof createSnapshots>,
connection: vscode.Connection,
configHost: ReturnType<typeof createConfigurationHost> | undefined,
) {
let inferredProject: Project | undefined;
const sys = fsHost.getWorkspaceFileSystem(rootUri);
const inferOptions = await getInferredCompilerOptions(ts, configHost);
const projects = shared.createUriAndPathMap<Project>(rootUri);
const rootTsConfigs = new Set(sys.readDirectory(rootUri.fsPath, rootTsConfigNames, undefined, ['**/*']));
const disposeWatch = fsHost.onDidChangeWatchedFiles(async (params, reason) => {
const disposes: Promise<any>[] = [];
for (const change of params.changes) {
if (rootTsConfigNames.includes(path.basename(change.uri))) {
if (change.type === vscode.FileChangeType.Created) {
if (shared.isFileInDir(URI.parse(change.uri).fsPath, rootUri.fsPath)) {
rootTsConfigs.add(URI.parse(change.uri).fsPath);
}
}
else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted) && projects.uriHas(change.uri)) {
if (change.type === vscode.FileChangeType.Deleted) {
rootTsConfigs.delete(URI.parse(change.uri).fsPath);
}
const _project = projects.uriGet(change.uri);
projects.uriDelete(change.uri);
disposes.push((async () => {
(await _project)?.dispose();
})());
}
}
}
if (reason === 'web-cache-updated' && params.changes.some(change => change.uri.indexOf('/node_modules/') >= 0)) {
clearProjects();
}
return Promise.all(disposes);
});
return {
projects,
getProjectAndTsConfig,
getInferredProject,
reload: clearProjects,
dispose() {
clearProjects();
disposeWatch();
},
};
function clearProjects() {
const _projects = [
inferredProject,
...projects.values(),
];
_projects.forEach(async project => {
(await project)?.dispose();
});
inferredProject = undefined;
projects.clear();
}
async function getProjectAndTsConfig(uri: string) {
const tsconfig = await findMatchConfigs(URI.parse(uri));
if (tsconfig) {
const project = await getProjectByCreate(tsconfig);
return {
tsconfig: tsconfig,
project,
};
}
}
function getInferredProject() {
if (!inferredProject) {
inferredProject = createProject(
runtimeEnv,
languageConfigs,
fsHost,
sys,
ts,
options,
rootUri,
rootUri.fsPath,
inferOptions,
tsLocalized,
documents,
connection,
configHost,
);
}
return inferredProject;
}
async function findMatchConfigs(uri: URI) {
await prepareClosestootParsedCommandLine();
return await findDirectIncludeTsconfig() ?? await findIndirectReferenceTsconfig();
async function prepareClosestootParsedCommandLine() {
let matches: string[] = [];
for (const rootTsConfig of rootTsConfigs) {
if (shared.isFileInDir(uri.fsPath, path.dirname(rootTsConfig))) {
matches.push(rootTsConfig);
}
}
matches = matches.sort((a, b) => sortTsConfigs(uri.fsPath, a, b));
if (matches.length) {
await getParsedCommandLine(matches[0]);
}
}
function findDirectIncludeTsconfig() {
return findTsconfig(async tsconfig => {
const parsedCommandLine = await getParsedCommandLine(tsconfig);
// use toLowerCase to fix https://github.com/johnsoncodehk/volar/issues/1125
const fileNames = new Set(parsedCommandLine.fileNames.map(fileName => shared.normalizeFileName(fileName.toLowerCase())));
return fileNames.has(shared.normalizeFileName(uri.fsPath.toLowerCase()));
});
}
function findIndirectReferenceTsconfig() {
return findTsconfig(async tsconfig => {
const project = await projects.pathGet(tsconfig);
const ls = await project?.getLanguageServiceDontCreate();
const validDoc = ls?.__internal__.context.getTsLs().__internal__.getValidTextDocument(uri.toString());
return !!validDoc;
});
}
async function findTsconfig(match: (tsconfig: string) => Promise<boolean> | boolean) {
const checked = new Set<string>();
for (const rootTsConfig of [...rootTsConfigs].sort((a, b) => sortTsConfigs(uri.fsPath, a, b))) {
const project = await projects.pathGet(rootTsConfig);
if (project) {
const chains = await getReferencesChains(project.getParsedCommandLine(), rootTsConfig, []);
for (const chain of chains) {
for (let i = chain.length - 1; i >= 0; i--) {
const tsconfig = chain[i];
if (checked.has(tsconfig))
continue;
checked.add(tsconfig);
if (await match(tsconfig)) {
return tsconfig;
}
}
}
}
}
}
async function getReferencesChains(parsedCommandLine: ts.ParsedCommandLine, tsConfig: string, before: string[]) {
if (parsedCommandLine.projectReferences?.length) {
const newChains: string[][] = [];
for (const projectReference of parsedCommandLine.projectReferences) {
let tsConfigPath = projectReference.path;
// fix https://github.com/johnsoncodehk/volar/issues/712
if (!sys.fileExists(tsConfigPath)) {
const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
if (sys.fileExists(newTsConfigPath)) {
tsConfigPath = newTsConfigPath;
}
else if (sys.fileExists(newJsConfigPath)) {
tsConfigPath = newJsConfigPath;
}
}
const beforeIndex = before.indexOf(tsConfigPath); // cycle
if (beforeIndex >= 0) {
newChains.push(before.slice(0, Math.max(beforeIndex, 1)));
}
else {
const referenceParsedCommandLine = await getParsedCommandLine(tsConfigPath);
for (const chain of await getReferencesChains(referenceParsedCommandLine, tsConfigPath, [...before, tsConfig])) {
newChains.push(chain);
}
}
}
return newChains;
}
else {
return [[...before, tsConfig]];
}
}
async function getParsedCommandLine(tsConfig: string) {
const project = await getProjectByCreate(tsConfig);
return project.getParsedCommandLine();
}
}
function getProjectByCreate(tsConfig: string) {
let project = projects.pathGet(tsConfig);
if (!project) {
project = createProject(
runtimeEnv,
languageConfigs,
fsHost,
sys,
ts,
options,
rootUri,
path.dirname(tsConfig),
tsConfig,
tsLocalized,
documents,
connection,
configHost,
);
projects.pathSet(tsConfig, project);
}
return project;
}
}
export function sortTsConfigs(fsPath: string, a: string, b: string) {
const inA = shared.isFileInDir(fsPath, path.dirname(a));
const inB = shared.isFileInDir(fsPath, path.dirname(b));
if (inA !== inB) {
const aWeight = inA ? 1 : 0;
const bWeight = inB ? 1 : 0;
return bWeight - aWeight;
}
const aLength = a.split('/').length;
const bLength = b.split('/').length;
if (aLength === bLength) {
const aWeight = path.basename(a) === 'tsconfig.json' ? 1 : 0;
const bWeight = path.basename(b) === 'tsconfig.json' ? 1 : 0;
return bWeight - aWeight;
}
return bLength - aLength;
}