Skip to content

Commit 6ccf06c

Browse files
authored
refactor(language-core): re-implement component references by codegen (#5736)
1 parent 1aa28ae commit 6ccf06c

File tree

2 files changed

+17
-61
lines changed

2 files changed

+17
-61
lines changed

packages/language-core/lib/codegen/script/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,36 @@ function* generateExportDefault(options: ScriptCodegenOptions): Generator<Code>
167167
return;
168168
}
169169

170-
let prefix: Code;
171-
let suffix: Code;
170+
const expression = `{} as typeof __VLS_export`;
171+
172172
if (options.sfc.script && options.scriptRanges?.exportDefault) {
173173
const { exportDefault, componentOptions } = options.scriptRanges;
174-
prefix = generateSfcBlockSection(
174+
yield generateSfcBlockSection(
175175
options.sfc.script,
176176
exportDefault.start,
177177
(componentOptions ?? exportDefault).expression.start,
178178
codeFeatures.all,
179179
);
180-
suffix = generateSfcBlockSection(
180+
yield expression;
181+
yield generateSfcBlockSection(
181182
options.sfc.script,
182183
(componentOptions ?? exportDefault).expression.end,
183184
options.sfc.script.content.length,
184185
codeFeatures.all,
185186
);
186187
}
187188
else {
188-
prefix = `export default `;
189-
suffix = endOfLine;
189+
yield `export `;
190+
if (options.sfc.template) {
191+
for (let i = 0; i < 'template'.length + 1; i++) {
192+
yield [
193+
``,
194+
'main',
195+
options.sfc.template.start + 1 + i,
196+
i ? { __combineOffset: i } : codeFeatures.navigationWithoutRename,
197+
];
198+
}
199+
}
200+
yield `default ${expression}${endOfLine}`;
190201
}
191-
yield prefix;
192-
yield `{} as typeof __VLS_export`;
193-
yield suffix;
194202
}

packages/typescript-plugin/lib/common.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export function createVueLanguageServiceProxy<T>(
1414
const proxyCache = new Map<string | symbol, Function | undefined>();
1515
const getProxyMethod = (target: ts.LanguageService, p: string | symbol): Function | undefined => {
1616
switch (p) {
17-
case 'findReferences':
18-
return findReferences(ts, language, languageService, asScriptId, target[p]);
1917
case 'getCompletionsAtPosition':
2018
return getCompletionsAtPosition(ts, language, asScriptId, vueOptions, target[p]);
2119
case 'getCompletionEntryDetails':
@@ -44,56 +42,6 @@ export function createVueLanguageServiceProxy<T>(
4442
});
4543
}
4644

47-
function findReferences<T>(
48-
ts: typeof import('typescript'),
49-
language: Language<T>,
50-
languageService: ts.LanguageService,
51-
asScriptId: (fileName: string) => T,
52-
findReferences: ts.LanguageService['findReferences'],
53-
): ts.LanguageService['findReferences'] {
54-
return (fileName, position) => {
55-
const result = findReferences(fileName, position);
56-
57-
const sourceScript = language.scripts.get(asScriptId(fileName));
58-
const root = sourceScript?.generated?.root;
59-
if (!(root instanceof VueVirtualCode)) {
60-
return result;
61-
}
62-
63-
const { template } = root.sfc;
64-
if (template) {
65-
const textSpan = {
66-
start: template.start + 1,
67-
length: 'template'.length,
68-
};
69-
if (position >= textSpan.start && position <= textSpan.start + textSpan.length) {
70-
result?.push({
71-
definition: {
72-
fileName,
73-
textSpan,
74-
kind: ts.ScriptElementKind.scriptElement,
75-
name: fileName,
76-
containerKind: ts.ScriptElementKind.unknown,
77-
containerName: '',
78-
displayParts: [],
79-
},
80-
references: [
81-
{
82-
fileName,
83-
textSpan,
84-
isDefinition: true,
85-
isWriteAccess: false,
86-
},
87-
...languageService.getFileReferences(fileName),
88-
],
89-
});
90-
}
91-
}
92-
93-
return result;
94-
};
95-
}
96-
9745
function getCompletionsAtPosition<T>(
9846
ts: typeof import('typescript'),
9947
language: Language<T>,

0 commit comments

Comments
 (0)