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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts
index 15bb694425d1774166afce91396511166266fc2d..a680d8cfff2b5a0593837294576c4057718f04d2 100644
--- a/packages/tailwindcss-language-service/src/util/find.ts
+++ b/packages/tailwindcss-language-service/src/util/find.ts
@@ -17,6 +17,9 @@ getComputedClassAttributeLexer,
} from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange'
+import { getDocumentSettings } from './getDocumentSettings'
+const dlv = require('dlv')
+import { createMultiRegexp } from './createMultiRegexp'
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
let match: RegExpMatchArray
@@ -77,20 +80,28 @@ }
return names
}
-export function findClassNamesInRange(
+export async function findClassNamesInRange(
+ state: State,
doc: TextDocument,
range?: Range,
- mode?: 'html' | 'css'
-): DocumentClassName[] {
- const classLists = findClassListsInRange(doc, range, mode)
+ mode?: 'html' | 'css',
+ includeCustom: boolean = true
+): Promise<DocumentClassName[]> {
+ const classLists = await findClassListsInRange(
+ state,
+ doc,
+ range,
+ mode,
+ includeCustom
+ )
return flatten(classLists.map(getClassNamesInClassList))
}
-export function findClassNamesInDocument(
+export async function findClassNamesInDocument(
state: State,
doc: TextDocument
-): DocumentClassName[] {
- const classLists = findClassListsInDocument(state, doc)
+): Promise<DocumentClassName[]> {
+ const classLists = await findClassListsInDocument(state, doc)
return flatten(classLists.map(getClassNamesInClassList))
}
@@ -130,12 +141,77 @@ }
})
}
+async function findCustomClassLists(
+ state: State,
+ doc: TextDocument,
+ range?: Range
+): Promise<DocumentClassList[]> {
+ const settings = await getDocumentSettings(state, doc)
+ const regexes = dlv(settings, 'experimental.classRegex', [])
+
+ if (!Array.isArray(regexes) || regexes.length === 0) return []
+
+ const text = doc.getText(range)
+ const result: DocumentClassList[] = []
+
+ for (let i = 0; i < regexes.length; i++) {
+ try {
+ let [containerRegex, classRegex] = Array.isArray(regexes[i])
+ ? regexes[i]
+ : [regexes[i]]
+
+ containerRegex = createMultiRegexp(containerRegex)
+ let containerMatch
+
+ while ((containerMatch = containerRegex.exec(text)) !== null) {
+ const searchStart = doc.offsetAt(
+ range?.start || { line: 0, character: 0 }
+ )
+ const matchStart = searchStart + containerMatch.start
+ const matchEnd = searchStart + containerMatch.end
+
+ if (classRegex) {
+ classRegex = createMultiRegexp(classRegex)
+ let classMatch
+
+ while (
+ (classMatch = classRegex.exec(containerMatch.match)) !== null
+ ) {
+ const classMatchStart = matchStart + classMatch.start
+ const classMatchEnd = matchStart + classMatch.end
+ result.push({
+ classList: classMatch.match,
+ range: {
+ start: doc.positionAt(classMatchStart),
+ end: doc.positionAt(classMatchEnd),
+ },
+ })
+ }
+ } else {
+ result.push({
+ classList: containerMatch.match,
+ range: {
+ start: doc.positionAt(matchStart),
+ end: doc.positionAt(matchEnd),
+ },
+ })
+ }
+ }
+ } catch (_) {}
+ }
+
+ return result
+}
+
export function findClassListsInHtmlRange(
doc: TextDocument,
range?: Range
): DocumentClassList[] {
const text = doc.getText(range)
- const matches = findAll(/(?:\s|:)(?:class(?:Name)?|\[ngClass\])=['"`{]/g, text)
+ const matches = findAll(
+ /(?:\s|:)(?:class(?:Name)?|\[ngClass\])=['"`{]/g,
+ text
+ )
const result: DocumentClassList[] = []
matches.forEach((match) => {
@@ -232,21 +308,29 @@
return result
}
-export function findClassListsInRange(
+export async function findClassListsInRange(
+ state: State,
doc: TextDocument,
range?: Range,
- mode?: 'html' | 'css'
-): DocumentClassList[] {
+ mode?: 'html' | 'css',
+ includeCustom: boolean = true
+): Promise<DocumentClassList[]> {
+ let classLists: DocumentClassList[]
if (mode === 'css') {
- return findClassListsInCssRange(doc, range)
+ classLists = findClassListsInCssRange(doc, range)
+ } else {
+ classLists = findClassListsInHtmlRange(doc, range)
}
- return findClassListsInHtmlRange(doc, range)
+ return [
+ ...classLists,
+ ...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
+ ]
}
-export function findClassListsInDocument(
+export async function findClassListsInDocument(
state: State,
doc: TextDocument
-): DocumentClassList[] {
+): Promise<DocumentClassList[]> {
if (isCssDoc(state, doc)) {
return findClassListsInCssRange(doc)
}
@@ -257,6 +341,7 @@
return flatten([
...boundaries.html.map((range) => findClassListsInHtmlRange(doc, range)),
...boundaries.css.map((range) => findClassListsInCssRange(doc, range)),
+ await findCustomClassLists(state, doc),
])
}
@@ -323,11 +408,11 @@ const { line, col } = lineColumn(str + '\n', index)
return { line: line - 1, character: col - 1 }
}
-export function findClassNameAtPosition(
+export async function findClassNameAtPosition(
state: State,
doc: TextDocument,
position: Position
-): DocumentClassName {
+): Promise<DocumentClassName> {
let classNames = []
const searchRange = {
start: { line: Math.max(position.line - 10, 0), character: 0 },
@@ -335,12 +420,12 @@ end: { line: position.line + 10, character: 0 },
}
if (isCssContext(state, doc, position)) {
- classNames = findClassNamesInRange(doc, searchRange, 'css')
+ classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
} else if (
isHtmlContext(state, doc, position) ||
isJsContext(state, doc, position)
) {
- classNames = findClassNamesInRange(doc, searchRange, 'html')
+ classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
}
if (classNames.length === 0) {
|