Skip to content

Commit 2f7f86f

Browse files
committed
Refactor document colors provider
This uses the new virtual document abstraction to simplify the implementation of the provider. Now it operates only on things it cares about: - utility class names - CSS helper functions
1 parent 16c13de commit 2f7f86f

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
1-
import type { State } from './util/state'
2-
import {
3-
findClassListsInDocument,
4-
getClassNamesInClassList,
5-
findHelperFunctionsInDocument,
6-
} from './util/find'
1+
import type { ColorInformation } from 'vscode-languageserver'
2+
import type { Document } from './documents/document'
73
import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/color'
84
import { stringToPath } from './util/stringToPath'
9-
import type { ColorInformation } from 'vscode-languageserver'
10-
import type { TextDocument } from 'vscode-languageserver-textdocument'
115
import dlv from 'dlv'
126
import { dedupeByRange } from './util/array'
137

14-
export async function getDocumentColors(
15-
state: State,
16-
document: TextDocument,
17-
): Promise<ColorInformation[]> {
8+
export function getDocumentColors(doc: Document): ColorInformation[] {
189
let colors: ColorInformation[] = []
19-
if (!state.enabled) return colors
2010

21-
let settings = await state.editor.getConfiguration(document.uri)
22-
if (settings.tailwindCSS.colorDecorators === false) return colors
11+
for (let className of doc.classNames()) {
12+
let color = getColor(doc.state, className.className)
13+
if (!color) continue
14+
if (typeof color === 'string') continue
15+
if ((color.alpha ?? 1) === 0) continue
2316

24-
let classLists = await findClassListsInDocument(state, document)
25-
classLists.forEach((classList) => {
26-
let classNames = getClassNamesInClassList(classList, state.blocklist)
27-
classNames.forEach((className) => {
28-
let color = getColor(state, className.className)
29-
if (color === null || typeof color === 'string' || (color.alpha ?? 1) === 0) {
30-
return
31-
}
32-
colors.push({
33-
range: className.range,
34-
color: culoriColorToVscodeColor(color),
35-
})
17+
colors.push({
18+
range: className.range,
19+
color: culoriColorToVscodeColor(color),
3620
})
37-
})
21+
}
3822

39-
let helperFns = findHelperFunctionsInDocument(state, document)
40-
helperFns.forEach((fn) => {
23+
for (let fn of doc.helperFns()) {
4124
let keys = stringToPath(fn.path)
4225
let base = fn.helper === 'theme' ? ['theme'] : []
43-
let value = dlv(state.config, [...base, ...keys])
26+
let value = dlv(doc.state.config, [...base, ...keys])
27+
4428
let color = getColorFromValue(value)
45-
if (color && typeof color !== 'string' && (color.alpha ?? 1) !== 0) {
46-
colors.push({ range: fn.ranges.path, color: culoriColorToVscodeColor(color) })
47-
}
48-
})
29+
if (!color) continue
30+
if (typeof color === 'string') continue
31+
if ((color.alpha ?? 1) === 0) continue
32+
33+
colors.push({
34+
range: fn.ranges.path,
35+
color: culoriColorToVscodeColor(color),
36+
})
37+
}
4938

5039
return dedupeByRange(colors)
5140
}

packages/tailwindcss-language-service/src/service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async function createLanguageDocument(
139139
async documentColors() {
140140
if (!state.enabled || !settings.tailwindCSS.colorDecorators) return []
141141

142-
return getDocumentColors(state, doc.storage)
142+
return getDocumentColors(doc)
143143
},
144144

145145
async colorPresentation(color: Color, range: Range) {

0 commit comments

Comments
 (0)