Skip to content

Commit

Permalink
refactor(language-core): make embeddedCodes optional in VirtualCode (#…
Browse files Browse the repository at this point in the history
…137)

In many cases `embeddedCodes` is always empty. It might as well be
optional.

Co-authored-by: Johnson Chu <johnsoncodehk@gmail.com>
  • Loading branch information
remcohaszing and johnsoncodehk committed Feb 20, 2024
1 parent b1932f7 commit d18261b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/language-core/lib/fileRegistry.ts
Expand Up @@ -151,7 +151,9 @@ export function updateVirtualCodeMaps(

export function* forEachEmbeddedCode(code: VirtualCode): Generator<VirtualCode> {
yield code;
for (const embeddedCode of code.embeddedCodes) {
yield* forEachEmbeddedCode(embeddedCode);
if (code.embeddedCodes) {
for (const embeddedCode of code.embeddedCodes) {
yield* forEachEmbeddedCode(embeddedCode);
}
}
}
2 changes: 1 addition & 1 deletion packages/language-core/lib/types.ts
Expand Up @@ -22,7 +22,7 @@ export interface VirtualCode<T extends string = string> {
languageId: string;
snapshot: ts.IScriptSnapshot;
mappings: CodeMapping[];
embeddedCodes: VirtualCode[];
embeddedCodes?: VirtualCode[];
codegenStacks?: Stack[];
linkedCodeMappings?: Mapping[];
}
Expand Down
Expand Up @@ -80,7 +80,7 @@ export function registerEditorFeatures(
fileUri: sourceFile!.id,
virtualCodeId: code.id,
languageId: code.languageId,
embeddedCodes: code.embeddedCodes.map(prune),
embeddedCodes: code.embeddedCodes?.map(prune) || [],
version,
disabled: languageService.context.disabledVirtualFileUris.has(uri),
};
Expand Down
16 changes: 12 additions & 4 deletions packages/language-service/lib/utils/featureWorkers.ts
Expand Up @@ -130,8 +130,10 @@ export function* eachEmbeddedDocument(
rootCode = current,
): Generator<SourceMapWithDocuments<CodeInformation>> {

for (const embeddedCode of current.embeddedCodes) {
yield* eachEmbeddedDocument(context, embeddedCode, rootCode);
if (current.embeddedCodes) {
for (const embeddedCode of current.embeddedCodes) {
yield* eachEmbeddedDocument(context, embeddedCode, rootCode);
}
}

for (const map of context.documents.getMaps(current)) {
Expand All @@ -155,10 +157,16 @@ export function getEmbeddedFilesByLevel(context: ServiceContext, sourceFileUri:
return embeddedFilesByLevel[level];
}

let nextLevel: VirtualCode[] = [];
const nextLevel: VirtualCode[] = [];

for (const file of embeddedFilesByLevel[embeddedFilesByLevel.length - 1]) {
nextLevel = nextLevel.concat(file.embeddedCodes.filter(file => !context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(sourceFileUri, file.id))));
if (file.embeddedCodes) {
for (const embedded of file.embeddedCodes) {
if (!context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(sourceFileUri, embedded.id))) {
nextLevel.push(embedded);
}
}
}
}

embeddedFilesByLevel.push(nextLevel);
Expand Down

0 comments on commit d18261b

Please sign in to comment.