/
workspace.ts
253 lines (216 loc) · 7.97 KB
/
workspace.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
import * as shared from '@volar/shared';
import * as path from 'typesafe-path';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { createProject, Project } from './project';
import { getInferredCompilerOptions } from './utils/inferredCompilerOptions';
import { loadServerConfig } from './utils/serverConfig';
import { createUriMap } from './utils/uriMap';
import { WorkspacesContext } from './workspaces';
export const rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];
export interface WorkspaceContext {
workspaces: WorkspacesContext;
rootUri: URI,
}
export async function createWorkspace(context: WorkspaceContext) {
let inferredProject: Project | undefined;
const serverConfig = loadServerConfig(shared.getPathOfUri(context.rootUri.toString()), context.workspaces.initOptions.configFilePath);
const sys = context.workspaces.fileSystemHost.getWorkspaceFileSystem(context.rootUri);
const documentRegistry = context.workspaces.ts.createDocumentRegistry(sys.useCaseSensitiveFileNames, shared.getPathOfUri(context.rootUri.toString()));
const projects = createUriMap<Project>();
const rootTsConfigs = new Set(sys.readDirectory(shared.getPathOfUri(context.rootUri.toString()), rootTsConfigNames, undefined, ['**/*']).map(fileName => shared.normalizeFileName(fileName)));
const disposeTsConfigWatch = context.workspaces.fileSystemHost.onDidChangeWatchedFiles(({ changes }) => {
for (const change of changes) {
if (rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1))) {
if (change.type === vscode.FileChangeType.Created) {
if (shared.isFileInDir(shared.getPathOfUri(change.uri), shared.getPathOfUri(context.rootUri.toString()))) {
rootTsConfigs.add(shared.getPathOfUri(change.uri));
}
}
else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted) && projects.uriHas(change.uri)) {
if (change.type === vscode.FileChangeType.Deleted) {
rootTsConfigs.delete(shared.getPathOfUri(change.uri));
}
const project = projects.uriGet(change.uri);
projects.uriDelete(change.uri);
project?.then(project => project.dispose());
}
}
}
});
return {
projects,
documentRegistry,
getProjectAndTsConfig,
getInferredProject,
getInferredProjectDontCreate: () => inferredProject,
reload: clearProjects,
dispose() {
clearProjects();
disposeTsConfigWatch();
},
};
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 = (async () => {
const inferOptions = await getInferredCompilerOptions(context.workspaces.ts, context.workspaces.configurationHost);
return createProject({
workspace: context,
rootUri: context.rootUri,
tsConfig: inferOptions,
documentRegistry,
serverConfig,
});
})();
}
return inferredProject;
}
async function findMatchConfigs(uri: URI) {
await prepareClosestootParsedCommandLine();
return await findDirectIncludeTsconfig() ?? await findIndirectReferenceTsconfig();
async function prepareClosestootParsedCommandLine() {
let matches: path.PosixPath[] = [];
for (const rootTsConfig of rootTsConfigs) {
if (shared.isFileInDir(shared.getPathOfUri(uri.toString()), path.dirname(rootTsConfig))) {
matches.push(rootTsConfig);
}
}
matches = matches.sort((a, b) => sortTsConfigs(shared.getPathOfUri(uri.toString()), a, b));
if (matches.length) {
await getParsedCommandLine(matches[0]);
}
}
function findDirectIncludeTsconfig() {
return findTsconfig(async tsconfig => {
const map = createUriMap<boolean>();
const parsedCommandLine = await getParsedCommandLine(tsconfig);
for (const fileName of parsedCommandLine.fileNames) {
map.pathSet(fileName, true);
}
return map.uriHas(uri.toString());
});
}
function findIndirectReferenceTsconfig() {
return findTsconfig(async tsconfig => {
const project = await projects.pathGet(tsconfig);
const ls = project?.getLanguageServiceDontCreate();
const validDoc = ls?.context.typescriptLanguageService.getProgram()?.getSourceFile(shared.getPathOfUri(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(shared.getPathOfUri(uri.toString()), a, b))) {
const project = await projects.pathGet(rootTsConfig);
if (project) {
let chains = await getReferencesChains(project.getParsedCommandLine(), rootTsConfig, []);
if (context.workspaces.initOptions.reverseConfigFilePriority) {
chains = chains.reverse();
}
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 as path.OsPath, 'tsconfig.json' as path.PosixPath);
const newJsConfigPath = path.join(tsConfigPath as path.OsPath, 'jsconfig.json' as path.PosixPath);
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) {
const tsConfig = shared.normalizeFileName(_tsConfig);
let project = projects.pathGet(tsConfig);
if (!project) {
project = createProject({
workspace: context,
serverConfig,
rootUri: URI.parse(shared.getUriByPath(path.dirname(tsConfig))),
tsConfig,
documentRegistry,
});
projects.pathSet(tsConfig, project);
}
return project;
}
}
export function sortTsConfigs(file: path.PosixPath, a: path.PosixPath, b: path.PosixPath) {
const inA = shared.isFileInDir(file, path.dirname(a));
const inB = shared.isFileInDir(file, 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;
}