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

Change autocomplete logic of bezier-vscode #2486

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/sixty-horses-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bezier-vscode': minor
---

Change autocomplete logic. Now token name can trigger autocomplete even without `var` string.
30 changes: 17 additions & 13 deletions packages/bezier-vscode/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const completionItemsByTokenGroup = Object.fromEntries(
const completionItems: CompletionItem[] = Object.entries(
tokenKeyValues
).map(([key, value]) => ({
label: `--${key}`,
insertText: `--${key}`,
label: key,
insertText: `var(--${key})`,
// RGBA conversion to display color preview in suggestion item
detail: groupName === 'color' ? hexToRGBA(value) : String(value),
kind:
Expand All @@ -43,15 +43,15 @@ const completionItemsByTokenGroup = Object.fromEntries(
) as Record<TokenGroup, CompletionItem[]>

const tokenGroupPatterns = {
radius: /border-radius/,
color: /color|background|border(?!-radius)|outline|background-color/,
elevation: /box-shadow/,
input: /box-shadow/,
typography: /font|letter-spacing|line-height/,
font: /font|letter-spacing|line-height/,
transition: /transition/,
opacity: /opacity/,
'z-index': /z-index/,
radius: /border-radius:/,
color: /color:|background:|border(?!-radius):|outline:|background-color:/,
elevation: /box-shadow:/,
input: /box-shadow:/,
typography: /font:|letter-spacing|line-height:/,
font: /font:|letter-spacing:|line-height:/,
transition: /transition:/,
opacity: /opacity:/,
'z-index': /z-index:/,
} satisfies Record<TokenGroup, RegExp>

const allCompletionItems = Object.values(completionItemsByTokenGroup).flat()
Expand All @@ -69,7 +69,7 @@ connection.onInitialize(() => {
textDocumentSync: TextDocumentSyncKind.Incremental,
// Tell the client that this server supports code completion.
completionProvider: {
triggerCharacters: ['--'],
resolveProvider: true,
},
},
}
Expand Down Expand Up @@ -97,7 +97,11 @@ connection.onCompletion(
end: { line: _textDocumentPosition.position.line, character: 1000 },
})

if (!currentText.includes('var(')) {
if (
Object.values(tokenGroupPatterns).every(
(pattern) => !pattern.test(currentText)
)
) {
return []
}

Expand Down
Loading