@volar/language-server 1.4.0-alpha.0 → 1.4.0-alpha.10

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,15 +1,7 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
1
+ "use strict";
10
2
  Object.defineProperty(exports, "__esModule", { value: true });
11
3
  exports.sortTsConfigs = exports.createWorkspace = exports.rootTsConfigNames = void 0;
12
- const path = require("typesafe-path");
4
+ const path = require("typesafe-path/posix");
13
5
  const vscode = require("vscode-languageserver");
14
6
  const vscode_uri_1 = require("vscode-uri");
15
7
  const project_1 = require("./project");
@@ -17,210 +9,206 @@ const inferredCompilerOptions_1 = require("./utils/inferredCompilerOptions");
17
9
  const uriMap_1 = require("./utils/uriMap");
18
10
  const isFileInDir_1 = require("./utils/isFileInDir");
19
11
  exports.rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];
20
- function createWorkspace(context) {
21
- var _a, _b, _c;
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const fileNameToUri = context.workspaces.server.runtimeEnv.fileNameToUri;
24
- const uriToFileName = context.workspaces.server.runtimeEnv.uriToFileName;
25
- let inferredProject;
26
- const sys = (_a = context.workspaces.fileSystemHost) === null || _a === void 0 ? void 0 : _a.getWorkspaceFileSystem(context.rootUri);
27
- const documentRegistry = sys ? (_b = context.workspaces.ts) === null || _b === void 0 ? void 0 : _b.createDocumentRegistry(sys.useCaseSensitiveFileNames, uriToFileName(context.rootUri.toString())) : undefined;
28
- const projects = (0, uriMap_1.createUriMap)(fileNameToUri);
29
- const rootTsConfigs = new Set(sys === null || sys === void 0 ? void 0 : sys.readDirectory(uriToFileName(context.rootUri.toString()), exports.rootTsConfigNames, undefined, ['**/*']).map(fileName => fileName.replace(/\\/g, '/')));
30
- const disposeTsConfigWatch = (_c = context.workspaces.fileSystemHost) === null || _c === void 0 ? void 0 : _c.onDidChangeWatchedFiles(({ changes }) => {
31
- for (const change of changes) {
32
- if (exports.rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1))) {
33
- if (change.type === vscode.FileChangeType.Created) {
34
- if ((0, isFileInDir_1.isFileInDir)(uriToFileName(change.uri), uriToFileName(context.rootUri.toString()))) {
35
- rootTsConfigs.add(uriToFileName(change.uri));
36
- }
12
+ async function createWorkspace(context) {
13
+ const fileNameToUri = context.workspaces.server.runtimeEnv.fileNameToUri;
14
+ const uriToFileName = context.workspaces.server.runtimeEnv.uriToFileName;
15
+ let inferredProject;
16
+ const sys = context.workspaces.fileSystemHost?.getWorkspaceFileSystem(context.rootUri);
17
+ const documentRegistry = sys ? context.workspaces.ts?.createDocumentRegistry(sys.useCaseSensitiveFileNames, uriToFileName(context.rootUri.toString())) : undefined;
18
+ const projects = (0, uriMap_1.createUriMap)(fileNameToUri);
19
+ const rootTsConfigs = new Set();
20
+ const searchedDirs = new Set();
21
+ const disposeTsConfigWatch = context.workspaces.fileSystemHost?.onDidChangeWatchedFiles(({ changes }) => {
22
+ for (const change of changes) {
23
+ if (exports.rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1))) {
24
+ if (change.type === vscode.FileChangeType.Created) {
25
+ if ((0, isFileInDir_1.isFileInDir)(uriToFileName(change.uri), uriToFileName(context.rootUri.toString()))) {
26
+ rootTsConfigs.add(uriToFileName(change.uri));
37
27
  }
38
- else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted) && projects.uriHas(change.uri)) {
39
- if (change.type === vscode.FileChangeType.Deleted) {
40
- rootTsConfigs.delete(uriToFileName(change.uri));
41
- }
42
- const project = projects.uriGet(change.uri);
43
- projects.uriDelete(change.uri);
44
- project === null || project === void 0 ? void 0 : project.then(project => project.dispose());
28
+ }
29
+ else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted) && projects.uriHas(change.uri)) {
30
+ if (change.type === vscode.FileChangeType.Deleted) {
31
+ rootTsConfigs.delete(uriToFileName(change.uri));
45
32
  }
33
+ const project = projects.uriGet(change.uri);
34
+ projects.uriDelete(change.uri);
35
+ project?.then(project => project.dispose());
46
36
  }
47
37
  }
38
+ }
39
+ });
40
+ return {
41
+ projects,
42
+ documentRegistry,
43
+ getProjectAndTsConfig,
44
+ getInferredProject,
45
+ getInferredProjectDontCreate: () => inferredProject,
46
+ reload: clearProjects,
47
+ dispose() {
48
+ clearProjects();
49
+ disposeTsConfigWatch?.();
50
+ },
51
+ };
52
+ function clearProjects() {
53
+ const _projects = [
54
+ inferredProject,
55
+ ...projects.values(),
56
+ ];
57
+ _projects.forEach(async (project) => {
58
+ (await project)?.dispose();
48
59
  });
49
- return {
50
- projects,
51
- documentRegistry,
52
- getProjectAndTsConfig,
53
- getInferredProject,
54
- getInferredProjectDontCreate: () => inferredProject,
55
- reload: clearProjects,
56
- dispose() {
57
- clearProjects();
58
- disposeTsConfigWatch === null || disposeTsConfigWatch === void 0 ? void 0 : disposeTsConfigWatch();
59
- },
60
- };
61
- function clearProjects() {
62
- const _projects = [
63
- inferredProject,
64
- ...projects.values(),
65
- ];
66
- _projects.forEach((project) => __awaiter(this, void 0, void 0, function* () {
67
- var _a;
68
- (_a = (yield project)) === null || _a === void 0 ? void 0 : _a.dispose();
69
- }));
70
- inferredProject = undefined;
71
- projects.clear();
60
+ inferredProject = undefined;
61
+ projects.clear();
62
+ }
63
+ async function getProjectAndTsConfig(uri) {
64
+ const tsconfig = await findMatchConfigs(vscode_uri_1.URI.parse(uri));
65
+ if (tsconfig) {
66
+ const project = await getProjectByCreate(tsconfig);
67
+ return {
68
+ tsconfig: tsconfig,
69
+ project,
70
+ };
72
71
  }
73
- function getProjectAndTsConfig(uri) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const tsconfig = yield findMatchConfigs(vscode_uri_1.URI.parse(uri));
76
- if (tsconfig) {
77
- const project = yield getProjectByCreate(tsconfig);
78
- return {
79
- tsconfig: tsconfig,
80
- project,
81
- };
72
+ }
73
+ function getInferredProject() {
74
+ if (!inferredProject) {
75
+ inferredProject = (async () => {
76
+ const inferOptions = await (0, inferredCompilerOptions_1.getInferredCompilerOptions)(context.workspaces.configurationHost);
77
+ return (0, project_1.createProject)({
78
+ workspace: context,
79
+ rootUri: context.rootUri,
80
+ tsConfig: inferOptions,
81
+ documentRegistry,
82
+ });
83
+ })();
84
+ }
85
+ return inferredProject;
86
+ }
87
+ async function findMatchConfigs(uri) {
88
+ const filePath = uriToFileName(uri.toString());
89
+ let dir = path.dirname(filePath);
90
+ while (true) {
91
+ if (searchedDirs.has(dir)) {
92
+ break;
93
+ }
94
+ searchedDirs.add(dir);
95
+ for (const tsConfigName of exports.rootTsConfigNames) {
96
+ const tsconfigPath = path.join(dir, tsConfigName);
97
+ if (sys?.fileExists(tsconfigPath)) {
98
+ rootTsConfigs.add(tsconfigPath);
82
99
  }
83
- });
100
+ }
101
+ dir = path.dirname(dir);
84
102
  }
85
- function getInferredProject() {
86
- if (!inferredProject) {
87
- inferredProject = (() => __awaiter(this, void 0, void 0, function* () {
88
- const inferOptions = yield (0, inferredCompilerOptions_1.getInferredCompilerOptions)(context.workspaces.configurationHost);
89
- return (0, project_1.createProject)({
90
- workspace: context,
91
- rootUri: context.rootUri,
92
- tsConfig: inferOptions,
93
- documentRegistry,
94
- });
95
- }))();
103
+ await prepareClosestootParsedCommandLine();
104
+ return await findDirectIncludeTsconfig() ?? await findIndirectReferenceTsconfig();
105
+ async function prepareClosestootParsedCommandLine() {
106
+ let matches = [];
107
+ for (const rootTsConfig of rootTsConfigs) {
108
+ if ((0, isFileInDir_1.isFileInDir)(uriToFileName(uri.toString()), path.dirname(rootTsConfig))) {
109
+ matches.push(rootTsConfig);
110
+ }
111
+ }
112
+ matches = matches.sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b));
113
+ if (matches.length) {
114
+ await getParsedCommandLine(matches[0]);
96
115
  }
97
- return inferredProject;
98
116
  }
99
- function findMatchConfigs(uri) {
100
- var _a;
101
- return __awaiter(this, void 0, void 0, function* () {
102
- yield prepareClosestootParsedCommandLine();
103
- return (_a = yield findDirectIncludeTsconfig()) !== null && _a !== void 0 ? _a : yield findIndirectReferenceTsconfig();
104
- function prepareClosestootParsedCommandLine() {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- let matches = [];
107
- for (const rootTsConfig of rootTsConfigs) {
108
- if ((0, isFileInDir_1.isFileInDir)(uriToFileName(uri.toString()), path.dirname(rootTsConfig))) {
109
- matches.push(rootTsConfig);
117
+ function findDirectIncludeTsconfig() {
118
+ return findTsconfig(async (tsconfig) => {
119
+ const map = (0, uriMap_1.createUriMap)(fileNameToUri);
120
+ const parsedCommandLine = await getParsedCommandLine(tsconfig);
121
+ for (const fileName of parsedCommandLine?.fileNames ?? []) {
122
+ map.pathSet(fileName, true);
123
+ }
124
+ return map.uriHas(uri.toString());
125
+ });
126
+ }
127
+ function findIndirectReferenceTsconfig() {
128
+ return findTsconfig(async (tsconfig) => {
129
+ const project = await projects.pathGet(tsconfig);
130
+ const ls = project?.getLanguageServiceDontCreate();
131
+ const validDoc = ls?.context.typescript?.languageService.getProgram()?.getSourceFile(uriToFileName(uri.toString()));
132
+ return !!validDoc;
133
+ });
134
+ }
135
+ async function findTsconfig(match) {
136
+ const checked = new Set();
137
+ for (const rootTsConfig of [...rootTsConfigs].sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b))) {
138
+ const project = await projects.pathGet(rootTsConfig);
139
+ if (project) {
140
+ let chains = await getReferencesChains(project.getParsedCommandLine(), rootTsConfig, []);
141
+ if (context.workspaces.initOptions.reverseConfigFilePriority) {
142
+ chains = chains.reverse();
143
+ }
144
+ for (const chain of chains) {
145
+ for (let i = chain.length - 1; i >= 0; i--) {
146
+ const tsconfig = chain[i];
147
+ if (checked.has(tsconfig))
148
+ continue;
149
+ checked.add(tsconfig);
150
+ if (await match(tsconfig)) {
151
+ return tsconfig;
110
152
  }
111
153
  }
112
- matches = matches.sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b));
113
- if (matches.length) {
114
- yield getParsedCommandLine(matches[0]);
115
- }
116
- });
154
+ }
117
155
  }
118
- function findDirectIncludeTsconfig() {
119
- return findTsconfig((tsconfig) => __awaiter(this, void 0, void 0, function* () {
120
- var _a;
121
- const map = (0, uriMap_1.createUriMap)(fileNameToUri);
122
- const parsedCommandLine = yield getParsedCommandLine(tsconfig);
123
- for (const fileName of (_a = parsedCommandLine === null || parsedCommandLine === void 0 ? void 0 : parsedCommandLine.fileNames) !== null && _a !== void 0 ? _a : []) {
124
- map.pathSet(fileName, true);
156
+ }
157
+ }
158
+ async function getReferencesChains(parsedCommandLine, tsConfig, before) {
159
+ if (parsedCommandLine.projectReferences?.length) {
160
+ const newChains = [];
161
+ for (const projectReference of parsedCommandLine.projectReferences) {
162
+ let tsConfigPath = projectReference.path.replace(/\\/g, '/');
163
+ // fix https://github.com/johnsoncodehk/volar/issues/712
164
+ if (sys && !sys.fileExists(tsConfigPath)) {
165
+ const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
166
+ const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
167
+ if (sys.fileExists(newTsConfigPath)) {
168
+ tsConfigPath = newTsConfigPath;
125
169
  }
126
- return map.uriHas(uri.toString());
127
- }));
128
- }
129
- function findIndirectReferenceTsconfig() {
130
- return findTsconfig((tsconfig) => __awaiter(this, void 0, void 0, function* () {
131
- var _a, _b;
132
- const project = yield projects.pathGet(tsconfig);
133
- const ls = project === null || project === void 0 ? void 0 : project.getLanguageServiceDontCreate();
134
- const validDoc = (_b = (_a = ls === null || ls === void 0 ? void 0 : ls.context.typescript) === null || _a === void 0 ? void 0 : _a.languageService.getProgram()) === null || _b === void 0 ? void 0 : _b.getSourceFile(uriToFileName(uri.toString()));
135
- return !!validDoc;
136
- }));
137
- }
138
- function findTsconfig(match) {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- const checked = new Set();
141
- for (const rootTsConfig of [...rootTsConfigs].sort((a, b) => sortTsConfigs(uriToFileName(uri.toString()), a, b))) {
142
- const project = yield projects.pathGet(rootTsConfig);
143
- if (project) {
144
- let chains = yield getReferencesChains(project.getParsedCommandLine(), rootTsConfig, []);
145
- if (context.workspaces.initOptions.reverseConfigFilePriority) {
146
- chains = chains.reverse();
147
- }
148
- for (const chain of chains) {
149
- for (let i = chain.length - 1; i >= 0; i--) {
150
- const tsconfig = chain[i];
151
- if (checked.has(tsconfig))
152
- continue;
153
- checked.add(tsconfig);
154
- if (yield match(tsconfig)) {
155
- return tsconfig;
156
- }
157
- }
158
- }
159
- }
170
+ else if (sys.fileExists(newJsConfigPath)) {
171
+ tsConfigPath = newJsConfigPath;
160
172
  }
161
- });
162
- }
163
- function getReferencesChains(parsedCommandLine, tsConfig, before) {
164
- var _a;
165
- return __awaiter(this, void 0, void 0, function* () {
166
- if ((_a = parsedCommandLine.projectReferences) === null || _a === void 0 ? void 0 : _a.length) {
167
- const newChains = [];
168
- for (const projectReference of parsedCommandLine.projectReferences) {
169
- let tsConfigPath = projectReference.path;
170
- // fix https://github.com/johnsoncodehk/volar/issues/712
171
- if (sys && !sys.fileExists(tsConfigPath)) {
172
- const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
173
- const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
174
- if (sys.fileExists(newTsConfigPath)) {
175
- tsConfigPath = newTsConfigPath;
176
- }
177
- else if (sys.fileExists(newJsConfigPath)) {
178
- tsConfigPath = newJsConfigPath;
179
- }
180
- }
181
- const beforeIndex = before.indexOf(tsConfigPath); // cycle
182
- if (beforeIndex >= 0) {
183
- newChains.push(before.slice(0, Math.max(beforeIndex, 1)));
184
- }
185
- else {
186
- const referenceParsedCommandLine = yield getParsedCommandLine(tsConfigPath);
187
- if (referenceParsedCommandLine) {
188
- for (const chain of yield getReferencesChains(referenceParsedCommandLine, tsConfigPath, [...before, tsConfig])) {
189
- newChains.push(chain);
190
- }
191
- }
192
- }
173
+ }
174
+ const beforeIndex = before.indexOf(tsConfigPath); // cycle
175
+ if (beforeIndex >= 0) {
176
+ newChains.push(before.slice(0, Math.max(beforeIndex, 1)));
177
+ }
178
+ else {
179
+ const referenceParsedCommandLine = await getParsedCommandLine(tsConfigPath);
180
+ if (referenceParsedCommandLine) {
181
+ for (const chain of await getReferencesChains(referenceParsedCommandLine, tsConfigPath, [...before, tsConfig])) {
182
+ newChains.push(chain);
193
183
  }
194
- return newChains;
195
184
  }
196
- else {
197
- return [[...before, tsConfig]];
198
- }
199
- });
200
- }
201
- function getParsedCommandLine(tsConfig) {
202
- return __awaiter(this, void 0, void 0, function* () {
203
- const project = yield getProjectByCreate(tsConfig);
204
- return project === null || project === void 0 ? void 0 : project.getParsedCommandLine();
205
- });
185
+ }
206
186
  }
207
- });
208
- }
209
- function getProjectByCreate(_tsConfig) {
210
- const tsConfig = _tsConfig.replace(/\\/g, '/');
211
- let project = projects.pathGet(tsConfig);
212
- if (!project && documentRegistry) {
213
- project = (0, project_1.createProject)({
214
- workspace: context,
215
- rootUri: vscode_uri_1.URI.parse(fileNameToUri(path.dirname(tsConfig))),
216
- tsConfig,
217
- documentRegistry,
218
- });
219
- projects.pathSet(tsConfig, project);
187
+ return newChains;
188
+ }
189
+ else {
190
+ return [[...before, tsConfig]];
220
191
  }
221
- return project;
222
192
  }
223
- });
193
+ async function getParsedCommandLine(tsConfig) {
194
+ const project = await getProjectByCreate(tsConfig);
195
+ return project?.getParsedCommandLine();
196
+ }
197
+ }
198
+ function getProjectByCreate(_tsConfig) {
199
+ const tsConfig = _tsConfig.replace(/\\/g, '/');
200
+ let project = projects.pathGet(tsConfig);
201
+ if (!project && documentRegistry) {
202
+ project = (0, project_1.createProject)({
203
+ workspace: context,
204
+ rootUri: vscode_uri_1.URI.parse(fileNameToUri(path.dirname(tsConfig))),
205
+ tsConfig,
206
+ documentRegistry,
207
+ });
208
+ projects.pathSet(tsConfig, project);
209
+ }
210
+ return project;
211
+ }
224
212
  }
225
213
  exports.createWorkspace = createWorkspace;
226
214
  function sortTsConfigs(file, a, b) {