Home

tailwind-ctp-intellisense @53743f2faa83c7a16f05234ce03377077655fa3d - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / packages / tailwindcss-language-service / src / documentColorProvider.ts
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { State } from './util/state'
import {
  findClassListsInDocument,
  getClassNamesInClassList,
  findHelperFunctionsInDocument,
} from './util/find'
import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/color'
import { stringToPath } from './util/stringToPath'
import type { TextDocument, ColorInformation } from 'vscode-languageserver'
import dlv from 'dlv'

export async function getDocumentColors(
  state: State,
  document: TextDocument
): Promise<ColorInformation[]> {
  let colors: ColorInformation[] = []
  if (!state.enabled) return colors

  let settings = await state.editor.getConfiguration(document.uri)
  if (settings.tailwindCSS.colorDecorators === false) return colors

  let classLists = await findClassListsInDocument(state, document)
  classLists.forEach((classList) => {
    let classNames = getClassNamesInClassList(classList)
    classNames.forEach((className) => {
      let color = getColor(state, className.className)
      if (color === null || typeof color === 'string' || (color.alpha ?? 1) === 0) {
        return
      }
      colors.push({
        range: className.range,
        color: culoriColorToVscodeColor(color),
      })
    })
  })

  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)
    if (color && typeof color !== 'string' && (color.alpha ?? 1) !== 0) {
      colors.push({ range: fn.valueRange, color: culoriColorToVscodeColor(color) })
    }
  })

  return colors
}