Skip to content

Commit d2bd6b1

Browse files
committed
wip
1 parent eb93db6 commit d2bd6b1

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { spliceChangesIntoString } from './splice-changes-into-string'
2+
13
export type Comment = { index: number; value: string }
24

35
export function applyComments(str: string, comments: Comment[]): string {
4-
let offset = 0
5-
6-
for (let comment of comments) {
7-
let index = comment.index + offset
8-
let commentStr = ` /* ${comment.value} */`
9-
str = str.slice(0, index) + commentStr + str.slice(index)
10-
offset += commentStr.length
11-
}
12-
13-
return str
6+
return spliceChangesIntoString(
7+
str,
8+
comments.map((c) => ({
9+
start: c.index,
10+
end: c.index,
11+
replacement: ` /* ${c.value} */`,
12+
})),
13+
)
1414
}

packages/tailwindcss-language-service/src/util/rewriting/add-theme-values.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { evaluateExpression } from './calc'
44
import { replaceCssVars, replaceCssCalc, Range } from './replacements'
55
import { addPixelEquivalentsToValue } from '../pixelEquivalents'
66
import { applyComments, Comment } from '../comments'
7+
import { getEquivalentColor } from '../colorEquivalents'
78

89
export function addThemeValues(css: string, state: State, settings: TailwindCssSettings) {
910
let comments: Comment[] = []
@@ -17,6 +18,9 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
1718
let value = state.designSystem.resolveThemeValue?.(name) ?? null
1819
if (value === null) return null
1920

21+
// Inline CSS calc expressions in theme values
22+
value = replaceCssCalc(value, (expr) => evaluateExpression(expr.value))
23+
2024
return value
2125
})
2226

@@ -38,6 +42,16 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
3842
return null
3943
}
4044

45+
let color = getEquivalentColor(evaluated)
46+
if (color !== evaluated) {
47+
comments.push({
48+
index: expr.range.end + 1,
49+
value: `${evaluated} = ${color}`,
50+
})
51+
52+
return null
53+
}
54+
4155
comments.push({
4256
index: expr.range.end + 1,
4357
value: evaluated,
@@ -68,6 +82,25 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
6882
return null
6983
}
7084

85+
let color = getEquivalentColor(value)
86+
if (color !== value) {
87+
comments.push({
88+
index: range.end + 1,
89+
value: `${value} = ${color}`,
90+
})
91+
92+
return null
93+
}
94+
95+
// Inline CSS calc expressions in theme values
96+
value = replaceCssCalc(value, (expr) => {
97+
let evaluated = evaluateExpression(expr.value)
98+
if (!evaluated) return null
99+
if (evaluated === expr.value) return null
100+
101+
return `calc(${expr.value}) ≈ ${evaluated}`
102+
})
103+
71104
comments.push({
72105
index: range.end + 1,
73106
value,
@@ -76,7 +109,5 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
76109
return null
77110
})
78111

79-
comments.sort((a, b) => a.index - b.index)
80-
81112
return applyComments(css, comments)
82113
}

packages/tailwindcss-language-service/src/util/rewriting/index.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ test('Evaluating CSS calc expressions', () => {
6565
expect(replaceCssCalc('calc(1rem + 1px)', (node) => evaluateExpression(node.value))).toBe(
6666
'calc(1rem + 1px)',
6767
)
68+
69+
expect(replaceCssCalc('calc(1.25 / 0.875)', (node) => evaluateExpression(node.value))).toBe(
70+
'1.4286',
71+
)
6872
})
6973

70-
test('Inlicing calc expressions using the design system', () => {
71-
let map = new Map<string, string>([['--spacing', '0.25rem']])
74+
test('Inlining calc expressions using the design system', () => {
75+
let map = new Map<string, string>([
76+
['--spacing', '0.25rem'],
77+
['--color-red-500', 'oklch(0.637 0.237 25.331)'],
78+
])
7279

7380
let state: State = {
7481
enabled: true,
@@ -81,6 +88,8 @@ test('Inlicing calc expressions using the design system', () => {
8188
rootFontSize: 10,
8289
} as any
8390

91+
// Inlining calc expressions
92+
// + pixel equivalents
8493
expect(addThemeValues('calc(var(--spacing) * 4)', state, settings)).toBe(
8594
'calc(var(--spacing) * 4) /* 1rem = 10px */',
8695
)
@@ -108,4 +117,9 @@ test('Inlicing calc expressions using the design system', () => {
108117
expect(addThemeValues('calc(var(--spacing) + 1px)', state, settings)).toBe(
109118
'calc(var(--spacing) /* 0.25rem = 2.5px */ + 1px)',
110119
)
120+
121+
// Color equivalents
122+
expect(addThemeValues('var(--color-red-500)', state, settings)).toBe(
123+
'var(--color-red-500) /* oklch(0.637 0.237 25.331) = #fb2c36 */',
124+
)
111125
})

0 commit comments

Comments
 (0)