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
|
diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts
index d0ad26681d864cb45c0d893a556e2bd88cc44497..d9a23c461e9c490c4d9708b6768822329c04c25d 100644
--- a/packages/tailwindcss-language-service/src/util/find.ts
+++ b/packages/tailwindcss-language-service/src/util/find.ts
@@ -77,15 +77,7 @@ mode?: 'html' | 'css',
includeCustom: boolean = true
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
- return flatten(
- classLists.flatMap((classList) => {
- if (Array.isArray(classList)) {
- return classList.map(getClassNamesInClassList)
- } else {
- return [getClassNamesInClassList(classList)]
- }
- })
- )
+ return flatten(classLists.map(getClassNamesInClassList))
}
export async function findClassNamesInDocument(
@@ -93,15 +85,7 @@ state: State,
doc: TextDocument
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInDocument(state, doc)
- return flatten(
- classLists.flatMap((classList) => {
- if (Array.isArray(classList)) {
- return classList.map(getClassNamesInClassList)
- } else {
- return [getClassNamesInClassList(classList)]
- }
- })
- )
+ return flatten(classLists.map(getClassNamesInClassList))
}
export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
@@ -198,7 +182,7 @@ export async function findClassListsInHtmlRange(
state: State,
doc: TextDocument,
range?: Range
-): Promise<Array<DocumentClassList | DocumentClassList[]>> {
+): Promise<DocumentClassList[]> {
const text = doc.getText(range)
const matches = matchClassAttributes(
@@ -206,7 +190,7 @@ text,
(await state.editor.getConfiguration(doc.uri)).tailwindCSS.classAttributes
)
- const result: Array<DocumentClassList | DocumentClassList[]> = []
+ const result: DocumentClassList[] = []
matches.forEach((match) => {
const subtext = text.substr(match.index + match[0].length - 1)
@@ -217,11 +201,9 @@ ? getComputedClassAttributeLexer()
: getClassAttributeLexer()
lexer.reset(subtext)
- let classLists: Array<{ value: string; offset: number } | { value: string; offset: number }[]> =
- []
- let rootClassList: { value: string; offset: number }[] = []
+ let classLists: { value: string; offset: number }[] = []
+ let token: moo.Token
let currentClassList: { value: string; offset: number }
- let depth = 0
try {
for (let token of lexer) {
@@ -236,53 +218,56 @@ }
}
} else {
if (currentClassList) {
- if (depth === 0) {
- rootClassList.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
- } else {
- classLists.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
- }
+ classLists.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
}
currentClassList = undefined
}
- if (token.type === 'lbrace') {
- depth += 1
- } else if (token.type === 'rbrace') {
- depth -= 1
- }
}
} catch (_) {}
if (currentClassList) {
- if (depth === 0) {
- rootClassList.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
- } else {
- classLists.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
- }
+ classLists.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
}
- classLists.push(rootClassList)
-
result.push(
...classLists
- .map((classList) => {
- if (Array.isArray(classList)) {
- return classList
- .map((classList) => resolveClassList(classList, text, match, range))
- .filter((x) => x !== null)
- } else {
- return resolveClassList(classList, text, match, range)
+ .map(({ value, offset }) => {
+ if (value.trim() === '') {
+ return null
+ }
+
+ const before = value.match(/^\s*/)
+ const beforeOffset = before === null ? 0 : before[0].length
+ const after = value.match(/\s*$/)
+ const afterOffset = after === null ? 0 : -after[0].length
+
+ const start = indexToPosition(
+ text,
+ match.index + match[0].length - 1 + offset + beforeOffset
+ )
+ const end = indexToPosition(
+ text,
+ match.index + match[0].length - 1 + offset + value.length + afterOffset
+ )
+
+ return {
+ 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,
+ },
+ end: {
+ line: (range?.start.line || 0) + end.line,
+ character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
+ },
+ },
}
})
.filter((x) => x !== null)
@@ -292,51 +277,14 @@
return result
}
-function resolveClassList(
- classList: { value: string; offset: number },
- text: string,
- match: RegExpMatchArray,
- range?: Range
-): DocumentClassList {
- let { value, offset } = classList
- if (value.trim() === '') {
- return null
- }
-
- const before = value.match(/^\s*/)
- const beforeOffset = before === null ? 0 : before[0].length
- const after = value.match(/\s*$/)
- const afterOffset = after === null ? 0 : -after[0].length
-
- const start = indexToPosition(text, match.index + match[0].length - 1 + offset + beforeOffset)
- const end = indexToPosition(
- text,
- match.index + match[0].length - 1 + offset + value.length + afterOffset
- )
-
- return {
- 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,
- },
- end: {
- line: (range?.start.line || 0) + end.line,
- character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
- },
- },
- }
-}
-
export async function findClassListsInRange(
state: State,
doc: TextDocument,
range?: Range,
mode?: 'html' | 'css',
includeCustom: boolean = true
-): Promise<Array<DocumentClassList | DocumentClassList[]>> {
- let classLists: Array<DocumentClassList | DocumentClassList[]>
+): Promise<DocumentClassList[]> {
+ let classLists: DocumentClassList[]
if (mode === 'css') {
classLists = findClassListsInCssRange(doc, range)
} else {
@@ -348,7 +296,7 @@
export async function findClassListsInDocument(
state: State,
doc: TextDocument
-): Promise<Array<DocumentClassList | DocumentClassList[]>> {
+): Promise<DocumentClassList[]> {
if (isCssDoc(state, doc)) {
return findClassListsInCssRange(doc)
}
|