Skip to content

v4: Show theme key completions in var(…) #1274

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

Merged
merged 6 commits into from
Mar 20, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect } from 'vitest'
import { withFixture } from '../common'
import { css, defineTest, html, js } from '../../src/testing'
import { createClient } from '../utils/client'
import { CompletionItemKind } from 'vscode-languageserver'

function buildCompletion(c) {
return async function completion({
Expand Down Expand Up @@ -798,3 +799,53 @@ defineTest({
expect(completion?.items.length).toBe(12289)
},
})

defineTest({
name: 'v4: Theme key completions show in var(…)',
fs: {
'app.css': css`
@import 'tailwindcss';

@theme {
--color-custom: #000;
}
`,
},
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
handle: async ({ client }) => {
let document = await client.open({
settings: {
tailwindCSS: {
classFunctions: ['clsx'],
},
},
lang: 'css',
text: css`
.foo {
color: var();
}
`,
})

// color: var();
// ^
let completion = await document.completions({ line: 1, character: 13 })

expect(completion).toEqual({
isIncomplete: false,
items: expect.arrayContaining([
// From the default theme
expect.objectContaining({ label: '--font-sans' }),

// From the `@theme` block in the CSS file
expect.objectContaining({
label: '--color-custom',

// And it's shown as a color
kind: CompletionItemKind.Color,
documentation: '#000000',
}),
]),
})
},
})
56 changes: 45 additions & 11 deletions packages/tailwindcss-language-service/src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ function provideCssHelperCompletions(

const match = text
.substr(0, text.length - 1) // don't include that extra character from earlier
.match(/[\s:;/*(){}](?<helper>config|theme|--theme)\(\s*['"]?(?<path>[^)'"]*)$/)
.match(/[\s:;/*(){}](?<helper>config|theme|--theme|var)\(\s*['"]?(?<path>[^)'"]*)$/d)

if (match === null) {
return null
Expand All @@ -1040,17 +1040,15 @@ function provideCssHelperCompletions(
end: position,
}

if (state.v4 && match.groups.helper === '--theme') {
// List known theme keys
let validThemeKeys = resolveKnownThemeKeys(state.designSystem)
if (
state.v4 &&
(match.groups.helper === '--theme' ||
match.groups.helper === 'theme' ||
match.groups.helper === 'var')
) {
let items: CompletionItem[] = themeKeyCompletions(state)

let items: CompletionItem[] = validThemeKeys.map((themeKey, index) => {
return {
label: themeKey,
sortText: naturalExpand(index, validThemeKeys.length),
kind: 9,
}
})
editRange.start.character = match.indices.groups.helper[1] + 1

return withDefaults(
{ isIncomplete: false, items },
Expand All @@ -1065,6 +1063,8 @@ function provideCssHelperCompletions(
)
}

if (match.groups.helper === 'var') return null

let base = match.groups.helper === 'config' ? state.config : dlv(state.config, 'theme', {})
let parts = path.split(/([\[\].]+)/)
let keys = parts.filter((_, i) => i % 2 === 0)
Expand Down Expand Up @@ -2486,3 +2486,37 @@ async function knownUtilityFunctionArguments(state: State, fn: UtilityFn): Promi

return args
}

export function themeKeyCompletions(state: State): CompletionItem[] {
if (!state.v4) return null
if (!state.designSystem) return null

let knownThemeKeys = resolveKnownThemeKeys(state.designSystem)

return knownThemeKeys.map((themeKey, index) => {
let value = state.designSystem.resolveThemeValue(themeKey, true)
let documentation: string | undefined

let color = getColorFromValue(value)
if (color !== null) {
if (typeof color !== 'string' && (color.alpha ?? 1) !== 0) {
documentation = formatColor(color)
}

return {
label: themeKey,
kind: CompletionItemKind.Color,
sortText: naturalExpand(index, knownThemeKeys.length),
detail: value,
documentation,
}
}

return {
label: themeKey,
kind: CompletionItemKind.Variable,
sortText: naturalExpand(index, knownThemeKeys.length),
detail: value,
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export function resolveVariableValue(design: DesignSystem, name: string) {
name = `--${name.slice(prefix.length + 3)}`
}

return design.resolveThemeValue?.(name) ?? null
return design.resolveThemeValue?.(name, true) ?? null
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface DesignSystem {
getVariants(): VariantEntry[]

// Optional because it did not exist in earlier v4 alpha versions
resolveThemeValue?(path: string): string | undefined
resolveThemeValue?(path: string, forceInline?: boolean): string | undefined
}

export interface DesignSystem {
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- v4: Add support for upcoming `@source inline(…)` feature ([#1262](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1262))
- LSP: Refresh internal caches when settings are updated ([#1273](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1273))
- LSP: Improve error message when a workspace folder does not exist or is inaccesible to the current user ([#1276](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1276))
- v4: Show theme key completions in `var(…)` in CSS ([#1274](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1274))

# 0.14.9

Expand Down