From 2a1f32cbd3baca15e699d13e6e50170c625d5929 Mon Sep 17 00:00:00 2001 From: KazariEX Date: Fri, 12 Sep 2025 16:46:31 +0800 Subject: [PATCH] fix(language-core): initialize properties of `VueVirtualCode` in constructor --- .../language-core/lib/virtualFile/vueFile.ts | 49 +++++++++---------- packages/typescript-plugin/lib/common.ts | 4 +- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/packages/language-core/lib/virtualFile/vueFile.ts b/packages/language-core/lib/virtualFile/vueFile.ts index 13b0c63b4b..2220de4781 100644 --- a/packages/language-core/lib/virtualFile/vueFile.ts +++ b/packages/language-core/lib/virtualFile/vueFile.ts @@ -1,4 +1,4 @@ -import type { VirtualCode } from '@volar/language-core'; +import type { CodeInformation, Mapping, VirtualCode } from '@volar/language-core'; import { computed, signal } from 'alien-signals'; import type * as ts from 'typescript'; import { allCodeFeatures } from '../plugins'; @@ -8,28 +8,16 @@ import { computedSfc } from './computedSfc'; import { computedVueSfc } from './computedVueSfc'; export class VueVirtualCode implements VirtualCode { - // sources + readonly id = 'main'; + readonly sfc: ReturnType; - id = 'main'; - - private _snapshot = signal(undefined!); - - // computeds - - private _vueSfc = computedVueSfc(this.plugins, this.fileName, this.languageId, this._snapshot); - private _sfc = computedSfc(this.ts, this.plugins, this.fileName, this._snapshot, this._vueSfc); - private _embeddedCodes = computedEmbeddedCodes(this.plugins, this.fileName, this._sfc); - private _mappings = computed(() => { - const snapshot = this._snapshot(); - return [{ - sourceOffsets: [0], - generatedOffsets: [0], - lengths: [snapshot.getLength()], - data: allCodeFeatures, - }]; - }); - - // others + private _snapshot: { + (): ts.IScriptSnapshot; + (value: ts.IScriptSnapshot): void; + }; + private _vueSfc: ReturnType; + private _embeddedCodes: ReturnType; + private _mappings: () => Mapping[]; get snapshot() { return this._snapshot(); @@ -37,9 +25,6 @@ export class VueVirtualCode implements VirtualCode { get vueSfc() { return this._vueSfc(); } - get sfc() { - return this._sfc; - } get embeddedCodes() { return this._embeddedCodes(); } @@ -55,7 +40,19 @@ export class VueVirtualCode implements VirtualCode { public plugins: VueLanguagePluginReturn[], public ts: typeof import('typescript'), ) { - this._snapshot(initSnapshot); + this._snapshot = signal(initSnapshot); + this._vueSfc = computedVueSfc(this.plugins, this.fileName, this.languageId, this._snapshot); + this.sfc = computedSfc(this.ts, this.plugins, this.fileName, this._snapshot, this._vueSfc); + this._embeddedCodes = computedEmbeddedCodes(this.plugins, this.fileName, this.sfc); + this._mappings = computed(() => { + const snapshot = this._snapshot(); + return [{ + sourceOffsets: [0], + generatedOffsets: [0], + lengths: [snapshot.getLength()], + data: allCodeFeatures, + }]; + }); } update(newSnapshot: ts.IScriptSnapshot) { diff --git a/packages/typescript-plugin/lib/common.ts b/packages/typescript-plugin/lib/common.ts index 76473b8770..796996f7d2 100644 --- a/packages/typescript-plugin/lib/common.ts +++ b/packages/typescript-plugin/lib/common.ts @@ -159,8 +159,8 @@ function getCompletionEntryDetails( const { fileName } = args[6].__isAutoImport; const sourceScript = language.scripts.get(fileName); if (sourceScript?.generated?.root instanceof VueVirtualCode) { - const sfc = sourceScript.generated.root.vueSfc; - if (!sfc?.descriptor.script && !sfc?.descriptor.scriptSetup) { + const { vueSfc } = sourceScript.generated.root; + if (!vueSfc?.descriptor.script && !vueSfc?.descriptor.scriptSetup) { for (const codeAction of details?.codeActions ?? []) { for (const change of codeAction.changes) { for (const textChange of change.textChanges) {