2020-10-08 15:20:54 +00:00
|
|
|
import { State } from './util/state'
|
|
|
|
import {
|
|
|
|
findClassListsInDocument,
|
|
|
|
getClassNamesInClassList,
|
|
|
|
findHelperFunctionsInDocument,
|
|
|
|
} from './util/find'
|
2021-05-03 17:00:04 +00:00
|
|
|
import { getColor, getColorFromValue, tinyColorToVscodeColor } from './util/color'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { stringToPath } from './util/stringToPath'
|
2021-05-03 17:00:04 +00:00
|
|
|
import type { TextDocument, ColorInformation } from 'vscode-languageserver'
|
|
|
|
import { TinyColor } from '@ctrl/tinycolor'
|
|
|
|
import dlv from 'dlv'
|
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)
|
|
|
|
if (settings.colorDecorators === 'off') return colors
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
let classLists = await findClassListsInDocument(state, document)
|
2020-10-08 15:20:54 +00:00
|
|
|
classLists.forEach((classList) => {
|
|
|
|
let classNames = getClassNamesInClassList(classList)
|
|
|
|
classNames.forEach((className) => {
|
2021-05-03 17:00:04 +00:00
|
|
|
let color = getColor(state, className.className)
|
2020-10-08 15:20:54 +00:00
|
|
|
if (color === null || typeof color === 'string' || color.a === 0) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-03 17:00:04 +00:00
|
|
|
colors.push({
|
|
|
|
range: className.range,
|
|
|
|
color: tinyColorToVscodeColor(color),
|
|
|
|
})
|
2020-10-08 15:20:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
let helperFns = findHelperFunctionsInDocument(state, document)
|
|
|
|
helperFns.forEach((fn) => {
|
|
|
|
let keys = stringToPath(fn.value)
|
|
|
|
let base = fn.helper === 'theme' ? ['theme'] : []
|
|
|
|
let value = dlv(state.config, [...base, ...keys])
|
|
|
|
let color = getColorFromValue(value)
|
2021-05-03 17:00:04 +00:00
|
|
|
if (color instanceof TinyColor && color.a !== 0) {
|
|
|
|
colors.push({ range: fn.valueRange, color: tinyColorToVscodeColor(color) })
|
2020-10-08 15:20:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return colors
|
|
|
|
}
|