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 3 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
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