Skip to content

Commit 101ba76

Browse files
authored
feat(language-server): improved token parsing on utilities (#69)
1 parent a5efba8 commit 101ba76

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

.changeset/heavy-kiwis-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@pandacss/language-server': minor
3+
---
4+
5+
improved token parsing for utilities

packages/language-server/__tests__/get-token.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,27 @@ test('zIndex: 1', () => {
1414

1515
test('margin: 2', () => {
1616
const ctx = createContext()
17-
expect(getTokenFromPropValue(ctx, 'margin', '2')).toMatchInlineSnapshot(`undefined`)
17+
expect(getTokenFromPropValue(ctx, 'margin', '2')).toMatchInlineSnapshot(`
18+
_Token {
19+
"description": undefined,
20+
"extensions": {
21+
"category": "spacing",
22+
"condition": "base",
23+
"pixelValue": "8px",
24+
"prop": "2",
25+
"var": "--spacing-2",
26+
"varRef": "var(--spacing-2)",
27+
},
28+
"name": "spacing.2",
29+
"originalValue": "0.5rem",
30+
"path": [
31+
"spacing",
32+
"2",
33+
],
34+
"type": "dimension",
35+
"value": "0.5rem",
36+
}
37+
`)
1838
})
1939

2040
test('CSS var', () => {
@@ -169,6 +189,31 @@ test('color: #fff', () => {
169189
`)
170190
})
171191

192+
test('width: xs', () => {
193+
const ctx = createContext()
194+
expect(getTokenFromPropValue(ctx, 'width', 'xs')).toMatchInlineSnapshot(`
195+
_Token {
196+
"description": undefined,
197+
"extensions": {
198+
"category": "sizes",
199+
"condition": "base",
200+
"pixelValue": "320px",
201+
"prop": "xs",
202+
"var": "--sizes-xs",
203+
"varRef": "var(--sizes-xs)",
204+
},
205+
"name": "sizes.xs",
206+
"originalValue": "20rem",
207+
"path": [
208+
"sizes",
209+
"xs",
210+
],
211+
"type": undefined,
212+
"value": "20rem",
213+
}
214+
`)
215+
})
216+
172217
test('color: green', () => {
173218
const ctx = createContext()
174219
expect(getTokenFromPropValue(ctx, 'color', 'green')).toMatchInlineSnapshot(`

packages/language-server/src/tokens/get-token.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,37 @@ const getColorExtensions = (value: string, kind: string) => {
1414
export const getTokenFromPropValue = (ctx: PandaContext, prop: string, value: string): Token | undefined => {
1515
const utility = ctx.config.utilities?.[prop]
1616

17-
const category = typeof utility?.values === 'string' && utility?.values
18-
if (!category) return
17+
const potentialCategories: string[] = []
1918

20-
const tokenPath = [category, value].join('.')
21-
const token = ctx.tokens.getByName(tokenPath)
19+
if (typeof utility?.values === 'string' && utility?.values) {
20+
potentialCategories.push(utility?.values)
21+
}
22+
23+
if (typeof utility?.values === 'function' && ctx.config.theme) {
24+
// Invoke the utility value function and capture categories potentially used by consumer
25+
utility.values((token: string) => {
26+
potentialCategories.push(token)
27+
})
28+
}
29+
30+
if (!potentialCategories.length) return
31+
32+
// Attempt to locate a token
33+
const matchedToken = potentialCategories
34+
.map((category) => {
35+
return [category, value].join('.')
36+
})
37+
.map((tokenPath) => {
38+
return {
39+
token: ctx.tokens.getByName(tokenPath),
40+
tokenPath,
41+
}
42+
})
43+
.find((t) => t.token !== undefined)
44+
45+
const token = matchedToken?.token
46+
// If token was located use the first category and value
47+
const tokenPath = matchedToken?.tokenPath ?? [potentialCategories[0], value].join('.')
2248

2349
// arbitrary value like
2450
// display: "block", zIndex: 1, ...

0 commit comments

Comments
 (0)