Skip to content

Commit c12cc69

Browse files
committed
Add css var() hovers in v4
Add test
1 parent 285d70d commit c12cc69

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

packages/tailwindcss-language-server/tests/hover/hover.test.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { test } from 'vitest'
1+
import { expect, test } from 'vitest'
22
import { withFixture } from '../common'
3+
import { css, defineTest } from '../../src/testing'
4+
import { createClient } from '../utils/client'
35

46
withFixture('basic', (c) => {
57
async function testHover(
@@ -545,3 +547,58 @@ withFixture('v4/path-mappings', (c) => {
545547
},
546548
})
547549
})
550+
551+
defineTest({
552+
name: 'wip',
553+
fs: {
554+
'app.css': css`
555+
@import 'tailwindcss';
556+
`,
557+
},
558+
559+
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
560+
561+
handle: async ({ client }) => {
562+
let doc = await client.open({
563+
lang: 'css',
564+
text: css`
565+
.foo {
566+
color: theme(--color-black);
567+
}
568+
.bar {
569+
color: var(--color-black);
570+
}
571+
`,
572+
})
573+
574+
// color: theme(--color-black);
575+
// ^
576+
let hoverTheme = await doc.hover({ line: 1, character: 18 })
577+
578+
// color: var(--color-black);
579+
// ^
580+
let hoverVar = await doc.hover({ line: 4, character: 16 })
581+
582+
expect(hoverTheme).toEqual({
583+
contents: {
584+
kind: 'markdown',
585+
value: ['```plaintext', '#000', '```'].join('\n'),
586+
},
587+
range: {
588+
start: { line: 1, character: 15 },
589+
end: { line: 1, character: 28 },
590+
},
591+
})
592+
593+
expect(hoverVar).toEqual({
594+
contents: {
595+
kind: 'markdown',
596+
value: ['```plaintext', '#000', '```'].join('\n'),
597+
},
598+
range: {
599+
start: { line: 4, character: 13 },
600+
end: { line: 4, character: 26 },
601+
},
602+
})
603+
},
604+
})

packages/tailwindcss-language-service/src/hoverProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ async function provideCssHelperHover(
5353
for (let helperFn of helperFns) {
5454
if (!isWithinRange(position, helperFn.ranges.path)) continue
5555

56+
if (helperFn.helper === 'var' && !state.v4) continue
57+
5658
let validated = validateConfigPath(
5759
state,
5860
helperFn.path,

packages/tailwindcss-language-service/src/util/find.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export function findHelperFunctionsInRange(
405405
): DocumentHelperFunction[] {
406406
const text = getTextWithoutComments(doc, 'css', range)
407407
let matches = findAll(
408-
/(?<prefix>[\W])(?<helper>config|theme|--theme)(?<innerPrefix>\(\s*)(?<path>[^)]*?)\s*\)/g,
408+
/(?<prefix>[\W])(?<helper>config|theme|--theme|var)(?<innerPrefix>\(\s*)(?<path>[^)]*?)\s*\)/g,
409409
text,
410410
)
411411

@@ -450,10 +450,12 @@ export function findHelperFunctionsInRange(
450450
match.groups.helper.length +
451451
match.groups.innerPrefix.length
452452

453-
let helper: 'config' | 'theme' = 'config'
453+
let helper: 'config' | 'theme' | 'var' = 'config'
454454

455455
if (match.groups.helper === 'theme' || match.groups.helper === '--theme') {
456456
helper = 'theme'
457+
} else if (match.groups.helper === 'var') {
458+
helper = 'var'
457459
}
458460

459461
return {

packages/tailwindcss-language-service/src/util/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export type DocumentClassName = {
161161
}
162162

163163
export type DocumentHelperFunction = {
164-
helper: 'theme' | 'config'
164+
helper: 'theme' | 'config' | 'var'
165165
path: string
166166
ranges: {
167167
full: Range

0 commit comments

Comments
 (0)