@volar/language-core 2.0.0-alpha.13 → 2.0.0-alpha.14

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,138 @@
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
+ value.generated.idToFileMap.clear();
28
+ for (const file of forEachEmbeddedCode(value.generated.code)) {
29
+ value.generated.idToFileMap.set(file.id, file);
30
+ virtualCodeToSourceFileMap.set(file, value);
31
+ }
32
+ }
33
+ return value;
34
+ }
35
+ else {
36
+ // not changed
37
+ return value;
38
+ }
39
+ }
40
+ // created
41
+ const sourceFile = { id, languageId, snapshot };
42
+ sourceFiles.set(id, sourceFile);
43
+ for (const languagePlugin of languagePlugins) {
44
+ const virtualCode = languagePlugin.createVirtualCode(id, languageId, snapshot, this);
45
+ if (virtualCode) {
46
+ sourceFile.generated = {
47
+ code: virtualCode,
48
+ languagePlugin,
49
+ idToFileMap: new Map(),
50
+ };
51
+ for (const file of forEachEmbeddedCode(virtualCode)) {
52
+ sourceFile.generated.idToFileMap.set(file.id, file);
53
+ virtualCodeToSourceFileMap.set(file, sourceFile);
54
+ }
55
+ break;
56
+ }
57
+ }
58
+ return sourceFile;
59
+ },
60
+ delete(id) {
61
+ const value = sourceFiles.get(id);
62
+ if (value) {
63
+ if (value.generated) {
64
+ value.generated.languagePlugin.disposeVirtualCode?.(id, value.generated.code, this);
65
+ }
66
+ sourceFiles.delete(id);
67
+ }
68
+ },
69
+ get(id) {
70
+ sync(id);
71
+ return sourceFiles.get(id);
72
+ },
73
+ getByVirtualCode(virtualCode) {
74
+ return virtualCodeToSourceFileMap.get(virtualCode);
75
+ },
76
+ getLinkedCodeMap(virtualCode) {
77
+ if (!virtualCodeToLinkedCodeMap.has(virtualCode.snapshot)) {
78
+ virtualCodeToLinkedCodeMap.set(virtualCode.snapshot, virtualCode.linkedCodeMappings
79
+ ? new linkedCodeMap_1.LinkedCodeMap(virtualCode.linkedCodeMappings)
80
+ : undefined);
81
+ }
82
+ return virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
83
+ },
84
+ getMaps(virtualCode) {
85
+ if (!virtualCodeToMaps.has(virtualCode.snapshot)) {
86
+ virtualCodeToMaps.set(virtualCode.snapshot, new Map());
87
+ }
88
+ updateVirtualCodeMaps(virtualCode, sourceFileId => {
89
+ if (sourceFileId) {
90
+ const sourceFile = sourceFiles.get(sourceFileId);
91
+ return [sourceFileId, sourceFile.snapshot];
92
+ }
93
+ else {
94
+ const sourceFile = virtualCodeToSourceFileMap.get(virtualCode);
95
+ return [sourceFile.id, sourceFile.snapshot];
96
+ }
97
+ }, virtualCodeToMaps.get(virtualCode.snapshot));
98
+ return virtualCodeToMaps.get(virtualCode.snapshot);
99
+ },
100
+ getVirtualCode(sourceFileId, virtualCodeId) {
101
+ const sourceFile = this.get(sourceFileId);
102
+ if (sourceFile) {
103
+ const virtualCode = sourceFile.generated?.idToFileMap.get(virtualCodeId);
104
+ if (virtualCode) {
105
+ return [virtualCode, sourceFile];
106
+ }
107
+ }
108
+ return [undefined, undefined];
109
+ },
110
+ };
111
+ }
112
+ exports.createFileRegistry = createFileRegistry;
113
+ function updateVirtualCodeMaps(virtualCode, getSourceSnapshot, map = new Map()) {
114
+ const sources = new Set();
115
+ for (const mapping of virtualCode.mappings) {
116
+ if (sources.has(mapping.source)) {
117
+ continue;
118
+ }
119
+ sources.add(mapping.source);
120
+ const source = getSourceSnapshot(mapping.source);
121
+ if (!source) {
122
+ continue;
123
+ }
124
+ if (!map.has(source[0]) || map.get(source[0])[0] !== source[1]) {
125
+ map.set(source[0], [source[1], new source_map_1.SourceMap(virtualCode.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
126
+ }
127
+ }
128
+ return map;
129
+ }
130
+ exports.updateVirtualCodeMaps = updateVirtualCodeMaps;
131
+ function* forEachEmbeddedCode(code) {
132
+ yield code;
133
+ for (const embeddedCode of code.embeddedCodes) {
134
+ yield* forEachEmbeddedCode(embeddedCode);
135
+ }
136
+ }
137
+ exports.forEachEmbeddedCode = forEachEmbeddedCode;
138
+ //# sourceMappingURL=fileRegistry.js.map
package/lib/types.d.ts CHANGED
@@ -1,16 +1,22 @@
1
1
  import type { Mapping, Stack } from '@volar/source-map';
2
2
  import type * as ts from 'typescript';
3
- import type { FileProvider } from './fileProvider';
4
- export interface SourceFile extends BaseFile {
5
- virtualFile?: [VirtualFile, LanguagePlugin];
3
+ import type { FileRegistry } from './fileRegistry';
4
+ export interface SourceFile extends BaseCodeInfo {
5
+ /**
6
+ * uri or fileName
7
+ */
8
+ id: string;
9
+ generated?: {
10
+ code: VirtualCode;
11
+ languagePlugin: LanguagePlugin;
12
+ idToFileMap: Map<string, VirtualCode>;
13
+ };
6
14
  }
7
15
  export type CodeMapping = Mapping<CodeInformation>;
8
- export interface VirtualFile extends BaseFile {
16
+ export interface VirtualCode<T extends string = string> extends BaseCodeInfo {
17
+ id: T;
9
18
  mappings: CodeMapping[];
10
- embeddedFiles: VirtualFile[];
11
- typescript?: {
12
- scriptKind: ts.ScriptKind;
13
- };
19
+ embeddedCodes: VirtualCode[];
14
20
  codegenStacks?: Stack[];
15
21
  linkedCodeMappings?: Mapping[];
16
22
  }
@@ -39,30 +45,33 @@ export interface CodeInformation {
39
45
  /** virtual code is expected correctly reflect the format information of the source code */
40
46
  format: boolean;
41
47
  }
42
- export interface BaseFile {
43
- fileName: string;
48
+ export interface BaseCodeInfo {
44
49
  languageId: string;
45
50
  snapshot: ts.IScriptSnapshot;
46
51
  }
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;
52
+ export interface LanguagePlugin<T extends VirtualCode = VirtualCode> {
53
+ createVirtualCode(fileId: string, languageId: string, snapshot: ts.IScriptSnapshot, files?: FileRegistry): T | undefined;
54
+ updateVirtualCode(fileId: string, virtualCode: T, newSnapshot: ts.IScriptSnapshot, files?: FileRegistry): T;
55
+ disposeVirtualCode?(fileId: string, virtualCode: T, files?: FileRegistry): void;
51
56
  typescript?: {
52
57
  extraFileExtensions: ts.FileExtensionInfo[];
53
- resolveSourceFileName(tsFileName: string): string | undefined;
54
- resolveModuleName?(path: string, impliedNodeFormat?: ts.ResolutionMode): string | undefined;
55
58
  resolveLanguageServiceHost?(host: ts.LanguageServiceHost): ts.LanguageServiceHost;
59
+ getScript(rootVirtualCode: T): {
60
+ code: VirtualCode;
61
+ extension: '.ts' | '.js' | '.mts' | '.mjs' | '.cjs' | '.cts' | '.d.ts' | string;
62
+ scriptKind: ts.ScriptKind;
63
+ } | undefined;
56
64
  };
57
65
  }
58
- export interface Language {
59
- files: FileProvider;
66
+ export interface LanguageContext {
67
+ files: FileRegistry;
60
68
  typescript?: {
61
69
  configFileName: string | undefined;
62
- sys: ts.System;
70
+ sys: ts.System & {
71
+ sync?(): Promise<number>;
72
+ };
63
73
  projectHost: TypeScriptProjectHost;
64
74
  languageServiceHost: ts.LanguageServiceHost;
65
- synchronizeFileSystem?(): Promise<number>;
66
75
  };
67
76
  }
68
77
  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.13",
3
+ "version": "2.0.0-alpha.14",
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.13"
15
+ "@volar/source-map": "2.0.0-alpha.14"
16
16
  },
17
- "gitHead": "b052e3d47235325ac614257be1cebd541d421134"
17
+ "gitHead": "83eb6a4a4475a3680f14fb3808e48ec4dbe62cc1"
18
18
  }
@@ -1,15 +0,0 @@
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, 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