-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathannotation.ts
51 lines (43 loc) · 1.64 KB
/
annotation.ts
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
50
51
import type { Range, TextDocument } from 'vscode-languageserver-textdocument'
import type { State } from './util/state'
export async function updateAnnotation(state: State, document: TextDocument): Promise<Range[]> {
const text = document.getText()
const extractorContext = {
tailwindConfig: {
separator: '-',
prefix: '',
},
}
if (state.jitContext?.tailwindConfig?.separator) {
extractorContext.tailwindConfig.separator = state.jitContext.tailwindConfig.separator
}
if (state.jitContext?.tailwindConfig?.prefix) {
extractorContext.tailwindConfig.prefix = state.jitContext.tailwindConfig.prefix
}
const classNames = state.modules.defaultExtractor.module(extractorContext)(text) as string[]
const result: Range[] = []
if (state.v4) {
const rules = state.designSystem.compile(classNames)
let index = 0
classNames.forEach((className, i) => {
const start = text.indexOf(className, index)
const end = start + className.length
if (rules.at(i).nodes.length > 0 && start !== -1) {
result.push({ start: document.positionAt(start), end: document.positionAt(end) })
}
index = end
})
} else if (state.jit) {
const rules = state.modules.jit.generateRules.module(classNames, state.jitContext)
let index = 0
classNames.forEach((className) => {
const start = text.indexOf(className, index)
const end = start + className.length
if (rules?.find(([, i]) => (i.raws.tailwind as any)?.candidate === className)) {
result.push({ start: document.positionAt(start), end: document.positionAt(end) })
}
index = end
})
}
return result
}