@volar/language-server 1.6.9 → 1.7.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.
@@ -15,12 +15,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.startLanguageServer = exports.createConnection = void 0;
18
- const request_light_1 = require("request-light");
19
18
  const vscode = require("vscode-languageserver/browser");
20
19
  const server_1 = require("../common/server");
21
20
  const http_1 = require("../common/schemaRequestHandlers/http");
22
- const fileSystems_1 = require("./fileSystems");
23
21
  const vscode_uri_1 = require("vscode-uri");
22
+ const protocol_1 = require("../protocol");
24
23
  __exportStar(require("../index"), exports);
25
24
  function createConnection() {
26
25
  const messageReader = new vscode.BrowserMessageReader(self);
@@ -30,68 +29,64 @@ function createConnection() {
30
29
  }
31
30
  exports.createConnection = createConnection;
32
31
  function startLanguageServer(connection, ...plugins) {
33
- (0, server_1.startCommonLanguageServer)(connection, (options) => {
34
- return {
35
- plugins,
36
- connection,
37
- runtimeEnv: {
38
- uriToFileName,
39
- fileNameToUri,
40
- timer: {
41
- setImmediate(callback, ...args) {
42
- const handle = setTimeout(callback, 0, ...args);
43
- return { dispose: () => clearTimeout(handle) };
44
- },
45
- },
46
- loadTypescript() {
47
- return require('typescript'); // force bundle because not support load by user config in web
48
- },
49
- async loadTypescriptLocalized(tsdk, locale) {
50
- try {
51
- const uri = fileNameToUri(`${tsdk}/${locale}/diagnosticMessages.generated.json`);
52
- const json = await (0, http_1.default)(uri);
53
- if (json) {
54
- return JSON.parse(json);
55
- }
56
- }
57
- catch { }
58
- },
59
- schemaRequestHandlers: {
60
- http: http_1.default,
61
- https: http_1.default,
62
- },
63
- onDidChangeConfiguration(settings) {
64
- (0, request_light_1.configure)(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
65
- },
66
- fileSystemProvide: undefined,
67
- createFileSystemHost: fileSystems_1.createWebFileSystemHost,
32
+ (0, server_1.startCommonLanguageServer)(connection, plugins, () => ({
33
+ uriToFileName,
34
+ fileNameToUri,
35
+ timer: {
36
+ setImmediate(callback, ...args) {
37
+ const handle = setTimeout(callback, 0, ...args);
38
+ return { dispose: () => clearTimeout(handle) };
68
39
  },
69
- };
70
- function uriToFileName(uri) {
71
- const parsed = vscode_uri_1.URI.parse(uri);
72
- if (options.typescript?.cdn && uri.startsWith(options.typescript.cdn)) {
73
- return parsed.toString(true).replace(options.typescript.cdn, '/node_modules/');
40
+ },
41
+ loadTypescript() {
42
+ return require('typescript'); // force bundle because not support load by user config in web
43
+ },
44
+ async loadTypescriptLocalized(tsdk, locale) {
45
+ try {
46
+ const uri = fileNameToUri(`${tsdk}/${locale}/diagnosticMessages.generated.json`);
47
+ const json = await (0, http_1.default)(uri);
48
+ if (json) {
49
+ return JSON.parse(json);
50
+ }
74
51
  }
75
- return `/${parsed.scheme}${parsed.authority ? '@' + parsed.authority : ''}${parsed.path}`;
76
- }
77
- ;
78
- function fileNameToUri(fileName) {
79
- if (fileName.startsWith('/node_modules/') && options.typescript?.cdn) {
80
- return vscode_uri_1.URI.parse(fileName.replace('/node_modules/', options.typescript.cdn)).toString();
81
- }
82
- const parts = fileName.split('/');
83
- if (parts.length <= 1) {
84
- return vscode_uri_1.URI.from({ scheme: '', path: '' }).toString();
85
- }
86
- const firstParts = parts[1].split('@');
87
- return vscode_uri_1.URI.from({
88
- scheme: firstParts[0],
89
- authority: firstParts.length > 1 ? firstParts[1] : undefined,
90
- path: '/' + parts.slice(2).join('/'),
91
- }).toString();
92
- }
93
- ;
94
- });
52
+ catch { }
53
+ },
54
+ fs: {
55
+ stat(uri) {
56
+ return connection.sendRequest(protocol_1.FsStatRequest.type, uri);
57
+ },
58
+ async readFile(uri, encoding) {
59
+ if (uri.startsWith('http://') || uri.startsWith('https://')) {
60
+ return await (0, http_1.default)(uri);
61
+ }
62
+ const data = await connection.sendRequest(protocol_1.FsReadFileRequest.type, uri);
63
+ if (data) {
64
+ return new TextDecoder(encoding ?? 'utf8').decode(data);
65
+ }
66
+ },
67
+ async readDirectory(uri) {
68
+ return connection.sendRequest(protocol_1.FsReadDirectoryRequest.type, uri);
69
+ },
70
+ },
71
+ }));
95
72
  }
96
73
  exports.startLanguageServer = startLanguageServer;
74
+ function uriToFileName(uri) {
75
+ const parsed = vscode_uri_1.URI.parse(uri);
76
+ return `/${parsed.scheme}${parsed.authority ? '@' + parsed.authority : ''}${parsed.path}`;
77
+ }
78
+ ;
79
+ function fileNameToUri(fileName) {
80
+ const parts = fileName.split('/');
81
+ if (parts.length <= 1) {
82
+ return vscode_uri_1.URI.from({ scheme: '', path: '' }).toString();
83
+ }
84
+ const firstParts = parts[1].split('@');
85
+ return vscode_uri_1.URI.from({
86
+ scheme: firstParts[0],
87
+ authority: firstParts.length > 1 ? firstParts[1] : undefined,
88
+ path: '/' + parts.slice(2).join('/'),
89
+ }).toString();
90
+ }
91
+ ;
97
92
  //# sourceMappingURL=index.js.map
@@ -33,12 +33,8 @@ function createCancellationTokenHost(_cancellationPipeName) {
33
33
  return token;
34
34
  }
35
35
  function getMtime() {
36
- try {
37
- return fs.statSync(cancellationPipeName).mtime.valueOf();
38
- }
39
- catch {
40
- return -1;
41
- }
36
+ const stat = fs.statSync(cancellationPipeName, { throwIfNoEntry: false });
37
+ return stat?.mtime.valueOf() ?? -1;
42
38
  }
43
39
  }
44
40
  exports.createCancellationTokenHost = createCancellationTokenHost;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createConfigurationHost = void 0;
4
4
  const vscode = require("vscode-languageserver");
5
5
  function createConfigurationHost(params, connection) {
6
- const callbacks = [];
6
+ const callbacks = new Set();
7
7
  const cache = new Map();
8
8
  connection.onDidChangeConfiguration(async () => {
9
9
  cache.clear();
@@ -27,7 +27,12 @@ function createConfigurationHost(params, connection) {
27
27
  return await getConfigurationWorker(section, scopeUri);
28
28
  },
29
29
  onDidChangeConfiguration(cb) {
30
- callbacks.push(cb);
30
+ callbacks.add(cb);
31
+ return {
32
+ dispose() {
33
+ callbacks.delete(cb);
34
+ },
35
+ };
31
36
  },
32
37
  };
33
38
  async function getConfigurationWorker(section, scopeUri) {
@@ -39,7 +39,7 @@ export declare function createDocuments(env: RuntimeEnvironment, connection: vsc
39
39
  pathHas: (path: string) => IncrementalScriptSnapshot | undefined;
40
40
  pathSet: (path: string, item: IncrementalScriptSnapshot) => void;
41
41
  };
42
- onDidChangeContent: (cb: (params: vscode.DidChangeTextDocumentParams) => void) => () => boolean;
43
- onDidClose: (cb: (params: vscode.DidCloseTextDocumentParams) => void) => () => boolean;
42
+ onDidChangeContent: (cb: (params: vscode.DidChangeTextDocumentParams) => void) => vscode.Disposable;
43
+ onDidClose: (cb: (params: vscode.DidCloseTextDocumentParams) => void) => vscode.Disposable;
44
44
  };
45
45
  export {};
@@ -196,11 +196,19 @@ function createDocuments(env, connection) {
196
196
  data: snapshots,
197
197
  onDidChangeContent: (cb) => {
198
198
  onDidChangeContents.add(cb);
199
- return () => onDidChangeContents.delete(cb);
199
+ return {
200
+ dispose() {
201
+ onDidChangeContents.delete(cb);
202
+ },
203
+ };
200
204
  },
201
205
  onDidClose: (cb) => {
202
206
  onDidCloses.add(cb);
203
- return () => onDidCloses.delete(cb);
207
+ return {
208
+ dispose() {
209
+ onDidCloses.delete(cb);
210
+ },
211
+ };
204
212
  },
205
213
  };
206
214
  }
@@ -2,57 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.register = void 0;
4
4
  const protocol_1 = require("../../protocol");
5
+ const language_core_1 = require("@volar/language-core");
5
6
  function register(connection, workspaces, env) {
6
- connection.onNotification(protocol_1.ReportStats.type, async () => {
7
- for (const [rootUri, _workspace] of workspaces.workspaces) {
8
- connection.console.log('workspace: ' + rootUri);
9
- const workspace = await _workspace;
10
- connection.console.log('documentRegistry stats: ' + workspace.documentRegistry?.reportStats());
11
- connection.console.log('');
12
- connection.console.log('tsconfig: inferred');
13
- const _inferredProject = workspace.getInferredProjectDontCreate();
14
- if (_inferredProject) {
15
- connection.console.log('loaded: true');
16
- const inferredProject = await _inferredProject;
17
- connection.console.log('largest 10 files:');
18
- for (const script of [...inferredProject.scripts.values()]
19
- .sort((a, b) => (b.snapshot?.getLength() ?? 0) - (a.snapshot?.getLength() ?? 0))
20
- .slice(0, 10)) {
21
- connection.console.log(' - ' + script.fileName);
22
- connection.console.log(` size: ${script.snapshot?.getLength()}`);
23
- }
24
- connection.console.log('files:');
25
- for (const script of inferredProject.scripts.values()) {
26
- connection.console.log(' - ' + script.fileName);
27
- connection.console.log(` size: ${script.snapshot?.getLength()}`);
28
- connection.console.log(` ref counts: "${workspace.documentRegistry.getLanguageServiceRefCounts?.(script.fileName, inferredProject.languageServiceHost.getScriptKind?.(script.fileName))})"`);
29
- }
30
- }
31
- else {
32
- connection.console.log('loaded: false');
33
- }
34
- connection.console.log('');
35
- for (const _project of workspace.projects.values()) {
36
- const project = await _project;
37
- connection.console.log('tsconfig: ' + project.tsConfig);
38
- connection.console.log('loaded: ' + !!project.getLanguageServiceDontCreate());
39
- connection.console.log('largest 10 files:');
40
- for (const script of [...project.scripts.values()]
41
- .sort((a, b) => (b.snapshot?.getLength() ?? 0) - (a.snapshot?.getLength() ?? 0))
42
- .slice(0, 10)) {
43
- connection.console.log(' - ' + script.fileName);
44
- connection.console.log(` size: ${script.snapshot?.getLength()}`);
45
- }
46
- connection.console.log('files:');
47
- for (const script of project.scripts.values()) {
48
- connection.console.log(' - ' + script.fileName);
49
- connection.console.log(` size: ${script.snapshot?.getLength()}`);
50
- connection.console.log(` ref counts: "${workspace.documentRegistry.getLanguageServiceRefCounts?.(script.fileName, project.languageServiceHost.getScriptKind?.(script.fileName))})"`);
51
- }
52
- }
53
- connection.console.log('');
54
- }
55
- });
7
+ const scriptVersions = new Map();
8
+ const scriptVersionSnapshots = new WeakSet();
56
9
  connection.onRequest(protocol_1.GetMatchTsConfigRequest.type, async (params) => {
57
10
  const project = (await workspaces.getProject(params.uri));
58
11
  if (project?.tsconfig) {
@@ -92,12 +45,12 @@ function register(connection, workspaces, env) {
92
45
  const project = await workspace.getInferredProject();
93
46
  if (!project)
94
47
  return [];
95
- return project.languageServiceHost.getScriptFileNames();
48
+ return project.languageHost.getScriptFileNames();
96
49
  }
97
50
  for (const _project of workspace.projects.values()) {
98
51
  const project = await _project;
99
52
  if (project.tsConfig === params.tsconfig) {
100
- return project.languageServiceHost.getScriptFileNames();
53
+ return project.languageHost.getScriptFileNames();
101
54
  }
102
55
  }
103
56
  return [];
@@ -105,15 +58,21 @@ function register(connection, workspaces, env) {
105
58
  connection.onRequest(protocol_1.GetVirtualFilesRequest.type, async (document) => {
106
59
  const project = await workspaces.getProject(document.uri);
107
60
  if (project) {
108
- const file = project.project?.getLanguageService().context.core.virtualFiles.getSource(env.uriToFileName(document.uri))?.root;
61
+ const file = project.project?.getLanguageService()?.context.virtualFiles.getSource(env.uriToFileName(document.uri))?.root;
109
62
  return file ? prune(file) : undefined;
110
63
  function prune(file) {
64
+ let version = scriptVersions.get(file.fileName) ?? 0;
65
+ if (!scriptVersionSnapshots.has(file.snapshot)) {
66
+ version++;
67
+ scriptVersions.set(file.fileName, version);
68
+ scriptVersionSnapshots.add(file.snapshot);
69
+ }
111
70
  return {
112
71
  fileName: file.fileName,
113
72
  kind: file.kind,
114
73
  capabilities: file.capabilities,
115
74
  embeddedFiles: file.embeddedFiles.map(prune),
116
- version: project.project.getLanguageService().context.core.typescript.languageServiceHost.getScriptVersion(file.fileName),
75
+ version,
117
76
  };
118
77
  }
119
78
  }
@@ -121,7 +80,7 @@ function register(connection, workspaces, env) {
121
80
  connection.onRequest(protocol_1.GetVirtualFileRequest.type, async (params) => {
122
81
  const project = await workspaces.getProject(params.sourceFileUri);
123
82
  if (project) {
124
- const [virtualFile, source] = project.project?.getLanguageService().context.core.virtualFiles.getVirtualFile(params.virtualFileName) ?? [];
83
+ const [virtualFile, source] = project.project?.getLanguageService()?.context.virtualFiles.getVirtualFile(params.virtualFileName) ?? [];
125
84
  if (virtualFile && source) {
126
85
  const mappings = {};
127
86
  for (const mapping of virtualFile.mappings) {
@@ -146,12 +105,23 @@ function register(connection, workspaces, env) {
146
105
  if (project) {
147
106
  const ls = (await project.project)?.getLanguageServiceDontCreate();
148
107
  if (ls) {
149
- const sourceFiles = new Set(ls.context.host.getScriptFileNames());
150
- for (const virtualFile of ls.context.core.typescript.languageServiceHost.getScriptFileNames()) {
151
- if (virtualFile.startsWith(ls.context.host.getCurrentDirectory()) && !sourceFiles.has(virtualFile)) {
152
- const snapshot = ls.context.core.typescript.languageServiceHost.getScriptSnapshot(virtualFile);
108
+ const rootPath = ls.context.env.uriToFileName(ls.context.env.rootUri.toString());
109
+ for (const { root } of ls.context.virtualFiles.allSources()) {
110
+ (0, language_core_1.forEachEmbeddedFile)(root, virtualFile => {
111
+ if (virtualFile.kind === language_core_1.FileKind.TypeScriptHostFile) {
112
+ if (virtualFile.fileName.startsWith(rootPath)) {
113
+ const snapshot = virtualFile.snapshot;
114
+ fs.writeFile(virtualFile.fileName, snapshot.getText(0, snapshot.getLength()), () => { });
115
+ }
116
+ }
117
+ });
118
+ }
119
+ // global virtual files
120
+ for (const fileName of ls.context.host.getScriptFileNames()) {
121
+ if (!fs.existsSync(fileName)) {
122
+ const snapshot = ls.context.host.getScriptSnapshot(fileName);
153
123
  if (snapshot) {
154
- fs.writeFile(virtualFile, snapshot.getText(0, snapshot.getLength()), () => { });
124
+ fs.writeFile(fileName, snapshot.getText(0, snapshot.getLength()), () => { });
155
125
  }
156
126
  }
157
127
  }
@@ -289,8 +289,9 @@ function register(connection, projects, initParams, initOptions, cancelHost, sem
289
289
  resolve(undefined);
290
290
  return;
291
291
  }
292
- const service = await getLanguageService(uri);
293
- if (service) {
292
+ const project = await getProject(uri);
293
+ if (project) {
294
+ const service = project.getLanguageService();
294
295
  try { // handle TS cancel throw
295
296
  const result = await cb(service);
296
297
  if (token.isCancellationRequested) {
@@ -310,9 +311,8 @@ function register(connection, projects, initParams, initOptions, cancelHost, sem
310
311
  });
311
312
  });
312
313
  }
313
- async function getLanguageService(uri) {
314
- const project = (await projects.getProject(uri))?.project;
315
- return project?.getLanguageService();
314
+ async function getProject(uri) {
315
+ return (await projects.getProject(uri))?.project;
316
316
  }
317
317
  function fixTextEdit(item) {
318
318
  const insertReplaceSupport = initParams.capabilities.textDocument?.completion?.completionItem?.insertReplaceSupport ?? false;