Skip to content

Commit

Permalink
feat(language-service): add style scoped and module completion (#4705)
Browse files Browse the repository at this point in the history
  • Loading branch information
runyasak authored Aug 26, 2024
1 parent 23dbcea commit 333d73e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
12 changes: 12 additions & 0 deletions packages/language-server/tests/completions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ describe('Completions', async () => {
"script setup lang="tsx"",
"script setup lang="jsx"",
"style lang="css"",
"style lang="css" scoped",
"style lang="css" module",
"style lang="scss"",
"style lang="scss" scoped",
"style lang="scss" module",
"style lang="less"",
"style lang="less" scoped",
"style lang="less" module",
"style lang="stylus"",
"style lang="stylus" scoped",
"style lang="stylus" module",
"style lang="postcss"",
"style lang="postcss" scoped",
"style lang="postcss" module",
"style lang="sass"",
"style lang="sass" scoped",
"style lang="sass" module",
"template lang="pug"",
]
`);
Expand Down
32 changes: 22 additions & 10 deletions packages/language-service/lib/plugins/vue-sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,11 @@ export function create(): LanguageServicePlugin {
styleItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File;
styleItem.detail = '.css';
for (const lang of styleLangs) {
result.items.push({
...styleItem,
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
detail: lang === 'postcss' ? '.css' : `.${lang}`,
label: styleItem.label + ' lang="' + lang + '"',
textEdit: styleItem.textEdit ? {
...styleItem.textEdit,
newText: styleItem.textEdit.newText + ' lang="' + lang + '"',
} : undefined,
});
result.items.push(
getStyleCompletionItem(styleItem, lang),
getStyleCompletionItem(styleItem, lang, 'scoped'),
getStyleCompletionItem(styleItem, lang, 'module')
);
}
}

Expand Down Expand Up @@ -253,3 +248,20 @@ export function create(): LanguageServicePlugin {
}
}
}

function getStyleCompletionItem(
styleItem: vscode.CompletionItem,
lang: string,
attr?: string
): vscode.CompletionItem {
return {
...styleItem,
kind: 17 satisfies typeof vscode.CompletionItemKind.File,
detail: lang === 'postcss' ? '.css' : `.${lang}`,
label: styleItem.label + ' lang="' + lang + '"' + (attr ? ` ${attr}` : ''),
textEdit: styleItem.textEdit ? {
...styleItem.textEdit,
newText: styleItem.textEdit.newText + ' lang="' + lang + '"' + (attr ? ` ${attr}` : ''),
} : undefined
};
}

0 comments on commit 333d73e

Please sign in to comment.