-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathindex.ts
More file actions
249 lines (241 loc) Β· 7.63 KB
/
index.ts
File metadata and controls
249 lines (241 loc) Β· 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
export { Mapping, SourceMap } from '@volar/source-map';
export * from './lib/editor';
export * from './lib/linkedCodeMap';
export * from './lib/types';
export * from './lib/utils';
import { SourceMap } from '@volar/source-map';
import { LinkedCodeMap } from './lib/linkedCodeMap';
import type {
CodegenContext,
IScriptSnapshot,
Language,
LanguagePlugin,
Mapper,
MapperFactory,
SourceScript,
VirtualCode
} from './lib/types';
export const defaultMapperFactory: MapperFactory = mappings => new SourceMap(mappings);
export function createLanguage<T>(
plugins: LanguagePlugin<T>[],
scriptRegistry: Map<T, SourceScript<T>>,
sync: (id: T, includeFsFiles: boolean, shouldRegister: boolean) => void,
onAssociationDirty?: (targetId: T) => void
) {
const virtualCodeToSourceScriptMap = new WeakMap<VirtualCode, SourceScript<T>>();
const virtualCodeToSourceMap = new WeakMap<IScriptSnapshot, WeakMap<IScriptSnapshot, Mapper>>();
const virtualCodeToLinkedCodeMap = new WeakMap<IScriptSnapshot, [IScriptSnapshot, LinkedCodeMap | undefined]>();
const language: Language<T> = {
mapperFactory: defaultMapperFactory,
plugins,
scripts: {
fromVirtualCode(virtualCode) {
return virtualCodeToSourceScriptMap.get(virtualCode)!;
},
get(id, includeFsFiles = true, shouldRegister = false) {
sync(id, includeFsFiles, shouldRegister);
const result = scriptRegistry.get(id);
// The sync function provider may not always call the set function due to caching, so it is necessary to explicitly check isAssociationDirty.
if (result?.isAssociationDirty) {
this.set(id, result.snapshot, result.languageId);
}
return scriptRegistry.get(id);
},
set(id, snapshot, languageId, _plugins = plugins) {
if (!languageId) {
for (const plugin of plugins) {
languageId = plugin.getLanguageId?.(id);
if (languageId) {
break;
}
}
}
if (!languageId) {
console.warn(`languageId not found for ${id}`);
return;
}
let associatedOnly = false;
for (const plugin of plugins) {
if (plugin.isAssociatedFileOnly?.(id, languageId)) {
associatedOnly = true;
break;
}
}
if (scriptRegistry.has(id)) {
const sourceScript = scriptRegistry.get(id)!;
if (sourceScript.languageId !== languageId || sourceScript.associatedOnly !== associatedOnly) {
this.delete(id);
triggerTargetsDirty(sourceScript);
return this.set(id, snapshot, languageId);
}
else if (associatedOnly) {
if (sourceScript.snapshot !== snapshot) {
sourceScript.snapshot = snapshot;
triggerTargetsDirty(sourceScript);
}
}
else if (sourceScript.isAssociationDirty || sourceScript.snapshot !== snapshot) {
if (sourceScript.snapshot !== snapshot) {
sourceScript.snapshot = snapshot;
triggerTargetsDirty(sourceScript);
}
const codegenCtx = prepareCreateVirtualCode(sourceScript);
if (sourceScript.generated) {
const { updateVirtualCode, createVirtualCode } = sourceScript.generated.languagePlugin;
const newVirtualCode = updateVirtualCode
? updateVirtualCode(id, sourceScript.generated.root, snapshot, codegenCtx)
: createVirtualCode?.(id, languageId, snapshot, codegenCtx);
if (newVirtualCode) {
sourceScript.generated.root = newVirtualCode;
sourceScript.generated.embeddedCodes.clear();
for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
virtualCodeToSourceScriptMap.set(code, sourceScript);
sourceScript.generated.embeddedCodes.set(code.id, code);
}
return sourceScript;
}
else {
this.delete(id);
return;
}
}
}
else {
// not changed
return sourceScript;
}
}
else {
// created
const sourceScript: SourceScript<T> = {
id: id,
languageId,
snapshot,
associatedIds: new Set(),
targetIds: new Set(),
associatedOnly
};
scriptRegistry.set(id, sourceScript);
if (associatedOnly) {
return sourceScript;
}
for (const languagePlugin of _plugins) {
const virtualCode = languagePlugin.createVirtualCode?.(id, languageId, snapshot, prepareCreateVirtualCode(sourceScript));
if (virtualCode) {
sourceScript.generated = {
root: virtualCode,
languagePlugin,
embeddedCodes: new Map(),
};
for (const code of forEachEmbeddedCode(virtualCode)) {
virtualCodeToSourceScriptMap.set(code, sourceScript);
sourceScript.generated.embeddedCodes.set(code.id, code);
}
break;
}
}
return sourceScript;
}
},
delete(id) {
const sourceScript = scriptRegistry.get(id);
if (sourceScript) {
sourceScript.generated?.languagePlugin.disposeVirtualCode?.(id, sourceScript.generated.root);
scriptRegistry.delete(id);
triggerTargetsDirty(sourceScript);
}
},
},
maps: {
get(virtualCode, sourceScript) {
let mapCache = virtualCodeToSourceMap.get(virtualCode.snapshot);
if (!mapCache) {
virtualCodeToSourceMap.set(
virtualCode.snapshot,
mapCache = new WeakMap()
);
}
if (!mapCache.has(sourceScript.snapshot)) {
const mappings = virtualCode.associatedScriptMappings?.get(sourceScript.id) ?? virtualCode.mappings;
mapCache.set(
sourceScript.snapshot,
language.mapperFactory(mappings)
);
}
return mapCache.get(sourceScript.snapshot)!;
},
*forEach(virtualCode) {
const sourceScript = virtualCodeToSourceScriptMap.get(virtualCode)!;
yield [
sourceScript,
this.get(virtualCode, sourceScript),
];
if (virtualCode.associatedScriptMappings) {
for (const [relatedScriptId] of virtualCode.associatedScriptMappings) {
const relatedSourceScript = scriptRegistry.get(relatedScriptId as T);
if (relatedSourceScript) {
yield [
relatedSourceScript,
this.get(virtualCode, relatedSourceScript),
];
}
}
}
},
},
linkedCodeMaps: {
get(virtualCode) {
const sourceScript = virtualCodeToSourceScriptMap.get(virtualCode)!;
let mapCache = virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
if (mapCache?.[0] !== sourceScript.snapshot) {
virtualCodeToLinkedCodeMap.set(
virtualCode.snapshot,
mapCache = [
sourceScript.snapshot,
virtualCode.linkedCodeMappings
? new LinkedCodeMap(virtualCode.linkedCodeMappings)
: undefined,
]
);
}
return mapCache[1];
},
},
};
return language;
function triggerTargetsDirty(sourceScript: SourceScript<T>) {
sourceScript.targetIds.forEach(id => {
const sourceScript = scriptRegistry.get(id);
if (sourceScript) {
sourceScript.isAssociationDirty = true;
onAssociationDirty?.(sourceScript.id);
}
});
}
function prepareCreateVirtualCode(sourceScript: SourceScript<T>): CodegenContext<T> {
for (const id of sourceScript.associatedIds) {
scriptRegistry.get(id)?.targetIds.delete(sourceScript.id);
}
sourceScript.associatedIds.clear();
sourceScript.isAssociationDirty = false;
return {
getAssociatedScript(id) {
sync(id, true, true);
const relatedSourceScript = scriptRegistry.get(id);
if (relatedSourceScript) {
relatedSourceScript.targetIds.add(sourceScript.id);
sourceScript.associatedIds.add(relatedSourceScript.id);
}
return relatedSourceScript;
},
};
}
}
export function* forEachEmbeddedCode(virtualCode: VirtualCode): Generator<VirtualCode> {
yield virtualCode;
if (virtualCode.embeddedCodes) {
for (const embeddedCode of virtualCode.embeddedCodes) {
yield* forEachEmbeddedCode(embeddedCode);
}
}
}