@volar/language-server 1.9.2 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sleep = exports.createWorkspaces = void 0;
3
+ exports.sortTsConfigs = exports.sleep = exports.createWorkspaces = exports.rootTsConfigNames = void 0;
4
4
  const vscode = require("vscode-languageserver");
5
+ const vscode_uri_1 = require("vscode-uri");
5
6
  const types_1 = require("../types");
6
- const workspace_1 = require("./workspace");
7
7
  const isFileInDir_1 = require("./utils/isFileInDir");
8
- function createWorkspaces(context) {
9
- const { uriToFileName } = context.server.runtimeEnv;
10
- const workspaces = new Map();
8
+ const path = require("typesafe-path/posix");
9
+ const project_1 = require("./project");
10
+ const inferredCompilerOptions_1 = require("./utils/inferredCompilerOptions");
11
+ const uriMap_1 = require("./utils/uriMap");
12
+ const language_service_1 = require("@volar/language-service");
13
+ exports.rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];
14
+ function createWorkspaces(context, rootUris) {
15
+ const { fileNameToUri, uriToFileName, fs } = context.server.runtimeEnv;
16
+ const configProjects = (0, uriMap_1.createUriMap)(fileNameToUri);
17
+ const inferredProjects = (0, uriMap_1.createUriMap)(fileNameToUri);
18
+ const rootTsConfigs = new Set();
19
+ const searchedDirs = new Set();
11
20
  let semanticTokensReq = 0;
12
21
  let documentUpdatedReq = 0;
13
22
  context.workspaces.documents.onDidChangeContent(({ textDocument }) => {
@@ -17,7 +26,7 @@ function createWorkspaces(context) {
17
26
  context.server.connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
18
27
  });
19
28
  context.server.onDidChangeWatchedFiles(({ changes }) => {
20
- const tsConfigChanges = changes.filter(change => workspace_1.rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1)));
29
+ const tsConfigChanges = changes.filter(change => exports.rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1)));
21
30
  if (tsConfigChanges.length) {
22
31
  reloadDiagnostics();
23
32
  }
@@ -25,33 +34,53 @@ function createWorkspaces(context) {
25
34
  updateDiagnosticsAndSemanticTokens();
26
35
  }
27
36
  });
37
+ context.server.onDidChangeWatchedFiles(({ changes }) => {
38
+ for (const change of changes) {
39
+ if (exports.rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1))) {
40
+ if (change.type === vscode.FileChangeType.Created) {
41
+ rootTsConfigs.add(uriToFileName(change.uri));
42
+ }
43
+ else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted) && configProjects.uriHas(change.uri)) {
44
+ if (change.type === vscode.FileChangeType.Deleted) {
45
+ rootTsConfigs.delete(uriToFileName(change.uri));
46
+ }
47
+ const project = configProjects.uriGet(change.uri);
48
+ configProjects.uriDelete(change.uri);
49
+ project?.then(project => project.dispose());
50
+ }
51
+ }
52
+ }
53
+ });
28
54
  context.server.configurationHost?.onDidChangeConfiguration?.(updateDiagnosticsAndSemanticTokens);
29
55
  return {
30
- workspaces,
56
+ configProjects,
57
+ inferredProjects,
31
58
  getProject: getProjectAndTsConfig,
32
- reloadProject,
59
+ reloadProjects: reloadProject,
33
60
  add: (rootUri) => {
34
- if (!workspaces.has(rootUri.toString())) {
35
- workspaces.set(rootUri.toString(), (0, workspace_1.createWorkspace)({
36
- ...context,
37
- workspace: {
38
- rootUri,
39
- },
40
- }));
61
+ if (!rootUris.some(uri => uri.toString() === rootUri.toString())) {
62
+ rootUris.push(rootUri);
41
63
  }
42
64
  },
43
65
  remove: (rootUri) => {
44
- const _workspace = workspaces.get(rootUri.toString());
45
- workspaces.delete(rootUri.toString());
46
- (async () => {
47
- (await _workspace)?.dispose();
48
- })();
66
+ rootUris = rootUris.filter(uri => uri.toString() !== rootUri.toString());
67
+ for (const tsconfig of configProjects.keys()) {
68
+ const project = configProjects.pathGet(tsconfig);
69
+ project.then(project => {
70
+ if (project.context.project.workspaceUri.toString() === rootUri.toString()) {
71
+ configProjects.uriDelete(tsconfig);
72
+ project.dispose();
73
+ }
74
+ });
75
+ }
49
76
  },
50
77
  };
51
78
  async function reloadProject() {
52
- for (const [_, workspace] of workspaces) {
53
- (await workspace).reload();
79
+ for (const project of [...configProjects.values(), ...inferredProjects.values()]) {
80
+ project.then(project => project.dispose());
54
81
  }
82
+ configProjects.clear();
83
+ inferredProjects.clear();
55
84
  reloadDiagnostics();
56
85
  }
57
86
  function reloadDiagnostics() {
@@ -125,34 +154,198 @@ function createWorkspaces(context) {
125
154
  context.server.connection.sendDiagnostics({ uri: uri, diagnostics: errors, version });
126
155
  }
127
156
  async function getProjectAndTsConfig(uri) {
128
- let rootUris = [...workspaces.keys()]
129
- .filter(rootUri => (0, isFileInDir_1.isFileInDir)(uriToFileName(uri), uriToFileName(rootUri)))
130
- .sort((a, b) => (0, workspace_1.sortTsConfigs)(uriToFileName(uri), uriToFileName(a), uriToFileName(b)));
131
157
  if (context.workspaces.initOptions.serverMode !== types_1.ServerMode.Syntactic) {
132
- for (const rootUri of rootUris) {
133
- const workspace = await workspaces.get(rootUri);
134
- const projectAndTsConfig = await workspace?.getProjectAndTsConfig(uri);
135
- if (projectAndTsConfig) {
136
- return projectAndTsConfig;
158
+ const tsconfig = await findMatchConfigs(vscode_uri_1.URI.parse(uri));
159
+ if (tsconfig) {
160
+ const project = await getProjectByCreate(tsconfig);
161
+ return {
162
+ tsconfig: tsconfig,
163
+ project,
164
+ };
165
+ }
166
+ }
167
+ const workspaceUri = getWorkspaceUri(vscode_uri_1.URI.parse(uri));
168
+ if (!inferredProjects.uriHas(workspaceUri.toString())) {
169
+ inferredProjects.uriSet(workspaceUri.toString(), (async () => {
170
+ const inferOptions = await (0, inferredCompilerOptions_1.getInferredCompilerOptions)(context.server.configurationHost);
171
+ return (0, project_1.createProject)({
172
+ ...context,
173
+ project: {
174
+ workspaceUri,
175
+ rootUri: workspaceUri,
176
+ tsConfig: inferOptions,
177
+ },
178
+ });
179
+ })());
180
+ }
181
+ const project = await inferredProjects.uriGet(workspaceUri.toString());
182
+ project.tryAddFile(uriToFileName(uri));
183
+ return {
184
+ tsconfig: undefined,
185
+ project,
186
+ };
187
+ }
188
+ function getWorkspaceUri(uri) {
189
+ const fileName = uriToFileName(uri.toString());
190
+ let _rootUris = [...rootUris]
191
+ .filter(rootUri => (0, isFileInDir_1.isFileInDir)(fileName, uriToFileName(rootUri.toString())))
192
+ .sort((a, b) => sortTsConfigs(fileName, uriToFileName(a.toString()), uriToFileName(b.toString())));
193
+ if (!_rootUris.length) {
194
+ _rootUris = [...rootUris];
195
+ }
196
+ if (!_rootUris.length) {
197
+ _rootUris = [uri.with({ path: '/' })];
198
+ }
199
+ return _rootUris[0];
200
+ }
201
+ async function findMatchConfigs(uri) {
202
+ const filePath = uriToFileName(uri.toString());
203
+ let dir = path.dirname(filePath);
204
+ while (true) {
205
+ if (searchedDirs.has(dir)) {
206
+ break;
207
+ }
208
+ searchedDirs.add(dir);
209
+ for (const tsConfigName of exports.rootTsConfigNames) {
210
+ const tsconfigPath = path.join(dir, tsConfigName);
211
+ if ((await fs.stat?.(fileNameToUri(tsconfigPath)))?.type === language_service_1.FileType.File) {
212
+ rootTsConfigs.add(tsconfigPath);
137
213
  }
138
214
  }
215
+ dir = path.dirname(dir);
216
+ }
217
+ await prepareClosestootParsedCommandLine();
218
+ return await findDirectIncludeTsconfig() ?? await findIndirectReferenceTsconfig();
219
+ async function prepareClosestootParsedCommandLine() {
220
+ let matches = [];
221
+ for (const rootTsConfig of rootTsConfigs) {
222
+ if ((0, isFileInDir_1.isFileInDir)(uriToFileName(uri.toString()), path.dirname(rootTsConfig))) {
223
+ matches.push(rootTsConfig);
224
+ }
225
+ }
226
+ matches = matches.sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b));
227
+ if (matches.length) {
228
+ await getParsedCommandLine(matches[0]);
229
+ }
139
230
  }
140
- if (!rootUris.length) {
141
- rootUris = [...workspaces.keys()];
231
+ function findIndirectReferenceTsconfig() {
232
+ return findTsconfig(async (tsconfig) => {
233
+ const project = await configProjects.pathGet(tsconfig);
234
+ return project?.askedFiles.uriHas(uri.toString()) ?? false;
235
+ });
142
236
  }
143
- if (rootUris.length) {
144
- const project = await (await workspaces.get(rootUris[0]))?.getInferredProject();
145
- project?.tryAddFile(uriToFileName(uri));
146
- return {
147
- tsconfig: undefined,
148
- project,
149
- };
237
+ function findDirectIncludeTsconfig() {
238
+ return findTsconfig(async (tsconfig) => {
239
+ const map = (0, uriMap_1.createUriMap)(fileNameToUri);
240
+ const parsedCommandLine = await getParsedCommandLine(tsconfig);
241
+ for (const fileName of parsedCommandLine?.fileNames ?? []) {
242
+ map.pathSet(fileName, true);
243
+ }
244
+ return map.uriHas(uri.toString());
245
+ });
246
+ }
247
+ async function findTsconfig(match) {
248
+ const checked = new Set();
249
+ for (const rootTsConfig of [...rootTsConfigs].sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b))) {
250
+ const project = await configProjects.pathGet(rootTsConfig);
251
+ if (project) {
252
+ let chains = await getReferencesChains(project.getParsedCommandLine(), rootTsConfig, []);
253
+ if (context.workspaces.initOptions.reverseConfigFilePriority) {
254
+ chains = chains.reverse();
255
+ }
256
+ for (const chain of chains) {
257
+ for (let i = chain.length - 1; i >= 0; i--) {
258
+ const tsconfig = chain[i];
259
+ if (checked.has(tsconfig))
260
+ continue;
261
+ checked.add(tsconfig);
262
+ if (await match(tsconfig)) {
263
+ return tsconfig;
264
+ }
265
+ }
266
+ }
267
+ }
268
+ }
269
+ }
270
+ async function getReferencesChains(parsedCommandLine, tsConfig, before) {
271
+ if (parsedCommandLine.projectReferences?.length) {
272
+ const newChains = [];
273
+ for (const projectReference of parsedCommandLine.projectReferences) {
274
+ let tsConfigPath = projectReference.path.replace(/\\/g, '/');
275
+ // fix https://github.com/johnsoncodehk/volar/issues/712
276
+ if ((await fs.stat?.(fileNameToUri(tsConfigPath)))?.type === language_service_1.FileType.File) {
277
+ const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
278
+ const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
279
+ if ((await fs.stat?.(fileNameToUri(newTsConfigPath)))?.type === language_service_1.FileType.File) {
280
+ tsConfigPath = newTsConfigPath;
281
+ }
282
+ else if ((await fs.stat?.(fileNameToUri(newJsConfigPath)))?.type === language_service_1.FileType.File) {
283
+ tsConfigPath = newJsConfigPath;
284
+ }
285
+ }
286
+ const beforeIndex = before.indexOf(tsConfigPath); // cycle
287
+ if (beforeIndex >= 0) {
288
+ newChains.push(before.slice(0, Math.max(beforeIndex, 1)));
289
+ }
290
+ else {
291
+ const referenceParsedCommandLine = await getParsedCommandLine(tsConfigPath);
292
+ if (referenceParsedCommandLine) {
293
+ for (const chain of await getReferencesChains(referenceParsedCommandLine, tsConfigPath, [...before, tsConfig])) {
294
+ newChains.push(chain);
295
+ }
296
+ }
297
+ }
298
+ }
299
+ return newChains;
300
+ }
301
+ else {
302
+ return [[...before, tsConfig]];
303
+ }
304
+ }
305
+ async function getParsedCommandLine(tsConfig) {
306
+ const project = await getProjectByCreate(tsConfig);
307
+ return project?.getParsedCommandLine();
150
308
  }
151
309
  }
310
+ function getProjectByCreate(_tsConfig) {
311
+ const tsConfig = _tsConfig.replace(/\\/g, '/');
312
+ let project = configProjects.pathGet(tsConfig);
313
+ if (!project) {
314
+ const rootUri = vscode_uri_1.URI.parse(fileNameToUri(path.dirname(tsConfig)));
315
+ project = (0, project_1.createProject)({
316
+ ...context,
317
+ project: {
318
+ workspaceUri: getWorkspaceUri(rootUri),
319
+ rootUri: rootUri,
320
+ tsConfig,
321
+ },
322
+ });
323
+ configProjects.pathSet(tsConfig, project);
324
+ }
325
+ return project;
326
+ }
152
327
  }
153
328
  exports.createWorkspaces = createWorkspaces;
154
329
  function sleep(ms) {
155
330
  return new Promise(resolve => setTimeout(resolve, ms));
156
331
  }
157
332
  exports.sleep = sleep;
333
+ function sortTsConfigs(file, a, b) {
334
+ const inA = (0, isFileInDir_1.isFileInDir)(file, path.dirname(a));
335
+ const inB = (0, isFileInDir_1.isFileInDir)(file, path.dirname(b));
336
+ if (inA !== inB) {
337
+ const aWeight = inA ? 1 : 0;
338
+ const bWeight = inB ? 1 : 0;
339
+ return bWeight - aWeight;
340
+ }
341
+ const aLength = a.split('/').length;
342
+ const bLength = b.split('/').length;
343
+ if (aLength === bLength) {
344
+ const aWeight = path.basename(a) === 'tsconfig.json' ? 1 : 0;
345
+ const bWeight = path.basename(b) === 'tsconfig.json' ? 1 : 0;
346
+ return bWeight - aWeight;
347
+ }
348
+ return bLength - aLength;
349
+ }
350
+ exports.sortTsConfigs = sortTsConfigs;
158
351
  //# sourceMappingURL=workspaces.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-server",
3
- "version": "1.9.2",
3
+ "version": "1.10.1",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -15,9 +15,9 @@
15
15
  "directory": "packages/language-server"
16
16
  },
17
17
  "dependencies": {
18
- "@volar/language-core": "1.9.2",
19
- "@volar/language-service": "1.9.2",
20
- "@volar/typescript": "1.9.2",
18
+ "@volar/language-core": "1.10.1",
19
+ "@volar/language-service": "1.10.1",
20
+ "@volar/typescript": "1.10.1",
21
21
  "@vscode/l10n": "^0.0.11",
22
22
  "request-light": "^0.7.0",
23
23
  "typesafe-path": "^0.2.2",
@@ -27,7 +27,7 @@
27
27
  "vscode-uri": "^3.0.7"
28
28
  },
29
29
  "devDependencies": {
30
- "@volar/source-map": "1.9.2"
30
+ "@volar/source-map": "1.10.1"
31
31
  },
32
- "gitHead": "c838ba22be287308e3fcf27f65af672749816df4"
32
+ "gitHead": "65775648936d786847511f3d8bdd8a3d1dca2e4e"
33
33
  }