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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts
index 91b0a525ef7c0dff597d266dc51695615903218b..cc8565908851d3a709220f0e8beef85acb478697 100644
--- a/packages/tailwindcss-language-service/src/util/find.ts
+++ b/packages/tailwindcss-language-service/src/util/find.ts
@@ -1,20 +1,12 @@
import type { TextDocument, Range, Position } from 'vscode-languageserver'
-import {
- DocumentClassName,
- DocumentClassList,
- State,
- DocumentHelperFunction,
-} from './state'
+import { DocumentClassName, DocumentClassList, State, DocumentHelperFunction } from './state'
import lineColumn from 'line-column'
import { isCssContext, isCssDoc } from './css'
import { isHtmlContext, isHtmlDoc, isSvelteDoc, isVueDoc } from './html'
import { isWithinRange } from './isWithinRange'
import { isJsContext, isJsDoc } from './js'
import { flatten } from './array'
-import {
- getClassAttributeLexer,
- getComputedClassAttributeLexer,
-} from './lexers'
+import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange'
const dlv = require('dlv')
@@ -63,13 +55,11 @@ },
range: {
start: {
line: range.start.line + start.line,
- character:
- (end.line === 0 ? range.start.character : 0) + start.character,
+ character: (end.line === 0 ? range.start.character : 0) + start.character,
},
end: {
line: range.start.line + end.line,
- character:
- (end.line === 0 ? range.start.character : 0) + end.character,
+ character: (end.line === 0 ? range.start.character : 0) + end.character,
},
},
})
@@ -86,13 +76,7 @@ range?: Range,
mode?: 'html' | 'css',
includeCustom: boolean = true
): Promise<DocumentClassName[]> {
- const classLists = await findClassListsInRange(
- state,
- doc,
- range,
- mode,
- includeCustom
- )
+ const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
return flatten(classLists.map(getClassNamesInClassList))
}
@@ -104,10 +88,7 @@ const classLists = await findClassListsInDocument(state, doc)
return flatten(classLists.map(getClassNamesInClassList))
}
-export function findClassListsInCssRange(
- doc: TextDocument,
- range?: Range
-): DocumentClassList[] {
+export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
const text = doc.getText(range)
const matches = findAll(
/(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g,
@@ -117,23 +98,18 @@ const globalStart: Position = range ? range.start : { line: 0, character: 0 }
return matches.map((match) => {
const start = indexToPosition(text, match.index + match[1].length)
- const end = indexToPosition(
- text,
- match.index + match[1].length + match.groups.classList.length
- )
+ const end = indexToPosition(text, match.index + match[1].length + match.groups.classList.length)
return {
classList: match.groups.classList,
important: Boolean(match.groups.important),
range: {
start: {
line: globalStart.line + start.line,
- character:
- (end.line === 0 ? globalStart.character : 0) + start.character,
+ character: (end.line === 0 ? globalStart.character : 0) + start.character,
},
end: {
line: globalStart.line + end.line,
- character:
- (end.line === 0 ? globalStart.character : 0) + end.character,
+ character: (end.line === 0 ? globalStart.character : 0) + end.character,
},
},
}
@@ -146,7 +122,7 @@ doc: TextDocument,
range?: Range
): Promise<DocumentClassList[]> {
const settings = await state.editor.getConfiguration(doc.uri)
- const regexes = dlv(settings, 'experimental.classRegex', [])
+ const regexes = dlv(settings, 'tailwindCSS.experimental.classRegex', [])
if (!Array.isArray(regexes) || regexes.length === 0) return []
@@ -155,17 +131,13 @@ const result: DocumentClassList[] = []
for (let i = 0; i < regexes.length; i++) {
try {
- let [containerRegex, classRegex] = Array.isArray(regexes[i])
- ? regexes[i]
- : [regexes[i]]
+ 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 searchStart = doc.offsetAt(range?.start || { line: 0, character: 0 })
const matchStart = searchStart + containerMatch.start
const matchEnd = searchStart + containerMatch.end
@@ -173,9 +145,7 @@ if (classRegex) {
classRegex = createMultiRegexp(classRegex)
let classMatch
- while (
- (classMatch = classRegex.exec(containerMatch.match)) !== null
- ) {
+ while ((classMatch = classRegex.exec(containerMatch.match)) !== null) {
const classMatchStart = matchStart + classMatch.start
const classMatchEnd = matchStart + classMatch.end
result.push({
@@ -202,15 +172,9 @@
return result
}
-export function findClassListsInHtmlRange(
- doc: TextDocument,
- range?: Range
-): DocumentClassList[] {
+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) => {
@@ -274,12 +238,7 @@ match.index + match[0].length - 1 + offset + beforeOffset
)
const end = indexToPosition(
text,
- match.index +
- match[0].length -
- 1 +
- offset +
- value.length +
- afterOffset
+ match.index + match[0].length - 1 + offset + value.length + afterOffset
)
return {
@@ -287,15 +246,11 @@ classList: value.substr(beforeOffset, value.length + afterOffset),
range: {
start: {
line: (range?.start.line || 0) + start.line,
- character:
- (end.line === 0 ? range?.start.character || 0 : 0) +
- start.character,
+ character: (end.line === 0 ? range?.start.character || 0 : 0) + start.character,
},
end: {
line: (range?.start.line || 0) + end.line,
- character:
- (end.line === 0 ? range?.start.character || 0 : 0) +
- end.character,
+ character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
},
},
}
@@ -320,10 +275,7 @@ classLists = findClassListsInCssRange(doc, range)
} else {
classLists = findClassListsInHtmlRange(doc, range)
}
- return [
- ...classLists,
- ...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
- ]
+ return [...classLists, ...(includeCustom ? await findCustomClassLists(state, doc, range) : [])]
}
export async function findClassListsInDocument(
@@ -355,9 +307,7 @@
let boundaries = getLanguageBoundaries(state, doc)
if (!boundaries) return []
- return flatten(
- boundaries.css.map((range) => findHelperFunctionsInRange(doc, range))
- )
+ return flatten(boundaries.css.map((range) => findHelperFunctionsInRange(doc, range)))
}
export function findHelperFunctionsInRange(
@@ -387,10 +337,7 @@ range
),
valueRange: resolveRange(
{
- start: indexToPosition(
- text,
- startIndex + match.groups.helper.length + 1
- ),
+ start: indexToPosition(text, startIndex + match.groups.helper.length + 1),
end: indexToPosition(
text,
startIndex + match.groups.helper.length + 1 + 1 + value.length + 1
@@ -420,10 +367,7 @@ }
if (isCssContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
- } else if (
- isHtmlContext(state, doc, position) ||
- isJsContext(state, doc, position)
- ) {
+ } else if (isHtmlContext(state, doc, position) || isJsContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
}
@@ -431,9 +375,7 @@ if (classNames.length === 0) {
return null
}
- const className = classNames.find(({ range }) =>
- isWithinRange(position, range)
- )
+ const className = classNames.find(({ range }) => isWithinRange(position, range))
if (!className) return null
|