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

fix(language-service): consistent data from provider for sfc completion #4645

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
47 changes: 34 additions & 13 deletions packages/language-service/lib/plugins/vue-sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ export function create(): LanguageServicePlugin {
return;
}
result.items = result.items.filter(item => item.label !== '!DOCTYPE' && item.label !== 'Custom Blocks');
for (const scriptItem of result.items.filter(item => item.label === 'script' || item.label === 'script setup')) {

const tags = sfcDataProvider?.provideTags();

const scriptLangs = getLangs('script');
const scriptItems = result.items.filter(item => item.label === 'script' || item.label === 'script setup');
for (const scriptItem of scriptItems) {
scriptItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
scriptItem.detail = '.js';
for (const lang of ['ts', 'tsx', 'jsx']) {
for (const lang of scriptLangs) {
result.items.push({
...scriptItem,
detail: `.${lang}`,
Expand All @@ -195,11 +200,13 @@ export function create(): LanguageServicePlugin {
});
}
}

const styleLangs = getLangs('style');
const styleItem = result.items.find(item => item.label === 'style');
if (styleItem) {
styleItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
styleItem.detail = '.css';
for (const lang of ['css', 'scss', 'less', 'postcss']) {
for (const lang of styleLangs) {
result.items.push({
...styleItem,
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
Expand All @@ -212,22 +219,36 @@ export function create(): LanguageServicePlugin {
});
}
}

const templateLangs = getLangs('template');
const templateItem = result.items.find(item => item.label === 'template');
if (templateItem) {
templateItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
templateItem.detail = '.html';
result.items.push({
...templateItem,
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
detail: '.pug',
label: templateItem.label + ' lang="pug"',
textEdit: templateItem.textEdit ? {
...templateItem.textEdit,
newText: templateItem.textEdit.newText + ' lang="pug"',
} : undefined,
});
for (const lang of templateLangs) {
if (lang === 'html') {
continue;
}
result.items.push({
...templateItem,
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
detail: `.${lang}`,
label: templateItem.label + ' lang="' + lang + '"',
textEdit: templateItem.textEdit ? {
...templateItem.textEdit,
newText: templateItem.textEdit.newText + ' lang="' + lang + '"',
} : undefined,
});
}
}
return result;

function getLangs(label: string) {
return tags
?.find(tag => tag.name === label)?.attributes
.find(attr => attr.name === 'lang')?.values
?.map(({ name }) => name) ?? [];
}
},
};
},
Expand Down
Loading