Skip to content

Commit dd83a93

Browse files
committed
extract fetchVirtualCode
1 parent 0cc8b37 commit dd83a93

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Diff for: packages/core/src/volar/language-server.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -115,45 +115,51 @@ function filterAndAugmentDiagnostics(
115115
return diagnostics;
116116
}
117117

118-
let cachedTransformedModule: TransformedModule | null | undefined = undefined;
119-
120-
const mappingForDiagnostic = (diagnostic: vscode.Diagnostic): MappingTree | null => {
121-
if (typeof cachedTransformedModule === 'undefined') {
122-
cachedTransformedModule = null;
118+
// Lazily fetch and cache the VirtualCode -- this might be a premature optimization
119+
// after the code went through enough changes, so maybe safe to simplify in the future.
120+
let cachedVirtualCode: VirtualGtsCode | null | undefined = undefined;
121+
const fetchVirtualCode = (): VirtualGtsCode | null => {
122+
if (typeof cachedVirtualCode === 'undefined') {
123+
cachedVirtualCode = null;
123124

124125
const decoded = context.decodeEmbeddedDocumentUri(URI.parse(document.uri));
125126
if (decoded) {
126127
const script = context.language.scripts.get(decoded[0]);
127128
const scriptRoot = script?.generated?.root;
128129
if (scriptRoot instanceof VirtualGtsCode) {
129-
const transformedModule = scriptRoot.transformedModule;
130-
if (transformedModule) {
131-
cachedTransformedModule = transformedModule;
132-
}
130+
cachedVirtualCode = scriptRoot;
133131
}
134132
}
135133
}
136134

137-
if (!cachedTransformedModule) {
135+
return cachedVirtualCode;
136+
}
137+
138+
const mappingForDiagnostic = (diagnostic: vscode.Diagnostic): MappingTree | null => {
139+
const transformedModule = fetchVirtualCode()?.transformedModule;
140+
141+
if (!transformedModule) {
138142
return null;
139143
}
140144

141145
const range = diagnostic.range;
142146
const start = document.offsetAt(range.start);
143147
const end = document.offsetAt(range.end);
144-
const rangeWithMappingAndSource = cachedTransformedModule.getOriginalRange(start, end);
148+
const rangeWithMappingAndSource = transformedModule.getOriginalRange(start, end);
145149
return rangeWithMappingAndSource.mapping || null;
146150
};
147151

148-
return diagnostics.map((diagnostic) => {
152+
const allDiagnostics: vscode.Diagnostic[] = [];
153+
154+
return diagnostics.forEach((diagnostic) => {
149155
diagnostic = {
150156
...diagnostic,
151157
source: 'glint',
152158
};
153159

154160
diagnostic = augmentDiagnostic(diagnostic as any, mappingForDiagnostic);
155161

156-
return diagnostic;
162+
allDiagnostics.push(diagnostic);
157163
});
158164
}
159165

0 commit comments

Comments
 (0)