Skip to content

Commit a13290c

Browse files
author
Brad Cornes
committed
show completion item color swatches for variable-alpha colors
1 parent 82dad05 commit a13290c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/tailwindcss-language-server/src/providers/completionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function completionsFromClassList(
8383
const color = getColor(state, [className])
8484
if (color) {
8585
kind = CompletionItemKind.Color
86-
documentation = color
86+
documentation = color.documentation
8787
}
8888
}
8989

packages/tailwindcss-language-server/src/util/color.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,35 @@ const COLOR_NAMES = {
172172
yellowgreen: '#9acd32',
173173
}
174174

175-
export function getColor(state: State, keys: string[]): string {
175+
export function getColor(
176+
state: State,
177+
keys: string[]
178+
): { documentation?: string } {
176179
const item = dlv(state.classNames.classNames, keys)
177180
if (!item.__rule) return null
178181
const props = Object.keys(removeMeta(item))
179-
if (props.length === 0 || props.length > 1) return null
180-
const prop = props[0]
182+
const nonCustomProps = props.filter((prop) => !prop.startsWith('--'))
183+
if (nonCustomProps.length !== 1) return null
184+
const prop = nonCustomProps[0]
181185
if (COLOR_PROPS.indexOf(prop) === -1) return null
182-
return COLOR_NAMES[item[prop].toLowerCase()] || item[prop]
186+
187+
const namedColor = COLOR_NAMES[item[prop].toLowerCase()]
188+
if (namedColor) {
189+
return { documentation: namedColor }
190+
}
191+
192+
// matches: rgba(<r>, <g>, <b>, var(--bg-opacity))
193+
// TODO: support other formats? e.g. hsla, css level 4
194+
const match = item[prop].match(
195+
/^\s*rgba\(\s*(?<r>[0-9]{1,3})\s*,\s*(?<g>[0-9]{1,3})\s*,\s*(?<b>[0-9]{1,3})\s*,\s*var/
196+
)
197+
if (match) {
198+
return {
199+
documentation: `rgb(${match.groups.r}, ${match.groups.g}, ${match.groups.b})`,
200+
}
201+
}
202+
203+
return {}
183204
}
184205

185206
export function isColor(str: any): boolean {

0 commit comments

Comments
 (0)