diff --git a/src/lsp/providers/completionProvider.ts b/src/lsp/providers/completionProvider.ts index 17fedcb8581c921f0c7045b8a86cc681890c264e..795127cc7c02bfbf8c198e44378cbd6f7cc9bedb 100644 --- a/src/lsp/providers/completionProvider.ts +++ b/src/lsp/providers/completionProvider.ts @@ -89,11 +89,9 @@ label += sep sortText = '-' + sortText // move to top } else { const color = getColor(state, [className]) - if (color !== null) { + if (color) { kind = CompletionItemKind.Color - if (typeof color !== 'string' && color.a !== 0) { - documentation = color.toRgbString() - } + documentation = color.documentation } } diff --git a/src/lsp/providers/documentColorProvider.ts b/src/lsp/providers/documentColorProvider.ts index 876f5777baef5960ee9a9bae262af5c1fc07117d..688ee746ab6a01dca0217c5515e14e599bb1f235 100644 --- a/src/lsp/providers/documentColorProvider.ts +++ b/src/lsp/providers/documentColorProvider.ts @@ -27,10 +27,8 @@ classNames.forEach((className) => { let parts = getClassNameParts(state, className.className) if (!parts) return let color = getColor(state, parts) - if (color === null || typeof color === 'string' || color.a === 0) { - return - } - colors.push({ range: className.range, color: color.toRgbString() }) + if (!color) return + colors.push({ range: className.range, color: color.documentation }) }) }) diff --git a/src/lsp/util/color.ts b/src/lsp/util/color.ts index 436a94c42d4f6e76d3e558dfd283b5c5b890bdd2..31d56ec33a369f671737f625a3d61c8960ca7d4b 100644 --- a/src/lsp/util/color.ts +++ b/src/lsp/util/color.ts @@ -21,14 +21,10 @@ 'stroke', 'text-decoration-color', ] -function isKeyword(value: string): boolean { - return ['transparent', 'currentcolor'].includes(value.toLowerCase()) -} - export function getColor( state: State, keys: string[] -): TinyColor | string | null { +): { documentation?: string } { const item = dlv(state.classNames.classNames, keys) if (!item.__rule) return null const props = Object.keys(removeMeta(item)) @@ -52,36 +48,40 @@ propsToCheck.map((prop) => ensureArray(item[prop]).map(createColor)) ) // check that all of the values are valid colors - if (colors.some((color) => typeof color !== 'string' && !color.isValid)) { + if (colors.some((color) => color !== 'transparent' && !color.isValid)) { return null } // check that all of the values are the same color, ignoring alpha const colorStrings = dedupe( colors.map((color) => - typeof color === 'string' ? color : `${color.r}-${color.g}-${color.b}` + color === 'transparent' + ? 'transparent' + : `${color.r}-${color.g}-${color.b}` ) ) if (colorStrings.length !== 1) { return null } - if (isKeyword(colorStrings[0])) { - return colorStrings[0] + if (colorStrings[0] === 'transparent') { + return { + documentation: 'rgba(0, 0, 0, 0.01)', + } } - const nonKeywordColors = colors.filter( - (color): color is TinyColor => typeof color !== 'string' + const nonTransparentColors = colors.filter( + (color): color is TinyColor => color !== 'transparent' ) - const alphas = dedupe(nonKeywordColors.map((color) => color.a)) + const alphas = dedupe(nonTransparentColors.map((color) => color.a)) - if (alphas.length === 1) { - return nonKeywordColors[0] - } - - if (alphas.length === 2 && alphas.includes(0)) { - return nonKeywordColors.find((color) => color.a !== 0) + if (alphas.length === 1 || (alphas.length === 2 && alphas.includes(0))) { + return { + documentation: nonTransparentColors + .find((color) => color.a !== 0) + .toRgbString(), + } } return null @@ -99,9 +99,9 @@ } return null } -function createColor(str: string): TinyColor | string { - if (isKeyword(str)) { - return str +function createColor(str: string): TinyColor | 'transparent' { + if (str === 'transparent') { + return 'transparent' } // matches: rgba(, , , var(--bg-opacity))