@volar/language-core 2.2.0-alpha.0 → 2.2.0-alpha.2
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/lib/fileRegistry.d.ts +17 -0
- package/lib/fileRegistry.js +137 -0
- package/package.json +3 -3
|
@@ -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>[];
|
|
8
|
+
set(id: string, languageId: string, snapshot: ts.IScriptSnapshot, plugins?: LanguagePlugin<VirtualCode>[]): 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, 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,137 @@
|
|
|
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, plugins = languagePlugins) {
|
|
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 plugins) {
|
|
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
|
+
if (code.embeddedCodes) {
|
|
131
|
+
for (const embeddedCode of code.embeddedCodes) {
|
|
132
|
+
yield* forEachEmbeddedCode(embeddedCode);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.forEachEmbeddedCode = forEachEmbeddedCode;
|
|
137
|
+
//# sourceMappingURL=fileRegistry.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volar/language-core",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.2",
|
|
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.2.0-alpha.
|
|
15
|
+
"@volar/source-map": "2.2.0-alpha.2"
|
|
16
16
|
},
|
|
17
|
-
"gitHead": "
|
|
17
|
+
"gitHead": "c6a538c915cc8b32ad9a7ca1092a29a326d49161"
|
|
18
18
|
}
|