-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.ts
188 lines (182 loc) Β· 5.65 KB
/
index.ts
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
export * from '@volar/source-map';
export * from './lib/editorFeatures';
export * from './lib/linkedCodeMap';
export * from './lib/types';
export * from './lib/utils';
import { SourceMap } from '@volar/source-map';
import type * as ts from 'typescript';
import { LinkedCodeMap } from './lib/linkedCodeMap';
import type { CodeInformation, Language, LanguagePlugin, SourceScript, VirtualCode } from './lib/types';
import { FileMap } from './lib/utils';
export function createLanguage(plugins: LanguagePlugin[], caseSensitive: boolean, sync: (id: string) => void): Language {
const sourceScripts = new FileMap<SourceScript>(caseSensitive);
const virtualCodeToSourceFileMap = new WeakMap<VirtualCode, SourceScript>();
const virtualCodeToMaps = new WeakMap<ts.IScriptSnapshot, Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>>();
const virtualCodeToLinkedCodeMap = new WeakMap<ts.IScriptSnapshot, LinkedCodeMap | undefined>();
return {
plugins,
scripts: {
get(id) {
sync(id);
return sourceScripts.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;
}
if (sourceScripts.has(id)) {
const sourceScript = sourceScripts.get(id)!;
if (sourceScript.languageId !== languageId) {
// languageId changed
this.delete(id);
return this.set(id, snapshot, languageId);
}
else if (sourceScript.snapshot !== snapshot) {
// snapshot updated
sourceScript.snapshot = snapshot;
if (sourceScript.generated) {
const newVirtualCode = sourceScript.generated.languagePlugin.updateVirtualCode?.(id, sourceScript.generated.root, snapshot);
if (newVirtualCode) {
sourceScript.generated.root = newVirtualCode;
sourceScript.generated.embeddedCodes.clear();
for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
virtualCodeToSourceFileMap.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 = { id, languageId, snapshot };
sourceScripts.set(id, sourceScript);
for (const languagePlugin of _plugins) {
const virtualCode = languagePlugin.createVirtualCode?.(id, languageId, snapshot);
if (virtualCode) {
sourceScript.generated = {
root: virtualCode,
languagePlugin,
embeddedCodes: new Map(),
};
for (const code of forEachEmbeddedCode(virtualCode)) {
virtualCodeToSourceFileMap.set(code, sourceScript);
sourceScript.generated.embeddedCodes.set(code.id, code);
}
break;
}
}
return sourceScript;
}
},
delete(id) {
const value = sourceScripts.get(id);
if (value) {
if (value.generated) {
value.generated.languagePlugin.disposeVirtualCode?.(id, value.generated.root);
}
sourceScripts.delete(id);
}
},
},
maps: {
get(virtualCode, scriptId) {
if (!scriptId) {
const sourceScript = virtualCodeToSourceFileMap.get(virtualCode);
if (!sourceScript) {
return;
}
scriptId = sourceScript.id;
}
for (const [id, [_snapshot, map]] of this.forEach(virtualCode)) {
if (id === scriptId) {
return map;
}
}
},
forEach(virtualCode) {
let map = virtualCodeToMaps.get(virtualCode.snapshot);
if (!map) {
map = new Map();
virtualCodeToMaps.set(virtualCode.snapshot, map);
}
updateVirtualCodeMapOfMap(virtualCode, map, id => {
if (id) {
const sourceScript = sourceScripts.get(id)!;
return [id, sourceScript.snapshot];
}
else {
const sourceScript = virtualCodeToSourceFileMap.get(virtualCode)!;
return [sourceScript.id, sourceScript.snapshot];
}
});
return map;
},
},
linkedCodeMaps: {
get(virtualCode) {
if (!virtualCodeToLinkedCodeMap.has(virtualCode.snapshot)) {
virtualCodeToLinkedCodeMap.set(
virtualCode.snapshot,
virtualCode.linkedCodeMappings
? new LinkedCodeMap(virtualCode.linkedCodeMappings)
: undefined
);
}
return virtualCodeToLinkedCodeMap.get(virtualCode.snapshot);
},
},
};
}
export function updateVirtualCodeMapOfMap(
virtualCode: VirtualCode,
mapOfMap: Map<string, [ts.IScriptSnapshot, SourceMap<CodeInformation>]>,
getSourceSnapshot: (id: string | undefined) => [string, ts.IScriptSnapshot] | undefined,
) {
const sources = new Set<string | undefined>();
if (!virtualCode.mappings.length) {
const source = getSourceSnapshot(undefined);
if (source) {
mapOfMap.set(source[0], [source[1], new SourceMap([])]);
}
}
for (const mapping of virtualCode.mappings) {
if (sources.has(mapping.source)) {
continue;
}
sources.add(mapping.source);
const source = getSourceSnapshot(mapping.source);
if (!source) {
continue;
}
if (!mapOfMap.has(source[0]) || mapOfMap.get(source[0])![0] !== source[1]) {
mapOfMap.set(source[0], [source[1], new SourceMap(virtualCode.mappings.filter(mapping2 => mapping2.source === mapping.source))]);
}
}
}
export function* forEachEmbeddedCode(virtualCode: VirtualCode): Generator<VirtualCode> {
yield virtualCode;
if (virtualCode.embeddedCodes) {
for (const embeddedCode of virtualCode.embeddedCodes) {
yield* forEachEmbeddedCode(embeddedCode);
}
}
}