Skip to content

Commit 9bfbfcc

Browse files
authored
fix(language-core): initialize properties of VueVirtualCode in constructor (#5635)
1 parent 75dca51 commit 9bfbfcc

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

packages/language-core/lib/virtualFile/vueFile.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { VirtualCode } from '@volar/language-core';
1+
import type { CodeInformation, Mapping, VirtualCode } from '@volar/language-core';
22
import { computed, signal } from 'alien-signals';
33
import type * as ts from 'typescript';
44
import { allCodeFeatures } from '../plugins';
@@ -8,38 +8,23 @@ import { computedSfc } from './computedSfc';
88
import { computedVueSfc } from './computedVueSfc';
99

1010
export class VueVirtualCode implements VirtualCode {
11-
// sources
11+
readonly id = 'main';
12+
readonly sfc: ReturnType<typeof computedSfc>;
1213

13-
id = 'main';
14-
15-
private _snapshot = signal<ts.IScriptSnapshot>(undefined!);
16-
17-
// computeds
18-
19-
private _vueSfc = computedVueSfc(this.plugins, this.fileName, this.languageId, this._snapshot);
20-
private _sfc = computedSfc(this.ts, this.plugins, this.fileName, this._snapshot, this._vueSfc);
21-
private _embeddedCodes = computedEmbeddedCodes(this.plugins, this.fileName, this._sfc);
22-
private _mappings = computed(() => {
23-
const snapshot = this._snapshot();
24-
return [{
25-
sourceOffsets: [0],
26-
generatedOffsets: [0],
27-
lengths: [snapshot.getLength()],
28-
data: allCodeFeatures,
29-
}];
30-
});
31-
32-
// others
14+
private _snapshot: {
15+
(): ts.IScriptSnapshot;
16+
(value: ts.IScriptSnapshot): void;
17+
};
18+
private _vueSfc: ReturnType<typeof computedVueSfc>;
19+
private _embeddedCodes: ReturnType<typeof computedEmbeddedCodes>;
20+
private _mappings: () => Mapping<CodeInformation>[];
3321

3422
get snapshot() {
3523
return this._snapshot();
3624
}
3725
get vueSfc() {
3826
return this._vueSfc();
3927
}
40-
get sfc() {
41-
return this._sfc;
42-
}
4328
get embeddedCodes() {
4429
return this._embeddedCodes();
4530
}
@@ -55,7 +40,19 @@ export class VueVirtualCode implements VirtualCode {
5540
public plugins: VueLanguagePluginReturn[],
5641
public ts: typeof import('typescript'),
5742
) {
58-
this._snapshot(initSnapshot);
43+
this._snapshot = signal(initSnapshot);
44+
this._vueSfc = computedVueSfc(this.plugins, this.fileName, this.languageId, this._snapshot);
45+
this.sfc = computedSfc(this.ts, this.plugins, this.fileName, this._snapshot, this._vueSfc);
46+
this._embeddedCodes = computedEmbeddedCodes(this.plugins, this.fileName, this.sfc);
47+
this._mappings = computed(() => {
48+
const snapshot = this._snapshot();
49+
return [{
50+
sourceOffsets: [0],
51+
generatedOffsets: [0],
52+
lengths: [snapshot.getLength()],
53+
data: allCodeFeatures,
54+
}];
55+
});
5956
}
6057

6158
update(newSnapshot: ts.IScriptSnapshot) {

packages/typescript-plugin/lib/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function getCompletionEntryDetails<T>(
159159
const { fileName } = args[6].__isAutoImport;
160160
const sourceScript = language.scripts.get(fileName);
161161
if (sourceScript?.generated?.root instanceof VueVirtualCode) {
162-
const sfc = sourceScript.generated.root.vueSfc;
163-
if (!sfc?.descriptor.script && !sfc?.descriptor.scriptSetup) {
162+
const { vueSfc } = sourceScript.generated.root;
163+
if (!vueSfc?.descriptor.script && !vueSfc?.descriptor.scriptSetup) {
164164
for (const codeAction of details?.codeActions ?? []) {
165165
for (const change of codeAction.changes) {
166166
for (const textChange of change.textChanges) {

0 commit comments

Comments
 (0)