@volar/language-server 1.6.7 → 1.6.9-patch.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.
@@ -71,6 +71,7 @@ function createWebFileSystemHost(_0, _1, env, initOptions) {
71
71
  function createWorkspaceFileSystem(rootUri) {
72
72
  const rootPath = env.uriToFileName(rootUri.toString());
73
73
  return {
74
+ args: [],
74
75
  newLine: '\n',
75
76
  useCaseSensitiveFileNames: false,
76
77
  getCurrentDirectory: () => env.uriToFileName(rootUri.toString()),
@@ -79,7 +80,14 @@ function createWebFileSystemHost(_0, _1, env, initOptions) {
79
80
  readDirectory,
80
81
  getDirectories,
81
82
  resolvePath,
82
- realpath: path => path, // TODO: cannot implement with vscode
83
+ // ignore
84
+ write: () => { },
85
+ writeFile: () => { },
86
+ createDirectory: () => { },
87
+ realpath: path => path,
88
+ directoryExists: () => true,
89
+ exit: () => { },
90
+ getExecutingFilePath: () => rootPath + '/__fake__.js',
83
91
  };
84
92
  function resolvePath(fsPath) {
85
93
  return path.resolve(fsPath);
@@ -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) {
@@ -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,22 @@ 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
+ for (const { root } of ls.context.virtualFiles.allSources()) {
109
+ (0, language_core_1.forEachEmbeddedFile)(root, virtualFile => {
110
+ if (virtualFile.kind === language_core_1.FileKind.TypeScriptHostFile) {
111
+ if (virtualFile.fileName.startsWith(ls.context.host.getCurrentDirectory())) {
112
+ const snapshot = virtualFile.snapshot;
113
+ fs.writeFile(virtualFile.fileName, snapshot.getText(0, snapshot.getLength()), () => { });
114
+ }
115
+ }
116
+ });
117
+ }
118
+ // global virtual files
119
+ for (const fileName of ls.context.host.getScriptFileNames()) {
120
+ if (!fs.existsSync(fileName)) {
121
+ const snapshot = ls.context.host.getScriptSnapshot(fileName);
153
122
  if (snapshot) {
154
- fs.writeFile(virtualFile, snapshot.getText(0, snapshot.getLength()), () => { });
123
+ fs.writeFile(fileName, snapshot.getText(0, snapshot.getLength()), () => { });
155
124
  }
156
125
  }
157
126
  }
@@ -111,8 +111,12 @@ function register(connection, projects, initParams, initOptions, cancelHost, sem
111
111
  });
112
112
  });
113
113
  connection.onPrepareRename(async (params, token) => {
114
- return worker(params.textDocument.uri, token, service => {
115
- return service.prepareRename(params.textDocument.uri, params.position, token);
114
+ return worker(params.textDocument.uri, token, async (service) => {
115
+ const result = await service.prepareRename(params.textDocument.uri, params.position, token);
116
+ if (result && 'message' in result) {
117
+ return new vscode.ResponseError(0, result.message);
118
+ }
119
+ return result;
116
120
  });
117
121
  });
118
122
  connection.onRenameRequest(async (params, token) => {
@@ -8,12 +8,12 @@ export interface ProjectContext extends WorkspaceContext {
8
8
  project: {
9
9
  rootUri: URI;
10
10
  tsConfig: path.PosixPath | ts.CompilerOptions;
11
- documentRegistry: ts.DocumentRegistry | undefined;
12
11
  };
13
12
  }
14
13
  export type Project = ReturnType<typeof createProject>;
15
14
  export declare function createProject(context: ProjectContext): Promise<{
16
- tsConfig: ts.CompilerOptions | path.PosixPath;
15
+ readFiles: Set<string>;
16
+ tsConfig: path.PosixPath | ts.CompilerOptions;
17
17
  scripts: {
18
18
  clear: () => void;
19
19
  values: () => IterableIterator<{
@@ -78,7 +78,12 @@ export declare function createProject(context: ProjectContext): Promise<{
78
78
  findDefinition: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
79
79
  findTypeDefinition: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
80
80
  findImplementations: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
81
- prepareRename: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<NonNullable<embeddedLS.Range | embeddedLS.ResponseError<void> | null | undefined> | undefined>;
81
+ prepareRename: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<NonNullable<embeddedLS.Range | {
82
+ range: embeddedLS.Range;
83
+ placeholder: string;
84
+ } | {
85
+ message: string;
86
+ } | null | undefined> | undefined>;
82
87
  doRename: (uri: string, position: embeddedLS.Position, newName: string, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.WorkspaceEdit | undefined>;
83
88
  getEditsForFileRename: (oldUri: string, newUri: string, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.WorkspaceEdit | undefined>;
84
89
  getSemanticTokens: (uri: string, range: embeddedLS.Range | undefined, legend: embeddedLS.SemanticTokensLegend, token?: embeddedLS.CancellationToken | undefined, reportProgress?: ((tokens: embeddedLS.SemanticTokens) => void) | undefined) => Promise<embeddedLS.SemanticTokens | undefined>;
@@ -102,8 +107,8 @@ export declare function createProject(context: ProjectContext): Promise<{
102
107
  getIncomingCalls(item: embeddedLS.CallHierarchyItem, token: embeddedLS.CancellationToken): Promise<embeddedLS.CallHierarchyIncomingCall[]>;
103
108
  getOutgoingCalls(item: embeddedLS.CallHierarchyItem, token: embeddedLS.CancellationToken): Promise<embeddedLS.CallHierarchyOutgoingCall[]>;
104
109
  };
105
- dispose: () => void | undefined;
106
- context: embeddedLS.ServiceContext;
110
+ dispose: () => void;
111
+ context: embeddedLS.ServiceContext<any>;
107
112
  };
108
113
  getLanguageServiceDontCreate: () => {
109
114
  triggerCharacters: string[];
@@ -126,7 +131,12 @@ export declare function createProject(context: ProjectContext): Promise<{
126
131
  findDefinition: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
127
132
  findTypeDefinition: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
128
133
  findImplementations: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.LocationLink[] | undefined>;
129
- prepareRename: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<NonNullable<embeddedLS.Range | embeddedLS.ResponseError<void> | null | undefined> | undefined>;
134
+ prepareRename: (uri: string, position: embeddedLS.Position, token?: embeddedLS.CancellationToken | undefined) => Promise<NonNullable<embeddedLS.Range | {
135
+ range: embeddedLS.Range;
136
+ placeholder: string;
137
+ } | {
138
+ message: string;
139
+ } | null | undefined> | undefined>;
130
140
  doRename: (uri: string, position: embeddedLS.Position, newName: string, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.WorkspaceEdit | undefined>;
131
141
  getEditsForFileRename: (oldUri: string, newUri: string, token?: embeddedLS.CancellationToken | undefined) => Promise<embeddedLS.WorkspaceEdit | undefined>;
132
142
  getSemanticTokens: (uri: string, range: embeddedLS.Range | undefined, legend: embeddedLS.SemanticTokensLegend, token?: embeddedLS.CancellationToken | undefined, reportProgress?: ((tokens: embeddedLS.SemanticTokens) => void) | undefined) => Promise<embeddedLS.SemanticTokens | undefined>;
@@ -150,8 +160,8 @@ export declare function createProject(context: ProjectContext): Promise<{
150
160
  getIncomingCalls(item: embeddedLS.CallHierarchyItem, token: embeddedLS.CancellationToken): Promise<embeddedLS.CallHierarchyIncomingCall[]>;
151
161
  getOutgoingCalls(item: embeddedLS.CallHierarchyItem, token: embeddedLS.CancellationToken): Promise<embeddedLS.CallHierarchyOutgoingCall[]>;
152
162
  };
153
- dispose: () => void | undefined;
154
- context: embeddedLS.ServiceContext;
163
+ dispose: () => void;
164
+ context: embeddedLS.ServiceContext<any>;
155
165
  } | undefined;
156
166
  getParsedCommandLine: () => ts.ParsedCommandLine;
157
167
  tryAddFile: (fileName: string) => void;
@@ -13,22 +13,37 @@ async function createProject(context) {
13
13
  const fileNameToUri = context.server.runtimeEnv.fileNameToUri;
14
14
  const sys = context.workspaces.initOptions.serverMode === types_1.ServerMode.Syntactic || !context.workspaces.fileSystemHost
15
15
  ? {
16
+ args: [],
16
17
  newLine: '\n',
17
18
  useCaseSensitiveFileNames: false,
18
19
  fileExists: () => false,
19
20
  readFile: () => undefined,
20
21
  readDirectory: () => [],
21
22
  getCurrentDirectory: () => '',
22
- realpath: () => '',
23
23
  resolvePath: () => '',
24
+ write: () => { },
25
+ writeFile: () => { },
26
+ createDirectory: () => { },
27
+ realpath: path => path,
28
+ directoryExists: () => true,
29
+ exit: () => { },
30
+ getDirectories: () => [],
31
+ getExecutingFilePath: () => '/__fake__.js',
24
32
  }
25
33
  : context.workspaces.fileSystemHost.getWorkspaceFileSystem(context.project.rootUri);
26
34
  let typeRootVersion = 0;
27
35
  let projectVersion = 0;
28
36
  let projectVersionUpdateTime = context.workspaces.cancelTokenHost.getMtime();
29
37
  let languageService;
30
- let parsedCommandLine = createParsedCommandLine(context.workspaces.ts, sys, uriToFileName(context.project.rootUri.toString()), context.project.tsConfig, context.workspaces.plugins);
38
+ let existingOptions;
39
+ for (const plugin of context.workspaces.plugins) {
40
+ if (plugin.resolveExistingOptions) {
41
+ existingOptions = plugin.resolveExistingOptions(existingOptions);
42
+ }
43
+ }
44
+ let parsedCommandLine = createParsedCommandLine(context.workspaces.ts, sys, uriToFileName(context.project.rootUri.toString()), context.project.tsConfig, context.workspaces.plugins, existingOptions);
31
45
  const scripts = (0, uriMap_1.createUriMap)(fileNameToUri);
46
+ const readFiles = new Set();
32
47
  const languageServiceHost = createLanguageServiceHost();
33
48
  const disposeWatchEvent = context.workspaces.fileSystemHost?.onDidChangeWatchedFiles(params => {
34
49
  onWorkspaceFilesChanged(params.changes);
@@ -38,6 +53,7 @@ async function createProject(context) {
38
53
  projectVersionUpdateTime = context.workspaces.cancelTokenHost.getMtime();
39
54
  });
40
55
  return {
56
+ readFiles,
41
57
  tsConfig: context.project.tsConfig,
42
58
  scripts,
43
59
  languageServiceHost,
@@ -74,6 +90,7 @@ async function createProject(context) {
74
90
  }
75
91
  return '';
76
92
  },
93
+ sys,
77
94
  };
78
95
  let config = (context.workspace.rootUri.scheme === 'file' ? (0, serverConfig_1.loadConfig)(context.server.runtimeEnv.uriToFileName(context.workspace.rootUri.toString()), context.workspaces.initOptions.configFilePath) : {}) ?? {};
79
96
  for (const plugin of context.workspaces.plugins) {
@@ -86,7 +103,7 @@ async function createProject(context) {
86
103
  });
87
104
  }
88
105
  }
89
- languageService = embeddedLS.createLanguageService({ typescript: context.workspaces.ts }, env, config, languageServiceHost, context.project.documentRegistry);
106
+ languageService = embeddedLS.createLanguageService({ typescript: context.workspaces.ts }, env, config, languageServiceHost);
90
107
  }
91
108
  return languageService;
92
109
  }
@@ -114,7 +131,7 @@ async function createProject(context) {
114
131
  const creates = changes.filter(change => change.type === vscode.FileChangeType.Created);
115
132
  const deletes = changes.filter(change => change.type === vscode.FileChangeType.Deleted);
116
133
  if (creates.length || deletes.length) {
117
- parsedCommandLine = createParsedCommandLine(context.workspaces.ts, sys, uriToFileName(context.project.rootUri.toString()), context.project.tsConfig, context.workspaces.plugins);
134
+ parsedCommandLine = createParsedCommandLine(context.workspaces.ts, sys, uriToFileName(context.project.rootUri.toString()), context.project.tsConfig, context.workspaces.plugins, existingOptions);
118
135
  projectVersion++;
119
136
  typeRootVersion++;
120
137
  }
@@ -133,7 +150,10 @@ async function createProject(context) {
133
150
  // ts
134
151
  getNewLine: () => sys.newLine,
135
152
  useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
136
- readFile: sys.readFile,
153
+ readFile: fileName => {
154
+ readFiles.add(fileName);
155
+ return sys.readFile(fileName);
156
+ },
137
157
  writeFile: sys.writeFile,
138
158
  directoryExists: sys.directoryExists,
139
159
  getDirectories: sys.getDirectories,
@@ -223,17 +243,20 @@ async function createProject(context) {
223
243
  }
224
244
  }
225
245
  exports.createProject = createProject;
226
- function createParsedCommandLine(ts, sys, rootPath, tsConfig, plugins) {
246
+ function createParsedCommandLine(ts, sys, rootPath, tsConfig, plugins, existingOptions) {
227
247
  const extraFileExtensions = plugins.map(plugin => plugin.extraFileExtensions ?? []).flat();
228
248
  if (ts) {
229
249
  try {
230
250
  let content;
231
251
  if (typeof tsConfig === 'string') {
232
252
  const config = ts.readJsonConfigFile(tsConfig, sys.readFile);
233
- content = ts.parseJsonSourceFileConfigFileContent(config, sys, path.dirname(tsConfig), {}, tsConfig, undefined, extraFileExtensions);
253
+ content = ts.parseJsonSourceFileConfigFileContent(config, sys, path.dirname(tsConfig), existingOptions, tsConfig, undefined, extraFileExtensions);
234
254
  }
235
255
  else {
236
- content = ts.parseJsonConfigFileContent({ files: [] }, sys, rootPath, tsConfig, path.join(rootPath, 'jsconfig.json'), undefined, extraFileExtensions);
256
+ content = ts.parseJsonConfigFileContent({ files: [] }, sys, rootPath, {
257
+ ...tsConfig,
258
+ ...existingOptions,
259
+ }, path.join(rootPath, 'jsconfig.json'), undefined, extraFileExtensions);
237
260
  }
238
261
  // fix https://github.com/johnsoncodehk/volar/issues/1786
239
262
  // https://github.com/microsoft/TypeScript/issues/30457