Skip to content

Commit 22d4ee0

Browse files
committed
feat: add completion for utility with values fn
1 parent 67d10dc commit 22d4ee0

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

packages/language-server/src/tokens/setup-tokens-helpers.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ export function setupTokensHelpers(setup: PandaExtensionSetup) {
419419
try {
420420
return await findClosestToken(node, stack, ({ propName, propNode, shorthand }) => {
421421
if (!box.isLiteral(propNode)) return undefined
422+
console.log({ propNode, shorthand })
422423
return getCompletionFor({ ctx, propName, propNode, settings, shorthand })
423424
})
424425
} catch (err) {
@@ -636,20 +637,50 @@ const getCompletionFor = ({
636637

637638
// token(colors.red.300) -> category = "colors"
638639
// color="red.300" -> no category, need to find it
640+
let propValues: Record<string, string> | undefined
639641
if (!category) {
640642
const utility = ctx.config.utilities?.[propName]
641-
if (typeof utility?.values === 'string' && utility?.values) {
643+
if (!utility?.values) return
644+
645+
// values: "spacing"
646+
if (typeof utility?.values === 'string') {
642647
category = utility.values
648+
} else if (typeof utility.values === 'function') {
649+
// values: (theme) => { ...theme("spacing") }
650+
const record = ctx.utility.getPropertyValues(utility)
651+
if (record) {
652+
if (record.type) category = record.type
653+
else propValues = record
654+
}
643655
}
644656
}
645657

658+
// values: { "1": "1px", "2": "2px", ... }
659+
if (propValues) {
660+
const items = [] as CompletionItem[]
661+
Object.entries(propValues).map(([name, value]) => {
662+
// margin: "2" -> ['var(--spacing-2)', 'var(--spacing-12)', 'var(--spacing-20)', ...]
663+
if (str && !name.includes(str)) return
664+
665+
items.push({
666+
data: { propName, token: getTokenFromPropValue(ctx, propName, value), shorthand },
667+
label: name,
668+
kind: CompletionItemKind.EnumMember,
669+
sortText: '-' + getSortText(name),
670+
preselect: false,
671+
})
672+
})
673+
674+
return items
675+
}
676+
646677
if (!category) return []
647678

648-
const values = ctx.tokens.categoryMap.get(category)
649-
if (!values) return []
679+
const categoryValues = ctx.tokens.categoryMap.get(category!)
680+
if (!categoryValues) return []
650681

651682
const items = [] as CompletionItem[]
652-
values.forEach((token, name) => {
683+
categoryValues.forEach((token, name) => {
653684
if (str && !name.includes(str)) return
654685

655686
const isColor = token.extensions.category === 'colors'

packages/vscode/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export async function activate(context: vscode.ExtensionContext) {
113113
const editor = vscode.window.visibleTextEditors.find((editor) => editor.document === document)
114114

115115
editor?.setDecorations(
116-
colorDecorationType,
116+
colorDecorationType,
117117
colors.map(({ range, color }) => {
118118
return {
119119
range,
@@ -126,7 +126,7 @@ export async function activate(context: vscode.ExtensionContext) {
126126
},
127127
}
128128
}),
129-
)
129+
)
130130
return []
131131
},
132132
},
@@ -166,7 +166,8 @@ export async function activate(context: vscode.ExtensionContext) {
166166
// synchronize the extension settings with the TS server plugin
167167
// so that we can disable removing built-ins from the completion list if the user has disabled completions in the settings
168168
context.subscriptions.push(
169-
vscode.workspace.onDidChangeConfiguration(() => {
169+
vscode.workspace.onDidChangeConfiguration((update) => {
170+
debug && console.log('onDidChangeConfiguration', update)
170171
if (!tsApi) return
171172

172173
const settings = getFreshPandaSettings()

packages/vscode/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
watch: ['../language-server/src/**/*', '../ts-plugin/src/**/*', '../shared/src/**/*'],
66
format: ['cjs'],
77
external: ['vscode', 'esbuild'],
8-
minify: false,
8+
minify: true,
99
outDir: 'dist',
1010
clean: true,
1111
shims: true,

0 commit comments

Comments
 (0)