@volar/language-core 2.0.0-alpha.9 → 2.0.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.
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './lib/editorFeatures';
2
- export * from './lib/fileProvider';
2
+ export * from './lib/fileRegistry';
3
3
  export * from './lib/linkedCodeMap';
4
4
  export * from './lib/types';
5
5
  export * from './lib/utils';
package/index.js CHANGED
@@ -16,7 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.resolveCommonLanguageId = void 0;
18
18
  __exportStar(require("./lib/editorFeatures"), exports);
19
- __exportStar(require("./lib/fileProvider"), exports);
19
+ __exportStar(require("./lib/fileRegistry"), exports);
20
20
  __exportStar(require("./lib/linkedCodeMap"), exports);
21
21
  __exportStar(require("./lib/types"), exports);
22
22
  __exportStar(require("./lib/utils"), exports);
@@ -0,0 +1,17 @@
1
+ import { SourceMap } from '@volar/source-map';
2
+ import type * as ts from 'typescript';
3
+ import { LinkedCodeMap } from './linkedCodeMap';
4
+ import type { CodeInformation, LanguagePlugin, SourceFile, VirtualCode } from './types';
5
+ export type FileRegistry = ReturnType<typeof createFileRegistry>;
6
+ export declare function createFileRegistry(languagePlugins: LanguagePlugin[], caseSensitive: boolean, sync: (id: string) => void): {
7
+ languagePlugins: LanguagePlugin<VirtualCode<string>>[];
8
+ set(id: string, languageId: string, snapshot: ts.IScriptSnapshot): SourceFile;
9
+ delete(id: string): void;
10
+ get(id: string): SourceFile | undefined;
11
+ getByVirtualCode(virtualCode: VirtualCode): SourceFile;
12
+ getLinkedCodeMap(virtualCode: VirtualCode): LinkedCodeMap | undefined;
13
+ getMaps(virtualCode: VirtualCode): Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
14
+ getVirtualCode(sourceFileId: string, virtualCodeId: string): readonly [VirtualCode<string>, SourceFile] | readonly [undefined, undefined];
15
+ };
16
+ export declare function updateVirtualCodeMaps(virtualCode: VirtualCode, getSourceSnapshot: (sourceUri: string | undefined) => [string, ts.IScriptSnapshot] | undefined, map?: Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>): Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
17
+ export declare function forEachEmbeddedCode(code: VirtualCode): Generator<VirtualCode>;
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.forEachEmbeddedCode = exports.updateVirtualCodeMaps = exports.createFileRegistry = void 0;
4
+ const source_map_1 = require("@volar/source-map");
5
+ const linkedCodeMap_1 = require("./linkedCodeMap");
6
+ const utils_1 = require("./utils");
7
+ function createFileRegistry(languagePlugins, caseSensitive, sync) {
8
+ const sourceFiles = new utils_1.FileMap(caseSensitive);
9
+ const virtualCodeToSourceFileMap = new WeakMap();
10
+ const virtualCodeToMaps = new WeakMap();
11
+ const virtualCodeToLinkedCodeMap = new WeakMap();
12
+ return {
13
+ languagePlugins,
14
+ set(id, languageId, snapshot) {
15
+ const value = sourceFiles.get(id);
16
+ if (value) {
17
+ if (value.languageId !== languageId) {
18
+ // languageId changed
19
+ this.delete(id);
20
+ return this.set(id, languageId, snapshot);
21
+ }
22
+ else if (value.snapshot !== snapshot) {
23
+ // snapshot updated
24
+ value.snapshot = snapshot;
25
+ if (value.generated) {
26
+ value.generated.code = value.generated.languagePlugin.updateVirtualCode(id, value.generated.code, snapshot, this);
27
+ for (const code of forEachEmbeddedCode(value.generated.code)) {
28
+ virtualCodeToSourceFileMap.set(code, value);
29
+ }
30
+ }
31
+ return value;
32
+ }
33
+ else {
34
+ // not changed
35
+ return value;
36
+ }
37
+ }
38
+ // created
39
+ const sourceFile = { id, languageId, snapshot };
40
+ sourceFiles.set(id, sourceFile);
41
+ for (const languagePlugin of languagePlugins) {
42
+ const virtualCode = languagePlugin.createVirtualCode(id, languageId, snapshot, this);
43
+ if (virtualCode) {
44
+ sourceFile.generated = {
45
+ code: virtualCode,
46
+ languagePlugin,
47
+ };
48
+ for (const code of forEachEmbeddedCode(virtualCode)) {
49
+ virtualCodeToSourceFileMap.set(code, sourceFile);
50
+ }
51
+ break;
52
+ }
53
+ }
54
+ return sourceFile;
55
+ },
56
+ delete(id) {
57
+ const value = sourceFiles.get(id);
58
+ if (value) {
59
+ if (value.generated) {
60
+ value.generated.languagePlugin.disposeVirtualCode?.(id, value.generated.code, this);
61
+ }
62
+ sourceFiles.delete(id);
63
+ }
64
+ },
65
+ get(id) {
66
+ sync(id);
67
+ return sourceFiles.get(id);
68
+ },
69
+ getByVirtualCode(virtualCode) {
70
+ return virtualCodeToSourceFileMap.get(virtualCode);
71
+ },
72
+ getLinkedCodeMap(virtualCode) {
73
+ if (!virtualCodeToLinkedCodeMap.has(virtualCode.snapshot)) {
74
+ virtualCodeToLinkedCodeMap.set(virtualCode.snapshot, virtualCode.linkedCodeMappings
75
+ ? new linkedCodeMap_1.LinkedCodeMap(virtualCode.linkedCodeMappings)
76
+ : undefined);
77
+ }
78
+ return virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
79
+ },
80
+ getMaps(virtualCode) {
81
+ if (!virtualCodeToMaps.has(virtualCode.snapshot)) {
82
+ virtualCodeToMaps.set(virtualCode.snapshot, new Map());
83
+ }
84
+ updateVirtualCodeMaps(virtualCode, sourceFileId => {
85
+ if (sourceFileId) {
86
+ const sourceFile = sourceFiles.get(sourceFileId);
87
+ return [sourceFileId, sourceFile.snapshot];
88
+ }
89
+ else {
90
+ const sourceFile = virtualCodeToSourceFileMap.get(virtualCode);
91
+ return [sourceFile.id, sourceFile.snapshot];
92
+ }
93
+ }, virtualCodeToMaps.get(virtualCode.snapshot));
94
+ return virtualCodeToMaps.get(virtualCode.snapshot);
95
+ },
96
+ getVirtualCode(sourceFileId, virtualCodeId) {
97
+ const sourceFile = this.get(sourceFileId);
98
+ if (sourceFile?.generated) {
99
+ for (const code of forEachEmbeddedCode(sourceFile.generated.code)) {
100
+ if (code.id === virtualCodeId) {
101
+ return [code, sourceFile];
102
+ }
103
+ }
104
+ }
105
+ return [undefined, undefined];
106
+ },
107
+ };
108
+ }
109
+ exports.createFileRegistry = createFileRegistry;
110
+ function updateVirtualCodeMaps(virtualCode, getSourceSnapshot, map = new Map()) {
111
+ const sources = new Set();
112
+ for (const mapping of virtualCode.mappings) {
113
+ if (sources.has(mapping.source)) {
114
+ continue;
115
+ }
116
+ sources.add(mapping.source);
117
+ const source = getSourceSnapshot(mapping.source);
118
+ if (!source) {
119
+ continue;
120
+ }
121
+ if (!map.has(source[0]) || map.get(source[0])[0] !== source[1]) {
122
+ map.set(source[0], [source[1], new source_map_1.SourceMap(virtualCode.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
123
+ }
124
+ }
125
+ return map;
126
+ }
127
+ exports.updateVirtualCodeMaps = updateVirtualCodeMaps;
128
+ function* forEachEmbeddedCode(code) {
129
+ yield code;
130
+ for (const embeddedCode of code.embeddedCodes) {
131
+ yield* forEachEmbeddedCode(embeddedCode);
132
+ }
133
+ }
134
+ exports.forEachEmbeddedCode = forEachEmbeddedCode;
135
+ //# sourceMappingURL=fileRegistry.js.map
package/lib/types.d.ts CHANGED
@@ -1,16 +1,25 @@
1
1
  import type { Mapping, Stack } from '@volar/source-map';
2
- import type * as ts from 'typescript/lib/tsserverlibrary';
3
- import type { FileProvider } from './fileProvider';
4
- export interface SourceFile extends BaseFile {
5
- virtualFile?: [VirtualFile, LanguagePlugin];
2
+ import type * as ts from 'typescript';
3
+ import type { FileRegistry } from './fileRegistry';
4
+ export interface SourceFile {
5
+ /**
6
+ * uri or fileName
7
+ */
8
+ id: string;
9
+ languageId: string;
10
+ snapshot: ts.IScriptSnapshot;
11
+ generated?: {
12
+ code: VirtualCode;
13
+ languagePlugin: LanguagePlugin;
14
+ };
6
15
  }
7
16
  export type CodeMapping = Mapping<CodeInformation>;
8
- export interface VirtualFile extends BaseFile {
17
+ export interface VirtualCode<T extends string = string> {
18
+ id: T;
19
+ languageId: string;
20
+ snapshot: ts.IScriptSnapshot;
9
21
  mappings: CodeMapping[];
10
- embeddedFiles: VirtualFile[];
11
- typescript?: {
12
- scriptKind: ts.ScriptKind;
13
- };
22
+ embeddedCodes: VirtualCode[];
14
23
  codegenStacks?: Stack[];
15
24
  linkedCodeMappings?: Mapping[];
16
25
  }
@@ -39,30 +48,47 @@ export interface CodeInformation {
39
48
  /** virtual code is expected correctly reflect the format information of the source code */
40
49
  format: boolean;
41
50
  }
42
- export interface BaseFile {
51
+ export interface ServiceScript {
52
+ code: VirtualCode;
53
+ extension: '.ts' | '.js' | '.mts' | '.mjs' | '.cjs' | '.cts' | '.d.ts' | string;
54
+ scriptKind: ts.ScriptKind;
55
+ }
56
+ export interface ExtraServiceScript extends ServiceScript {
43
57
  fileName: string;
44
- languageId: string;
45
- snapshot: ts.IScriptSnapshot;
46
58
  }
47
- export interface LanguagePlugin<T extends VirtualFile = VirtualFile> {
48
- createVirtualFile(id: string, languageId: string, snapshot: ts.IScriptSnapshot, files?: FileProvider): T | undefined;
49
- updateVirtualFile(virtualFile: T, snapshot: ts.IScriptSnapshot, files?: FileProvider): void;
50
- disposeVirtualFile?(virtualFile: T, files?: FileProvider): void;
59
+ export interface LanguagePlugin<T extends VirtualCode = VirtualCode> {
60
+ createVirtualCode(fileId: string, languageId: string, snapshot: ts.IScriptSnapshot, files?: FileRegistry): T | undefined;
61
+ updateVirtualCode(fileId: string, virtualCode: T, newSnapshot: ts.IScriptSnapshot, files?: FileRegistry): T;
62
+ disposeVirtualCode?(fileId: string, virtualCode: T, files?: FileRegistry): void;
51
63
  typescript?: {
64
+ /**
65
+ * LSP + TS Plugin
66
+ */
52
67
  extraFileExtensions: ts.FileExtensionInfo[];
53
- resolveSourceFileName(tsFileName: string): string | undefined;
54
- resolveModuleName?(path: string, impliedNodeFormat?: ts.ResolutionMode): string | undefined;
68
+ /**
69
+ * LSP + TS Plugin
70
+ */
71
+ getScript(rootVirtualCode: T): ServiceScript | undefined;
72
+ /**
73
+ * LSP only
74
+ */
75
+ getExtraScripts?(fileName: string, rootVirtualCode: T): ExtraServiceScript[];
76
+ /**
77
+ * LSP only
78
+ */
55
79
  resolveLanguageServiceHost?(host: ts.LanguageServiceHost): ts.LanguageServiceHost;
56
80
  };
57
81
  }
58
- export interface Language {
59
- files: FileProvider;
82
+ export interface LanguageContext {
83
+ files: FileRegistry;
60
84
  typescript?: {
61
85
  configFileName: string | undefined;
62
- sys: ts.System;
86
+ sys: ts.System & {
87
+ sync?(): Promise<number>;
88
+ };
63
89
  projectHost: TypeScriptProjectHost;
64
90
  languageServiceHost: ts.LanguageServiceHost;
65
- synchronizeFileSystem?(): Promise<number>;
91
+ getExtraScript(fileName: string): ExtraServiceScript | undefined;
66
92
  };
67
93
  }
68
94
  export interface TypeScriptProjectHost extends Pick<ts.LanguageServiceHost, 'getLocalizedDiagnosticMessages' | 'getCompilationSettings' | 'getProjectReferences' | 'getCurrentDirectory' | 'getScriptFileNames' | 'getProjectVersion' | 'getScriptSnapshot'> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-core",
3
- "version": "2.0.0-alpha.9",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/language-core"
13
13
  },
14
14
  "dependencies": {
15
- "@volar/source-map": "2.0.0-alpha.9"
15
+ "@volar/source-map": "2.0.1"
16
16
  },
17
- "gitHead": "dcd70c78b0b6901901dc0e68223e34dd922f5fdc"
17
+ "gitHead": "b1fbf6eed522624ccccfb17a8999edfbc1d8d9bb"
18
18
  }
@@ -1,15 +0,0 @@
1
- import { SourceMap } from '@volar/source-map';
2
- import type * as ts from 'typescript/lib/tsserverlibrary';
3
- import { LinkedCodeMap } from './linkedCodeMap';
4
- import type { CodeInformation, LanguagePlugin, SourceFile, VirtualFile } from './types';
5
- export type FileProvider = ReturnType<typeof createFileProvider>;
6
- export declare function createFileProvider(languages: LanguagePlugin[], caseSensitive: boolean, sync: (sourceFileName: string) => void): {
7
- updateSourceFile(fileName: string, languageId: string, snapshot: ts.IScriptSnapshot): SourceFile;
8
- deleteSourceFile(fileName: string): void;
9
- getLinkedCodeMap(file: VirtualFile): LinkedCodeMap | undefined;
10
- getMaps(virtualFile: VirtualFile): Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
11
- getSourceFile(fileName: string): SourceFile | undefined;
12
- getVirtualFile(fileName: string): [VirtualFile, SourceFile] | readonly [undefined, undefined];
13
- };
14
- export declare function updateVirtualFileMaps(virtualFile: VirtualFile, getSourceSnapshot: (sourceUri: string | undefined) => [string, ts.IScriptSnapshot] | undefined, map?: Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>): Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>;
15
- export declare function forEachEmbeddedFile(file: VirtualFile): Generator<VirtualFile>;
@@ -1,142 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.forEachEmbeddedFile = exports.updateVirtualFileMaps = exports.createFileProvider = void 0;
4
- const source_map_1 = require("@volar/source-map");
5
- const linkedCodeMap_1 = require("./linkedCodeMap");
6
- const utils_1 = require("./utils");
7
- function createFileProvider(languages, caseSensitive, sync) {
8
- const sourceFileRegistry = new utils_1.FileMap(caseSensitive);
9
- const virtualFileRegistry = new utils_1.FileMap(caseSensitive);
10
- const virtualFileToMaps = new WeakMap();
11
- const virtualFileToLinkedCodeMap = new WeakMap();
12
- return {
13
- updateSourceFile(fileName, languageId, snapshot) {
14
- const value = sourceFileRegistry.get(fileName);
15
- if (value) {
16
- if (value.languageId !== languageId) {
17
- // languageId changed
18
- this.deleteSourceFile(fileName);
19
- return this.updateSourceFile(fileName, languageId, snapshot);
20
- }
21
- else if (value.snapshot !== snapshot) {
22
- // updated
23
- value.snapshot = snapshot;
24
- if (value.virtualFile) {
25
- disposeVirtualFiles(value);
26
- value.virtualFile[1].updateVirtualFile(value.virtualFile[0], snapshot, this);
27
- updateVirtualFiles(value);
28
- }
29
- return value;
30
- }
31
- else {
32
- // not changed
33
- return value;
34
- }
35
- }
36
- for (const language of languages) {
37
- const virtualFile = language.createVirtualFile(fileName, languageId, snapshot, this);
38
- if (virtualFile) {
39
- // created
40
- const source = {
41
- fileName,
42
- languageId,
43
- snapshot,
44
- virtualFile: [virtualFile, language],
45
- };
46
- sourceFileRegistry.set(fileName, source);
47
- updateVirtualFiles(source);
48
- return source;
49
- }
50
- }
51
- const source = { fileName, languageId, snapshot };
52
- sourceFileRegistry.set(fileName, source);
53
- return source;
54
- },
55
- deleteSourceFile(fileName) {
56
- const value = sourceFileRegistry.get(fileName);
57
- if (value) {
58
- if (value.virtualFile) {
59
- value.virtualFile[1].disposeVirtualFile?.(value.virtualFile[0], this);
60
- }
61
- sourceFileRegistry.delete(fileName); // deleted
62
- disposeVirtualFiles(value);
63
- }
64
- },
65
- getLinkedCodeMap(file) {
66
- if (!virtualFileToLinkedCodeMap.has(file.snapshot)) {
67
- virtualFileToLinkedCodeMap.set(file.snapshot, file.linkedCodeMappings ? new linkedCodeMap_1.LinkedCodeMap(file.linkedCodeMappings) : undefined);
68
- }
69
- return virtualFileToLinkedCodeMap.get(file.snapshot);
70
- },
71
- getMaps(virtualFile) {
72
- if (!virtualFileToMaps.has(virtualFile.snapshot)) {
73
- virtualFileToMaps.set(virtualFile.snapshot, new Map());
74
- }
75
- updateVirtualFileMaps(virtualFile, sourceId => {
76
- if (sourceId) {
77
- const sourceFile = sourceFileRegistry.get(sourceId);
78
- return [sourceId, sourceFile.snapshot];
79
- }
80
- else {
81
- const sourceFile = virtualFileRegistry.get(virtualFile.fileName)[1];
82
- return [sourceFile.fileName, sourceFile.snapshot];
83
- }
84
- }, virtualFileToMaps.get(virtualFile.snapshot));
85
- return virtualFileToMaps.get(virtualFile.snapshot);
86
- },
87
- getSourceFile(fileName) {
88
- sync(fileName);
89
- return sourceFileRegistry.get(fileName);
90
- },
91
- getVirtualFile(fileName) {
92
- let sourceAndVirtual = virtualFileRegistry.get(fileName);
93
- if (sourceAndVirtual) {
94
- sync(sourceAndVirtual[1].fileName);
95
- sourceAndVirtual = virtualFileRegistry.get(fileName);
96
- if (sourceAndVirtual) {
97
- return sourceAndVirtual;
98
- }
99
- }
100
- return [undefined, undefined];
101
- },
102
- };
103
- function disposeVirtualFiles(source) {
104
- if (source.virtualFile) {
105
- for (const file of forEachEmbeddedFile(source.virtualFile[0])) {
106
- virtualFileRegistry.delete(file.fileName);
107
- }
108
- }
109
- }
110
- function updateVirtualFiles(source) {
111
- if (source.virtualFile) {
112
- for (const file of forEachEmbeddedFile(source.virtualFile[0])) {
113
- virtualFileRegistry.set(file.fileName, [file, source]);
114
- }
115
- }
116
- }
117
- }
118
- exports.createFileProvider = createFileProvider;
119
- function updateVirtualFileMaps(virtualFile, getSourceSnapshot, map = new Map()) {
120
- const sources = new Set();
121
- for (const mapping of virtualFile.mappings) {
122
- if (sources.has(mapping.source))
123
- continue;
124
- sources.add(mapping.source);
125
- const source = getSourceSnapshot(mapping.source);
126
- if (!source)
127
- continue;
128
- if (!map.has(source[0]) || map.get(source[0])[0] !== source[1]) {
129
- map.set(source[0], [source[1], new source_map_1.SourceMap(virtualFile.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
130
- }
131
- }
132
- return map;
133
- }
134
- exports.updateVirtualFileMaps = updateVirtualFileMaps;
135
- function* forEachEmbeddedFile(file) {
136
- yield file;
137
- for (const embeddedFile of file.embeddedFiles) {
138
- yield* forEachEmbeddedFile(embeddedFile);
139
- }
140
- }
141
- exports.forEachEmbeddedFile = forEachEmbeddedFile;
142
- //# sourceMappingURL=fileProvider.js.map