Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(language-core): make embeddedCodes optional in VirtualCode #137

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/language-core/lib/fileRegistry.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading