@volar/language-server 2.4.30 → 3.0.0-alpha.0

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.
package/protocol.js CHANGED
@@ -10,12 +10,34 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
13
35
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
36
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
37
  };
16
38
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.GetVirtualCodeRequest = exports.GetVirtualFileRequest = exports.GetServicePluginsRequest = exports.UpdateServicePluginStateNotification = exports.UpdateVirtualCodeStateNotification = exports.DocumentDrop_DataTransferItemFileDataRequest = exports.DocumentDrop_DataTransferItemAsStringRequest = exports.DocumentDropRequest = exports.ReloadProjectNotification = exports.AutoInsertRequest = exports.GetMatchTsConfigRequest = exports.FindFileReferenceRequest = void 0;
18
- const protocol = require("vscode-languageserver-protocol");
39
+ exports.GetVirtualCodeRequest = exports.GetVirtualFileRequest = exports.GetServicePluginsRequest = exports.UpdateServicePluginStateNotification = exports.UpdateVirtualCodeStateNotification = exports.DocumentDrop_DataTransferItemFileDataRequest = exports.DocumentDrop_DataTransferItemAsStringRequest = exports.DocumentDropRequest = exports.AutoInsertRequest = exports.FindFileReferenceRequest = void 0;
40
+ const protocol = __importStar(require("vscode-languageserver-protocol"));
19
41
  __exportStar(require("vscode-languageserver-protocol"), exports);
20
42
  /**
21
43
  * Client request server
@@ -24,18 +46,10 @@ var FindFileReferenceRequest;
24
46
  (function (FindFileReferenceRequest) {
25
47
  FindFileReferenceRequest.type = new protocol.RequestType('volar/client/findFileReference');
26
48
  })(FindFileReferenceRequest || (exports.FindFileReferenceRequest = FindFileReferenceRequest = {}));
27
- var GetMatchTsConfigRequest;
28
- (function (GetMatchTsConfigRequest) {
29
- GetMatchTsConfigRequest.type = new protocol.RequestType('volar/client/tsconfig');
30
- })(GetMatchTsConfigRequest || (exports.GetMatchTsConfigRequest = GetMatchTsConfigRequest = {}));
31
49
  var AutoInsertRequest;
32
50
  (function (AutoInsertRequest) {
33
51
  AutoInsertRequest.type = new protocol.RequestType('volar/client/autoInsert');
34
52
  })(AutoInsertRequest || (exports.AutoInsertRequest = AutoInsertRequest = {}));
35
- var ReloadProjectNotification;
36
- (function (ReloadProjectNotification) {
37
- ReloadProjectNotification.type = new protocol.NotificationType('volar/client/reloadProject');
38
- })(ReloadProjectNotification || (exports.ReloadProjectNotification = ReloadProjectNotification = {}));
39
53
  /**
40
54
  * Document Drop
41
55
  */
@@ -1,3 +0,0 @@
1
- import type * as ts from 'typescript';
2
- import type { LanguageServer } from '../types';
3
- export declare function getInferredCompilerOptions(server: LanguageServer): Promise<ts.CompilerOptions>;
@@ -1,65 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getInferredCompilerOptions = getInferredCompilerOptions;
4
- async function getInferredCompilerOptions(server) {
5
- const [implicitProjectConfig_1 = {}, implicitProjectConfig_2 = {},] = await Promise.all([
6
- server.configurations.get('js/ts.implicitProjectConfig'),
7
- server.configurations.get('javascript.implicitProjectConfig'),
8
- ]);
9
- const checkJs = readCheckJs();
10
- const experimentalDecorators = readExperimentalDecorators();
11
- const strictNullChecks = readImplicitStrictNullChecks();
12
- const strictFunctionTypes = readImplicitStrictFunctionTypes();
13
- const options = {
14
- ...inferredProjectCompilerOptions('typescript'),
15
- allowJs: true,
16
- allowSyntheticDefaultImports: true,
17
- allowNonTsExtensions: true,
18
- resolveJsonModule: true,
19
- jsx: 1,
20
- };
21
- return options;
22
- function readCheckJs() {
23
- return implicitProjectConfig_1['checkJs']
24
- ?? implicitProjectConfig_2['checkJs']
25
- ?? false;
26
- }
27
- function readExperimentalDecorators() {
28
- return implicitProjectConfig_1['experimentalDecorators']
29
- ?? implicitProjectConfig_2['experimentalDecorators']
30
- ?? false;
31
- }
32
- function readImplicitStrictNullChecks() {
33
- return implicitProjectConfig_1['strictNullChecks'] ?? false;
34
- }
35
- function readImplicitStrictFunctionTypes() {
36
- return implicitProjectConfig_1['strictFunctionTypes'] ?? true;
37
- }
38
- function inferredProjectCompilerOptions(projectType) {
39
- const projectConfig = {
40
- module: 1,
41
- target: 7,
42
- jsx: 1,
43
- };
44
- if (checkJs) {
45
- projectConfig.checkJs = true;
46
- if (projectType === 'typescript') {
47
- projectConfig.allowJs = true;
48
- }
49
- }
50
- if (experimentalDecorators) {
51
- projectConfig.experimentalDecorators = true;
52
- }
53
- if (strictNullChecks) {
54
- projectConfig.strictNullChecks = true;
55
- }
56
- if (strictFunctionTypes) {
57
- projectConfig.strictFunctionTypes = true;
58
- }
59
- if (projectType === 'typescript') {
60
- projectConfig.sourceMap = true;
61
- }
62
- return projectConfig;
63
- }
64
- }
65
- //# sourceMappingURL=inferredCompilerOptions.js.map
@@ -1,22 +0,0 @@
1
- import { type Language, type LanguagePlugin, type ProjectContext, type ProviderResult } from '@volar/language-service';
2
- import type * as ts from 'typescript';
3
- import { URI } from 'vscode-uri';
4
- import type { LanguageServerProject } from '../types';
5
- import { type ProjectExposeContext } from './typescriptProjectLs';
6
- export declare function createTypeScriptProject(ts: typeof import('typescript'), tsLocalized: ts.MapLike<string> | undefined, create: (projectContext: ProjectExposeContext) => ProviderResult<{
7
- languagePlugins: LanguagePlugin<URI>[];
8
- setup?(options: {
9
- language: Language;
10
- project: ProjectContext;
11
- }): void;
12
- }>): LanguageServerProject;
13
- export declare function createUriConverter(rootFolders: URI[]): {
14
- asFileName: (parsed: URI) => string;
15
- asUri: (fileName: string) => URI;
16
- };
17
- export declare function sortTSConfigs(file: string, a: string, b: string): number;
18
- export declare function isFileInDir(fileName: string, dir: string): boolean;
19
- export declare function getWorkspaceFolder(uri: URI, workspaceFolders: {
20
- has(uri: URI): boolean;
21
- all: URI[];
22
- }): URI;
@@ -1,324 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTypeScriptProject = createTypeScriptProject;
4
- exports.createUriConverter = createUriConverter;
5
- exports.sortTSConfigs = sortTSConfigs;
6
- exports.isFileInDir = isFileInDir;
7
- exports.getWorkspaceFolder = getWorkspaceFolder;
8
- const language_service_1 = require("@volar/language-service");
9
- const path = require("path-browserify");
10
- const vscode = require("vscode-languageserver");
11
- const vscode_uri_1 = require("vscode-uri");
12
- const inferredCompilerOptions_1 = require("./inferredCompilerOptions");
13
- const simpleProject_1 = require("./simpleProject");
14
- const typescriptProjectLs_1 = require("./typescriptProjectLs");
15
- const rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];
16
- function createTypeScriptProject(ts, tsLocalized, create) {
17
- let server;
18
- let uriConverter;
19
- const configProjects = (0, language_service_1.createUriMap)();
20
- const inferredProjects = (0, language_service_1.createUriMap)();
21
- const rootTsConfigs = new Set();
22
- const searchedDirs = new Set();
23
- return {
24
- setup(_server) {
25
- uriConverter = createUriConverter(_server.workspaceFolders.all);
26
- server = _server;
27
- server.fileWatcher.onDidChangeWatchedFiles(({ changes }) => {
28
- const tsConfigChanges = changes.filter(change => rootTsConfigNames.includes(change.uri.substring(change.uri.lastIndexOf('/') + 1)));
29
- for (const change of tsConfigChanges) {
30
- const changeUri = vscode_uri_1.URI.parse(change.uri);
31
- const changeFileName = uriConverter.asFileName(changeUri);
32
- if (change.type === vscode.FileChangeType.Created) {
33
- rootTsConfigs.add(changeFileName);
34
- }
35
- else if ((change.type === vscode.FileChangeType.Changed || change.type === vscode.FileChangeType.Deleted)
36
- && configProjects.has(changeUri)) {
37
- if (change.type === vscode.FileChangeType.Deleted) {
38
- rootTsConfigs.delete(changeFileName);
39
- }
40
- const project = configProjects.get(changeUri);
41
- configProjects.delete(changeUri);
42
- project?.then(project => project.dispose());
43
- }
44
- }
45
- server.languageFeatures.requestRefresh(!!tsConfigChanges.length);
46
- });
47
- },
48
- async getLanguageService(uri) {
49
- const tsconfig = await findMatchTSConfig(server, uri);
50
- if (tsconfig) {
51
- const project = await getOrCreateConfiguredProject(server, tsconfig);
52
- return project.languageService;
53
- }
54
- const workspaceFolder = getWorkspaceFolder(uri, server.workspaceFolders);
55
- const project = await getOrCreateInferredProject(server, uri, workspaceFolder);
56
- return project.languageService;
57
- },
58
- async getExistingLanguageServices() {
59
- const projects = await Promise.all([
60
- ...configProjects.values() ?? [],
61
- ...inferredProjects.values() ?? [],
62
- ]);
63
- return projects.map(project => project.languageService);
64
- },
65
- reload() {
66
- for (const project of [
67
- ...configProjects.values() ?? [],
68
- ...inferredProjects.values() ?? [],
69
- ]) {
70
- project.then(p => p.dispose());
71
- }
72
- configProjects.clear();
73
- inferredProjects.clear();
74
- },
75
- };
76
- async function findMatchTSConfig(server, uri) {
77
- const fileName = uriConverter.asFileName(uri);
78
- let dir = path.dirname(fileName);
79
- while (true) {
80
- if (searchedDirs.has(dir)) {
81
- break;
82
- }
83
- searchedDirs.add(dir);
84
- for (const tsConfigName of rootTsConfigNames) {
85
- const tsconfigPath = path.join(dir, tsConfigName);
86
- if ((await server.fileSystem.stat?.(uriConverter.asUri(tsconfigPath)))?.type === language_service_1.FileType.File) {
87
- rootTsConfigs.add(tsconfigPath);
88
- }
89
- }
90
- dir = path.dirname(dir);
91
- }
92
- await prepareClosestootCommandLine();
93
- return await findDirectIncludeTsconfig() ?? await findIndirectReferenceTsconfig();
94
- async function prepareClosestootCommandLine() {
95
- let matches = [];
96
- for (const rootTsConfig of rootTsConfigs) {
97
- if (isFileInDir(fileName, path.dirname(rootTsConfig))) {
98
- matches.push(rootTsConfig);
99
- }
100
- }
101
- matches = matches.sort((a, b) => sortTSConfigs(fileName, a, b));
102
- if (matches.length) {
103
- await getCommandLine(matches[0]);
104
- }
105
- }
106
- function findIndirectReferenceTsconfig() {
107
- return findTSConfig(async (tsconfig) => {
108
- const tsconfigUri = uriConverter.asUri(tsconfig);
109
- const project = await configProjects.get(tsconfigUri);
110
- const languageService = project?.languageService.context.inject('typescript/languageService');
111
- return !!languageService?.getProgram()?.getSourceFile(fileName);
112
- });
113
- }
114
- function findDirectIncludeTsconfig() {
115
- return findTSConfig(async (tsconfig) => {
116
- const map = (0, language_service_1.createUriMap)();
117
- const commandLine = await getCommandLine(tsconfig);
118
- for (const fileName of commandLine?.fileNames ?? []) {
119
- const uri = uriConverter.asUri(fileName);
120
- map.set(uri, true);
121
- }
122
- return map.has(uri);
123
- });
124
- }
125
- async function findTSConfig(match) {
126
- const checked = new Set();
127
- for (const rootTsConfig of [...rootTsConfigs].sort((a, b) => sortTSConfigs(fileName, a, b))) {
128
- const tsconfigUri = uriConverter.asUri(rootTsConfig);
129
- const project = await configProjects.get(tsconfigUri);
130
- if (project) {
131
- let chains = await getReferencesChains(project.getCommandLine(), rootTsConfig, []);
132
- // This is to be consistent with tsserver behavior
133
- chains = chains.reverse();
134
- for (const chain of chains) {
135
- for (let i = chain.length - 1; i >= 0; i--) {
136
- const tsconfig = chain[i];
137
- if (checked.has(tsconfig)) {
138
- continue;
139
- }
140
- checked.add(tsconfig);
141
- if (await match(tsconfig)) {
142
- return tsconfig;
143
- }
144
- }
145
- }
146
- }
147
- }
148
- }
149
- async function getReferencesChains(commandLine, tsConfig, before) {
150
- if (commandLine.projectReferences?.length) {
151
- const newChains = [];
152
- for (const projectReference of commandLine.projectReferences) {
153
- let tsConfigPath = projectReference.path.replace(/\\/g, '/');
154
- // fix https://github.com/johnsoncodehk/volar/issues/712
155
- if ((await server.fileSystem.stat?.(uriConverter.asUri(tsConfigPath)))?.type === language_service_1.FileType.Directory) {
156
- const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
157
- const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
158
- if ((await server.fileSystem.stat?.(uriConverter.asUri(newTsConfigPath)))?.type === language_service_1.FileType.File) {
159
- tsConfigPath = newTsConfigPath;
160
- }
161
- else if ((await server.fileSystem.stat?.(uriConverter.asUri(newJsConfigPath)))?.type === language_service_1.FileType.File) {
162
- tsConfigPath = newJsConfigPath;
163
- }
164
- }
165
- const beforeIndex = before.indexOf(tsConfigPath); // cycle
166
- if (beforeIndex >= 0) {
167
- newChains.push(before.slice(0, Math.max(beforeIndex, 1)));
168
- }
169
- else {
170
- const referenceCommandLine = await getCommandLine(tsConfigPath);
171
- if (referenceCommandLine) {
172
- for (const chain of await getReferencesChains(referenceCommandLine, tsConfigPath, [...before, tsConfig])) {
173
- newChains.push(chain);
174
- }
175
- }
176
- }
177
- }
178
- return newChains;
179
- }
180
- else {
181
- return [[...before, tsConfig]];
182
- }
183
- }
184
- async function getCommandLine(tsConfig) {
185
- const project = await getOrCreateConfiguredProject(server, tsConfig);
186
- return project?.getCommandLine();
187
- }
188
- }
189
- function getOrCreateConfiguredProject(server, tsconfig) {
190
- tsconfig = tsconfig.replace(/\\/g, '/');
191
- const tsconfigUri = uriConverter.asUri(tsconfig);
192
- let projectPromise = configProjects.get(tsconfigUri);
193
- if (!projectPromise) {
194
- const workspaceFolder = getWorkspaceFolder(tsconfigUri, server.workspaceFolders);
195
- const serviceEnv = (0, simpleProject_1.createLanguageServiceEnvironment)(server, [workspaceFolder]);
196
- projectPromise = (0, typescriptProjectLs_1.createTypeScriptLS)(ts, tsLocalized, tsconfig, server, serviceEnv, workspaceFolder, uriConverter, create);
197
- configProjects.set(tsconfigUri, projectPromise);
198
- }
199
- return projectPromise;
200
- }
201
- async function getOrCreateInferredProject(server, uri, workspaceFolder) {
202
- if (!inferredProjects.has(workspaceFolder)) {
203
- inferredProjects.set(workspaceFolder, (async () => {
204
- const inferOptions = await (0, inferredCompilerOptions_1.getInferredCompilerOptions)(server);
205
- const serviceEnv = (0, simpleProject_1.createLanguageServiceEnvironment)(server, [workspaceFolder]);
206
- return (0, typescriptProjectLs_1.createTypeScriptLS)(ts, tsLocalized, inferOptions, server, serviceEnv, workspaceFolder, uriConverter, create);
207
- })());
208
- }
209
- const project = await inferredProjects.get(workspaceFolder);
210
- project.tryAddFile(uriConverter.asFileName(uri));
211
- return project;
212
- }
213
- }
214
- function createUriConverter(rootFolders) {
215
- const encodeds = new Map();
216
- const isFileScheme = rootFolders.every(folder => folder.scheme === 'file');
217
- const fragmentPrefix = '/' + encodeURIComponent('#');
218
- return {
219
- asFileName,
220
- asUri,
221
- };
222
- function asFileName(parsed) {
223
- if (rootFolders.every(folder => folder.scheme === parsed.scheme && folder.authority === parsed.authority)) {
224
- if (isFileScheme) {
225
- return parsed.fsPath.replace(/\\/g, '/');
226
- }
227
- else {
228
- return parsed.path;
229
- }
230
- }
231
- const encoded = encodeURIComponent(`${parsed.scheme}://${parsed.authority}`);
232
- encodeds.set(encoded, parsed);
233
- const fragment = parsed.fragment ? fragmentPrefix + encodeURIComponent(parsed.fragment) : '';
234
- return `/${encoded}${parsed.path}${fragment}`;
235
- }
236
- function asUri(fileName) {
237
- for (const [encoded, uri] of encodeds) {
238
- const prefix = `/${encoded}`;
239
- if (fileName === prefix) {
240
- return vscode_uri_1.URI.from({
241
- scheme: uri.scheme,
242
- authority: uri.authority,
243
- });
244
- }
245
- if (uri.authority) {
246
- if (fileName.startsWith(prefix + '/')) {
247
- return vscode_uri_1.URI.from({
248
- scheme: uri.scheme,
249
- authority: uri.authority,
250
- ...getComponents(fileName, prefix.length),
251
- });
252
- }
253
- }
254
- else {
255
- if (fileName.startsWith(prefix)) {
256
- return vscode_uri_1.URI.from({
257
- scheme: uri.scheme,
258
- authority: uri.authority,
259
- ...getComponents(fileName, prefix.length),
260
- });
261
- }
262
- }
263
- }
264
- if (!isFileScheme) {
265
- for (const folder of rootFolders) {
266
- return vscode_uri_1.URI.parse(`${folder.scheme}://${folder.authority}${fileName}`);
267
- }
268
- }
269
- return vscode_uri_1.URI.file(fileName);
270
- }
271
- function getComponents(fileName, prefixLength) {
272
- // Fragment is present when the fileName contains the fragment prefix and is not followed by a slash.
273
- const fragmentPosition = fileName.lastIndexOf(fragmentPrefix);
274
- if (fragmentPosition >= prefixLength) {
275
- if (fileName.indexOf('/', fragmentPosition + fragmentPrefix.length) < 0) {
276
- return {
277
- path: fileName.substring(prefixLength, fragmentPosition),
278
- fragment: decodeURIComponent(fileName.substring(fragmentPosition + fragmentPrefix.length)),
279
- };
280
- }
281
- }
282
- return {
283
- path: fileName.substring(prefixLength),
284
- };
285
- }
286
- }
287
- function sortTSConfigs(file, a, b) {
288
- const inA = isFileInDir(file, path.dirname(a));
289
- const inB = isFileInDir(file, path.dirname(b));
290
- if (inA !== inB) {
291
- const aWeight = inA ? 1 : 0;
292
- const bWeight = inB ? 1 : 0;
293
- return bWeight - aWeight;
294
- }
295
- const aLength = a.split('/').length;
296
- const bLength = b.split('/').length;
297
- if (aLength === bLength) {
298
- const aWeight = path.basename(a) === 'tsconfig.json' ? 1 : 0;
299
- const bWeight = path.basename(b) === 'tsconfig.json' ? 1 : 0;
300
- return bWeight - aWeight;
301
- }
302
- return bLength - aLength;
303
- }
304
- function isFileInDir(fileName, dir) {
305
- const relative = path.relative(dir, fileName);
306
- return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
307
- }
308
- function getWorkspaceFolder(uri, workspaceFolders) {
309
- while (true) {
310
- if (workspaceFolders.has(uri)) {
311
- return uri;
312
- }
313
- const next = uri.with({ path: uri.path.substring(0, uri.path.lastIndexOf('/')) });
314
- if (next.path === uri.path) {
315
- break;
316
- }
317
- uri = next;
318
- }
319
- for (const folder of workspaceFolders.all) {
320
- return folder;
321
- }
322
- return uri.with({ path: '/' });
323
- }
324
- //# sourceMappingURL=typescriptProject.js.map
@@ -1,31 +0,0 @@
1
- import { type Language, type LanguagePlugin, type LanguageService, type LanguageServiceEnvironment, type ProjectContext, type ProviderResult } from '@volar/language-service';
2
- import { createSys, type TypeScriptProjectHost } from '@volar/typescript';
3
- import type * as ts from 'typescript';
4
- import { URI } from 'vscode-uri';
5
- import type { LanguageServer } from '../types';
6
- export interface TypeScriptProjectLS {
7
- tryAddFile(fileName: string): void;
8
- getCommandLine(): ts.ParsedCommandLine;
9
- languageService: LanguageService;
10
- dispose(): void;
11
- }
12
- export interface ProjectExposeContext {
13
- env: LanguageServiceEnvironment;
14
- configFileName: string | undefined;
15
- projectHost: TypeScriptProjectHost;
16
- sys: ReturnType<typeof createSys>;
17
- uriConverter: {
18
- asUri(fileName: string): URI;
19
- asFileName(uri: URI): string;
20
- };
21
- }
22
- export declare function createTypeScriptLS(ts: typeof import('typescript'), tsLocalized: ts.MapLike<string> | undefined, tsconfig: string | ts.CompilerOptions, server: LanguageServer, serviceEnv: LanguageServiceEnvironment, workspaceFolder: URI, uriConverter: {
23
- asUri(fileName: string): URI;
24
- asFileName(uri: URI): string;
25
- }, create: (projectContext: ProjectExposeContext) => ProviderResult<{
26
- languagePlugins: LanguagePlugin<URI>[];
27
- setup?(options: {
28
- language: Language;
29
- project: ProjectContext;
30
- }): void;
31
- }>): Promise<TypeScriptProjectLS>;