Skip to content

Commit f6fab34

Browse files
authored
fix(language-service): consistent data from provider for sfc completion (#4645)
1 parent 4dd5ee9 commit f6fab34

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

packages/language-service/lib/plugins/vue-sfc.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,15 @@ export function create(): LanguageServicePlugin {
167167
return;
168168
}
169169
result.items = result.items.filter(item => item.label !== '!DOCTYPE' && item.label !== 'Custom Blocks');
170-
for (const scriptItem of result.items.filter(item => item.label === 'script' || item.label === 'script setup')) {
170+
171+
const tags = sfcDataProvider?.provideTags();
172+
173+
const scriptLangs = getLangs('script');
174+
const scriptItems = result.items.filter(item => item.label === 'script' || item.label === 'script setup');
175+
for (const scriptItem of scriptItems) {
171176
scriptItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
172177
scriptItem.detail = '.js';
173-
for (const lang of ['ts', 'tsx', 'jsx']) {
178+
for (const lang of scriptLangs) {
174179
result.items.push({
175180
...scriptItem,
176181
detail: `.${lang}`,
@@ -183,11 +188,13 @@ export function create(): LanguageServicePlugin {
183188
});
184189
}
185190
}
191+
192+
const styleLangs = getLangs('style');
186193
const styleItem = result.items.find(item => item.label === 'style');
187194
if (styleItem) {
188195
styleItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
189196
styleItem.detail = '.css';
190-
for (const lang of ['css', 'scss', 'less', 'postcss']) {
197+
for (const lang of styleLangs) {
191198
result.items.push({
192199
...styleItem,
193200
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
@@ -200,22 +207,36 @@ export function create(): LanguageServicePlugin {
200207
});
201208
}
202209
}
210+
211+
const templateLangs = getLangs('template');
203212
const templateItem = result.items.find(item => item.label === 'template');
204213
if (templateItem) {
205214
templateItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
206215
templateItem.detail = '.html';
207-
result.items.push({
208-
...templateItem,
209-
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
210-
detail: '.pug',
211-
label: templateItem.label + ' lang="pug"',
212-
textEdit: templateItem.textEdit ? {
213-
...templateItem.textEdit,
214-
newText: templateItem.textEdit.newText + ' lang="pug"',
215-
} : undefined,
216-
});
216+
for (const lang of templateLangs) {
217+
if (lang === 'html') {
218+
continue;
219+
}
220+
result.items.push({
221+
...templateItem,
222+
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
223+
detail: `.${lang}`,
224+
label: templateItem.label + ' lang="' + lang + '"',
225+
textEdit: templateItem.textEdit ? {
226+
...templateItem.textEdit,
227+
newText: templateItem.textEdit.newText + ' lang="' + lang + '"',
228+
} : undefined,
229+
});
230+
}
217231
}
218232
return result;
233+
234+
function getLangs(label: string) {
235+
return tags
236+
?.find(tag => tag.name === label)?.attributes
237+
.find(attr => attr.name === 'lang')?.values
238+
?.map(({ name }) => name) ?? [];
239+
}
219240
},
220241
};
221242
},

0 commit comments

Comments
 (0)