2020-10-08 15:20:54 +00:00
|
|
|
import { State } from './util/state'
|
|
|
|
import {
|
|
|
|
findClassListsInDocument,
|
|
|
|
getClassNamesInClassList,
|
|
|
|
findHelperFunctionsInDocument,
|
|
|
|
} from './util/find'
|
2021-10-01 13:11:45 +00:00
|
|
|
import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/color'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { stringToPath } from './util/stringToPath'
|
2023-05-15 10:21:43 +00:00
|
|
|
import type { ColorInformation } from 'vscode-languageserver'
|
|
|
|
import type { TextDocument } from 'vscode-languageserver-textdocument'
|
2021-05-03 17:00:04 +00:00
|
|
|
import dlv from 'dlv'
|
2022-11-04 10:56:23 +00:00
|
|
|
import { dedupeByRange } from './util/array'
|
2020-10-08 15:20:54 +00:00
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
export async function getDocumentColors(
|
|
|
|
state: State,
|
|
|
|
document: TextDocument
|
|
|
|
): Promise<ColorInformation[]> {
|
|
|
|
let colors: ColorInformation[] = []
|
2020-10-08 15:20:54 +00:00
|
|
|
if (!state.enabled) return colors
|
|
|
|
|
2021-05-03 17:00:04 +00:00
|
|
|
let settings = await state.editor.getConfiguration(document.uri)
|
2021-05-04 11:40:50 +00:00
|
|
|
if (settings.tailwindCSS.colorDecorators === false) return colors
|
2021-05-03 17:00:04 +00:00
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
let classLists = await findClassListsInDocument(state, document)
|
2022-04-13 21:24:02 +00:00
|
|
|
classLists.forEach((classList) => {
|
2023-03-27 17:56:02 +00:00
|
|
|
let classNames = getClassNamesInClassList(classList, state.blocklist)
|
2020-10-08 15:20:54 +00:00
|
|
|
classNames.forEach((className) => {
|
2021-05-03 17:00:04 +00:00
|
|
|
let color = getColor(state, className.className)
|
2021-10-01 13:11:45 +00:00
|
|
|
if (color === null || typeof color === 'string' || (color.alpha ?? 1) === 0) {
|
2020-10-08 15:20:54 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-03 17:00:04 +00:00
|
|
|
colors.push({
|
|
|
|
range: className.range,
|
2021-10-01 13:11:45 +00:00
|
|
|
color: culoriColorToVscodeColor(color),
|
2021-05-03 17:00:04 +00:00
|
|
|
})
|
2020-10-08 15:20:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
let helperFns = findHelperFunctionsInDocument(state, document)
|
|
|
|
helperFns.forEach((fn) => {
|
2022-10-17 16:56:00 +00:00
|
|
|
let keys = stringToPath(fn.path)
|
2020-10-08 15:20:54 +00:00
|
|
|
let base = fn.helper === 'theme' ? ['theme'] : []
|
|
|
|
let value = dlv(state.config, [...base, ...keys])
|
|
|
|
let color = getColorFromValue(value)
|
2021-10-01 13:11:45 +00:00
|
|
|
if (color && typeof color !== 'string' && (color.alpha ?? 1) !== 0) {
|
2022-10-17 16:56:00 +00:00
|
|
|
colors.push({ range: fn.ranges.path, color: culoriColorToVscodeColor(color) })
|
2020-10-08 15:20:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-11-04 10:56:23 +00:00
|
|
|
return dedupeByRange(colors)
|
2020-10-08 15:20:54 +00:00
|
|
|
}
|