@volar/language-server 2.4.0-alpha.9 → 2.4.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.
Files changed (44) hide show
  1. package/browser.d.ts +43 -21
  2. package/browser.js +16 -31
  3. package/lib/features/configurations.d.ts +8 -0
  4. package/lib/features/configurations.js +49 -0
  5. package/lib/features/editorFeatures.d.ts +2 -0
  6. package/lib/features/editorFeatures.js +186 -0
  7. package/lib/features/fileSystem.d.ts +8 -0
  8. package/lib/features/fileSystem.js +61 -0
  9. package/lib/features/fileWatcher.d.ts +9 -0
  10. package/lib/features/fileWatcher.js +63 -0
  11. package/lib/features/languageFeatures.d.ts +4 -0
  12. package/lib/features/languageFeatures.js +730 -0
  13. package/lib/features/textDocuments.d.ts +12 -0
  14. package/lib/features/textDocuments.js +54 -0
  15. package/lib/features/workspaceFolders.d.ts +10 -0
  16. package/lib/features/workspaceFolders.js +67 -0
  17. package/lib/fileSystemProviders/http.d.ts +6 -0
  18. package/lib/fileSystemProviders/http.js +46 -0
  19. package/lib/fileSystemProviders/node.d.ts +2 -0
  20. package/lib/fileSystemProviders/node.js +49 -0
  21. package/lib/project/inferredCompilerOptions.js +2 -2
  22. package/lib/project/simpleProject.js +7 -8
  23. package/lib/project/typescriptProject.d.ts +7 -4
  24. package/lib/project/typescriptProject.js +49 -40
  25. package/lib/project/typescriptProjectLs.d.ts +8 -7
  26. package/lib/project/typescriptProjectLs.js +140 -85
  27. package/lib/server.d.ts +46 -27
  28. package/lib/server.js +59 -479
  29. package/lib/types.d.ts +20 -5
  30. package/lib/utils/combineChangeRanges.d.ts +2 -0
  31. package/lib/utils/combineChangeRanges.js +28 -0
  32. package/lib/utils/snapshotDocument.d.ts +28 -0
  33. package/lib/utils/snapshotDocument.js +138 -0
  34. package/node.d.ts +43 -23
  35. package/node.js +16 -58
  36. package/package.json +5 -6
  37. package/protocol.d.ts +1 -13
  38. package/protocol.js +1 -16
  39. package/lib/register/registerEditorFeatures.d.ts +0 -2
  40. package/lib/register/registerEditorFeatures.js +0 -208
  41. package/lib/register/registerLanguageFeatures.d.ts +0 -3
  42. package/lib/register/registerLanguageFeatures.js +0 -435
  43. package/lib/schemaRequestHandlers/http.d.ts +0 -2
  44. package/lib/schemaRequestHandlers/http.js +0 -16
@@ -3,41 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTypeScriptLS = createTypeScriptLS;
4
4
  const language_service_1 = require("@volar/language-service");
5
5
  const typescript_1 = require("@volar/typescript");
6
+ const utilities_1 = require("@volar/typescript/lib/typescript/utilities");
6
7
  const path = require("path-browserify");
7
8
  const vscode = require("vscode-languageserver");
8
9
  const vscode_uri_1 = require("vscode-uri");
9
10
  const fsFileSnapshots = (0, language_service_1.createUriMap)();
10
- async function createTypeScriptLS(ts, tsLocalized, tsconfig, server, serviceEnv, workspaceFolder, { asUri, asFileName }, create) {
11
- let parsedCommandLine;
11
+ async function createTypeScriptLS(ts, tsLocalized, tsconfig, server, serviceEnv, workspaceFolder, uriConverter, create) {
12
+ let commandLine;
12
13
  let projectVersion = 0;
13
- const getCurrentDirectory = () => asFileName(workspaceFolder);
14
- const sys = (0, typescript_1.createSys)(ts.sys, serviceEnv, getCurrentDirectory, {
15
- asFileName,
16
- asUri,
17
- });
14
+ const getCurrentDirectory = () => uriConverter.asFileName(workspaceFolder);
15
+ const sys = (0, typescript_1.createSys)(ts.sys, serviceEnv, getCurrentDirectory, uriConverter);
18
16
  const projectHost = {
19
17
  getCurrentDirectory,
20
18
  getProjectVersion() {
21
19
  return projectVersion.toString();
22
20
  },
23
21
  getScriptFileNames() {
24
- return rootFiles;
25
- },
26
- getScriptSnapshot(fileName) {
27
- const uri = asUri(fileName);
28
- const documentKey = server.getSyncedDocumentKey(uri) ?? uri.toString();
29
- const document = server.documents.get(documentKey);
30
- askedFiles.set(uri, true);
31
- if (document) {
32
- return document.getSnapshot();
33
- }
22
+ return commandLine.fileNames;
34
23
  },
35
24
  getCompilationSettings() {
36
- return parsedCommandLine.options;
25
+ return commandLine.options;
37
26
  },
38
27
  getLocalizedDiagnosticMessages: tsLocalized ? () => tsLocalized : undefined,
39
28
  getProjectReferences() {
40
- return parsedCommandLine.projectReferences;
29
+ return commandLine.projectReferences;
41
30
  },
42
31
  };
43
32
  const { languagePlugins, setup } = await create({
@@ -45,31 +34,58 @@ async function createTypeScriptLS(ts, tsLocalized, tsconfig, server, serviceEnv,
45
34
  configFileName: typeof tsconfig === 'string' ? tsconfig : undefined,
46
35
  projectHost,
47
36
  sys,
48
- asFileName,
49
- asUri,
37
+ uriConverter,
50
38
  });
51
- const askedFiles = (0, language_service_1.createUriMap)();
52
- const docOpenWatcher = server.documents.onDidOpen(({ document }) => updateFsCacheFromSyncedDocument(document));
53
- const docSaveWatcher = server.documents.onDidSave(({ document }) => updateFsCacheFromSyncedDocument(document));
54
- const docChangeWatcher = server.documents.onDidChangeContent(() => projectVersion++);
55
- const fileWatch = serviceEnv.onDidChangeWatchedFiles?.(params => onWorkspaceFilesChanged(params.changes));
56
- let rootFiles = await getRootFiles(languagePlugins);
39
+ const unsavedRootFileUris = (0, language_service_1.createUriMap)();
40
+ const disposables = [
41
+ server.documents.onDidOpen(({ document }) => updateFsCacheFromSyncedDocument(document)),
42
+ server.documents.onDidSave(({ document }) => updateFsCacheFromSyncedDocument(document)),
43
+ server.documents.onDidChangeContent(() => projectVersion++),
44
+ serviceEnv.onDidChangeWatchedFiles?.(async ({ changes }) => {
45
+ const createdOrDeleted = changes.some(change => change.type !== vscode.FileChangeType.Changed);
46
+ if (createdOrDeleted) {
47
+ await updateCommandLine();
48
+ }
49
+ projectVersion++;
50
+ }),
51
+ server.documents.onDidOpen(async ({ document }) => {
52
+ const uri = vscode_uri_1.URI.parse(document.uri);
53
+ const isWorkspaceFile = workspaceFolder.scheme === uri.scheme;
54
+ if (!isWorkspaceFile) {
55
+ return;
56
+ }
57
+ const stat = await serviceEnv.fs?.stat(uri);
58
+ const isUnsaved = stat?.type !== 1;
59
+ if (isUnsaved) {
60
+ const lastProjectVersion = projectVersion;
61
+ await updateCommandLine();
62
+ if (lastProjectVersion !== projectVersion) {
63
+ unsavedRootFileUris.set(uri, true);
64
+ }
65
+ }
66
+ }),
67
+ server.documents.onDidClose(async ({ document }) => {
68
+ const uri = vscode_uri_1.URI.parse(document.uri);
69
+ if (unsavedRootFileUris.has(uri)) {
70
+ unsavedRootFileUris.delete(uri);
71
+ await updateCommandLine();
72
+ }
73
+ }),
74
+ ].filter(d => !!d);
75
+ await updateCommandLine();
57
76
  const language = (0, language_service_1.createLanguage)([
58
- { getLanguageId: uri => server.documents.get(server.getSyncedDocumentKey(uri) ?? uri.toString())?.languageId },
77
+ { getLanguageId: uri => server.documents.get(uri)?.languageId },
59
78
  ...languagePlugins,
60
79
  { getLanguageId: uri => (0, typescript_1.resolveFileLanguageId)(uri.path) },
61
- ], (0, language_service_1.createUriMap)(sys.useCaseSensitiveFileNames), uri => {
62
- askedFiles.set(uri, true);
63
- const documentUri = server.getSyncedDocumentKey(uri);
64
- const syncedDocument = documentUri ? server.documents.get(documentUri) : undefined;
80
+ ], (0, language_service_1.createUriMap)(sys.useCaseSensitiveFileNames), (uri, includeFsFiles) => {
81
+ const syncedDocument = server.documents.get(uri);
65
82
  let snapshot;
66
83
  if (syncedDocument) {
67
84
  snapshot = syncedDocument.getSnapshot();
68
85
  }
69
- else {
70
- // fs files
86
+ else if (includeFsFiles) {
71
87
  const cache = fsFileSnapshots.get(uri);
72
- const fileName = asFileName(uri);
88
+ const fileName = uriConverter.asFileName(uri);
73
89
  const modifiedTime = sys.getModifiedTime?.(fileName)?.valueOf();
74
90
  if (!cache || cache[0] !== modifiedTime) {
75
91
  if (sys.fileExists(fileName)) {
@@ -94,84 +110,123 @@ async function createTypeScriptLS(ts, tsLocalized, tsconfig, server, serviceEnv,
94
110
  typescript: {
95
111
  configFileName: typeof tsconfig === 'string' ? tsconfig : undefined,
96
112
  sys,
97
- asUri,
98
- asFileName,
99
- ...(0, typescript_1.createLanguageServiceHost)(ts, sys, language, asUri, projectHost),
113
+ uriConverter,
114
+ ...(0, typescript_1.createLanguageServiceHost)(ts, sys, language, s => uriConverter.asUri(s), projectHost),
100
115
  },
101
116
  };
102
- setup({ language, project });
117
+ setup?.({ language, project });
103
118
  const languageService = (0, language_service_1.createLanguageService)(language, server.languageServicePlugins, serviceEnv, project);
104
119
  return {
105
- askedFiles,
106
120
  languageService,
107
121
  tryAddFile(fileName) {
108
- if (!rootFiles.includes(fileName)) {
109
- rootFiles.push(fileName);
122
+ if (!commandLine.fileNames.includes(fileName)) {
123
+ commandLine.fileNames.push(fileName);
110
124
  projectVersion++;
111
125
  }
112
126
  },
113
127
  dispose: () => {
114
128
  sys.dispose();
115
129
  languageService?.dispose();
116
- fileWatch?.dispose();
117
- docOpenWatcher.dispose();
118
- docSaveWatcher.dispose();
119
- docChangeWatcher.dispose();
130
+ disposables.forEach(({ dispose }) => dispose());
131
+ disposables.length = 0;
120
132
  },
121
- getParsedCommandLine: () => parsedCommandLine,
133
+ getCommandLine: () => commandLine,
122
134
  };
123
135
  function updateFsCacheFromSyncedDocument(document) {
124
136
  const uri = vscode_uri_1.URI.parse(document.uri);
125
- const fileName = asFileName(uri);
137
+ const fileName = uriConverter.asFileName(uri);
126
138
  if (fsFileSnapshots.has(uri) || sys.fileExists(fileName)) {
127
139
  const modifiedTime = sys.getModifiedTime?.(fileName);
128
140
  fsFileSnapshots.set(uri, [modifiedTime?.valueOf(), document.getSnapshot()]);
129
141
  }
130
142
  }
131
- async function getRootFiles(languagePlugins) {
132
- parsedCommandLine = await createParsedCommandLine(ts, sys, asFileName(workspaceFolder), tsconfig, languagePlugins.map(plugin => plugin.typescript?.extraFileExtensions ?? []).flat());
133
- return parsedCommandLine.fileNames;
134
- }
135
- async function onWorkspaceFilesChanged(changes) {
136
- const createsAndDeletes = changes.filter(change => change.type !== vscode.FileChangeType.Changed);
137
- if (createsAndDeletes.length) {
138
- rootFiles = await getRootFiles(languagePlugins);
143
+ async function updateCommandLine() {
144
+ const oldFileNames = new Set(commandLine?.fileNames ?? []);
145
+ commandLine = await parseConfig(ts, sys, uriConverter.asFileName(workspaceFolder), tsconfig, languagePlugins.map(plugin => plugin.typescript?.extraFileExtensions ?? []).flat());
146
+ const newFileNames = new Set(commandLine.fileNames);
147
+ if (oldFileNames.size !== newFileNames.size || [...oldFileNames].some(fileName => !newFileNames.has(fileName))) {
148
+ projectVersion++;
139
149
  }
140
- projectVersion++;
141
150
  }
142
- }
143
- async function createParsedCommandLine(ts, sys, workspacePath, tsconfig, extraFileExtensions) {
144
- let content = {
145
- errors: [],
146
- fileNames: [],
147
- options: {},
148
- };
149
- let sysVersion;
150
- let newSysVersion = await sys.sync();
151
- while (sysVersion !== newSysVersion) {
152
- sysVersion = newSysVersion;
153
- try {
154
- if (typeof tsconfig === 'string') {
155
- const config = ts.readJsonConfigFile(tsconfig, sys.readFile);
156
- content = ts.parseJsonSourceFileConfigFileContent(config, sys, path.dirname(tsconfig), {}, tsconfig, undefined, extraFileExtensions);
151
+ async function parseConfig(ts, sys, workspacePath, tsconfig, extraFileExtensions) {
152
+ let commandLine = {
153
+ errors: [],
154
+ fileNames: [],
155
+ options: {},
156
+ };
157
+ let sysVersion;
158
+ let newSysVersion = await sys.sync();
159
+ while (sysVersion !== newSysVersion) {
160
+ sysVersion = newSysVersion;
161
+ try {
162
+ commandLine = await parseConfigWorker(ts, sys, workspacePath, tsconfig, extraFileExtensions);
157
163
  }
158
- else {
159
- content = ts.parseJsonConfigFileContent({ files: [] }, sys, workspacePath, tsconfig, workspacePath + '/jsconfig.json', undefined, extraFileExtensions);
164
+ catch {
165
+ // will be failed if web fs host first result not ready
160
166
  }
161
- // fix https://github.com/johnsoncodehk/volar/issues/1786
162
- // https://github.com/microsoft/TypeScript/issues/30457
163
- // patching ts server broke with outDir + rootDir + composite/incremental
164
- content.options.outDir = undefined;
165
- content.fileNames = content.fileNames.map(fileName => fileName.replace(/\\/g, '/'));
166
- }
167
- catch {
168
- // will be failed if web fs host first result not ready
167
+ newSysVersion = await sys.sync();
169
168
  }
170
- newSysVersion = await sys.sync();
169
+ return commandLine;
171
170
  }
172
- if (content) {
171
+ function parseConfigWorker(ts, _host, workspacePath, tsconfig, extraFileExtensions) {
172
+ let content = {
173
+ errors: [],
174
+ fileNames: [],
175
+ options: {},
176
+ };
177
+ const maybeUnsavedFileNames = server.documents.all()
178
+ .map(document => vscode_uri_1.URI.parse(document.uri))
179
+ .filter(uri => uri.scheme === workspaceFolder.scheme)
180
+ .map(uri => uriConverter.asFileName(uri));
181
+ const host = {
182
+ ..._host,
183
+ readDirectory(rootDir, extensions, excludes, includes, depth) {
184
+ const fsFiles = _host.readDirectory(rootDir, extensions, excludes, includes, depth);
185
+ const unsavedFiles = (0, utilities_1.matchFiles)(rootDir, extensions, excludes, includes, sys.useCaseSensitiveFileNames, getCurrentDirectory(), depth, dirPath => {
186
+ dirPath = dirPath.replace(/\\/g, '/');
187
+ const files = [];
188
+ const dirs = [];
189
+ for (const fileName of maybeUnsavedFileNames) {
190
+ const match = sys.useCaseSensitiveFileNames
191
+ ? fileName.startsWith(dirPath + '/')
192
+ : fileName.toLowerCase().startsWith(dirPath.toLowerCase() + '/');
193
+ if (match) {
194
+ const name = fileName.slice(dirPath.length + 1);
195
+ if (name.includes('/')) {
196
+ const dir = name.split('/')[0];
197
+ if (!dirs.includes(dir)) {
198
+ dirs.push(dir);
199
+ }
200
+ }
201
+ else {
202
+ files.push(name);
203
+ }
204
+ }
205
+ }
206
+ return {
207
+ files,
208
+ directories: dirs,
209
+ };
210
+ }, path => path);
211
+ if (!unsavedFiles.length) {
212
+ return fsFiles;
213
+ }
214
+ return [...new Set([...fsFiles, ...unsavedFiles])];
215
+ },
216
+ };
217
+ if (typeof tsconfig === 'string') {
218
+ const config = ts.readJsonConfigFile(tsconfig, host.readFile);
219
+ content = ts.parseJsonSourceFileConfigFileContent(config, host, path.dirname(tsconfig), {}, tsconfig, undefined, extraFileExtensions);
220
+ }
221
+ else {
222
+ content = ts.parseJsonConfigFileContent({ files: [] }, host, workspacePath, tsconfig, workspacePath + '/jsconfig.json', undefined, extraFileExtensions);
223
+ }
224
+ // fix https://github.com/johnsoncodehk/volar/issues/1786
225
+ // https://github.com/microsoft/TypeScript/issues/30457
226
+ // patching ts server broke with outDir + rootDir + composite/incremental
227
+ content.options.outDir = undefined;
228
+ content.fileNames = content.fileNames.map(fileName => fileName.replace(/\\/g, '/'));
173
229
  return content;
174
230
  }
175
- return content;
176
231
  }
177
232
  //# sourceMappingURL=typescriptProjectLs.js.map
package/lib/server.d.ts CHANGED
@@ -1,33 +1,52 @@
1
- import { Disposable, FileSystem, LanguageServicePlugin } from '@volar/language-service';
2
- import { SnapshotDocument } from '@volar/snapshot-document';
1
+ import { LanguageServicePlugin } from '@volar/language-service';
3
2
  import * as vscode from 'vscode-languageserver';
4
- import { URI } from 'vscode-uri';
5
- import type { LanguageServerProject, VolarInitializeResult } from './types.js';
6
- export * from '@volar/snapshot-document';
7
- export declare function createServerBase(connection: vscode.Connection, fs: FileSystem): {
8
- connection: vscode.Connection;
9
- fs: FileSystem;
3
+ import type { ExperimentalFeatures, LanguageServerEnvironment, LanguageServerProject } from './types.js';
4
+ export declare function createServerBase(connection: vscode.Connection, env: LanguageServerEnvironment): {
10
5
  initializeParams: vscode.InitializeParams;
11
- initializeResult: VolarInitializeResult;
12
- languageServicePlugins: LanguageServicePlugin[];
13
6
  project: LanguageServerProject;
14
- pullModelDiagnostics: boolean;
15
- documents: vscode.TextDocuments<SnapshotDocument>;
16
- workspaceFolders: Map<URI, boolean>;
17
- getSyncedDocumentKey: (uri: URI) => string | undefined;
18
- initialize: (initializeParams: vscode.InitializeParams, project: LanguageServerProject, languageServicePlugins: LanguageServicePlugin[], options?: {
19
- pullModelDiagnostics?: boolean;
20
- }) => VolarInitializeResult;
21
- initialized: () => void;
22
- shutdown: () => void;
23
- watchFiles: (patterns: string[]) => Promise<Disposable>;
24
- getConfiguration: <T>(section: string, scopeUri?: string) => Promise<T | undefined>;
25
- onDidChangeConfiguration: (cb: vscode.NotificationHandler<vscode.DidChangeConfigurationParams>) => {
26
- dispose(): void;
7
+ languageServicePlugins: LanguageServicePlugin<any>[];
8
+ initialize(params: vscode.InitializeParams, project: LanguageServerProject, languageServicePlugins: LanguageServicePlugin[]): vscode.InitializeResult<ExperimentalFeatures>;
9
+ initialized(): void;
10
+ shutdown(): void;
11
+ configurations: {
12
+ get: <T>(section: string, scopeUri?: string) => Promise<T | undefined>;
13
+ onDidChange: (cb: vscode.NotificationHandler<vscode.DidChangeConfigurationParams>) => {
14
+ dispose(): void;
15
+ };
16
+ };
17
+ editorFeatures: void;
18
+ documents: {
19
+ all: () => import("./utils/snapshotDocument.js").SnapshotDocument[];
20
+ onDidChangeContent: vscode.Event<vscode.TextDocumentChangeEvent<import("./utils/snapshotDocument.js").SnapshotDocument>>;
21
+ onDidOpen: vscode.Event<vscode.TextDocumentChangeEvent<import("./utils/snapshotDocument.js").SnapshotDocument>>;
22
+ onDidClose: vscode.Event<vscode.TextDocumentChangeEvent<import("./utils/snapshotDocument.js").SnapshotDocument>>;
23
+ onDidSave: vscode.Event<vscode.TextDocumentChangeEvent<import("./utils/snapshotDocument.js").SnapshotDocument>>;
24
+ get(uri: import("vscode-uri").URI): import("./utils/snapshotDocument.js").SnapshotDocument | undefined;
25
+ };
26
+ workspaceFolders: {
27
+ readonly all: import("vscode-uri").URI[];
28
+ has(uri: import("vscode-uri").URI): boolean;
29
+ onDidChange: (cb: vscode.NotificationHandler<vscode.WorkspaceFoldersChangeEvent>) => {
30
+ dispose(): void;
31
+ };
27
32
  };
28
- onDidChangeWatchedFiles: (cb: vscode.NotificationHandler<vscode.DidChangeWatchedFilesParams>) => {
29
- dispose: () => void;
33
+ fileWatcher: {
34
+ watchFiles: (patterns: string[]) => Promise<vscode.Disposable>;
35
+ onDidChangeWatchedFiles: (cb: vscode.NotificationHandler<vscode.DidChangeWatchedFilesParams>) => {
36
+ dispose: () => void;
37
+ };
30
38
  };
31
- clearPushDiagnostics: () => void;
32
- refresh: (project: LanguageServerProject) => Promise<void>;
39
+ languageFeatures: {
40
+ requestRefresh: (clearDiagnostics: boolean) => Promise<void>;
41
+ };
42
+ fileSystem: {
43
+ readFile(uri: import("vscode-uri").URI): string | Thenable<string | undefined>;
44
+ stat(uri: import("vscode-uri").URI): import("@volar/language-service").FileStat | Thenable<import("@volar/language-service").FileStat | undefined>;
45
+ readDirectory(uri: import("vscode-uri").URI): import("@volar/language-service").ProviderResult<[string, import("@volar/language-service").FileType][]>;
46
+ install(scheme: string, provider: import("@volar/language-service").FileSystem): void;
47
+ };
48
+ env: LanguageServerEnvironment;
49
+ connection: vscode.Connection;
50
+ onInitialize(callback: (serverCapabilities: vscode.ServerCapabilities<ExperimentalFeatures>) => void): void;
51
+ onInitialized(callback: () => void): void;
33
52
  };