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