@@ -77,31 +77,15 @@ export async function findClassNamesInRange(
77
77
includeCustom : boolean = true
78
78
) : Promise < DocumentClassName [ ] > {
79
79
const classLists = await findClassListsInRange ( state , doc , range , mode , includeCustom )
80
- return flatten (
81
- classLists . flatMap ( ( classList ) => {
82
- if ( Array . isArray ( classList ) ) {
83
- return classList . map ( getClassNamesInClassList )
84
- } else {
85
- return [ getClassNamesInClassList ( classList ) ]
86
- }
87
- } )
88
- )
80
+ return flatten ( classLists . map ( getClassNamesInClassList ) )
89
81
}
90
82
91
83
export async function findClassNamesInDocument (
92
84
state : State ,
93
85
doc : TextDocument
94
86
) : Promise < DocumentClassName [ ] > {
95
87
const classLists = await findClassListsInDocument ( state , doc )
96
- return flatten (
97
- classLists . flatMap ( ( classList ) => {
98
- if ( Array . isArray ( classList ) ) {
99
- return classList . map ( getClassNamesInClassList )
100
- } else {
101
- return [ getClassNamesInClassList ( classList ) ]
102
- }
103
- } )
104
- )
88
+ return flatten ( classLists . map ( getClassNamesInClassList ) )
105
89
}
106
90
107
91
export function findClassListsInCssRange ( doc : TextDocument , range ?: Range ) : DocumentClassList [ ] {
@@ -198,15 +182,15 @@ export async function findClassListsInHtmlRange(
198
182
state : State ,
199
183
doc : TextDocument ,
200
184
range ?: Range
201
- ) : Promise < Array < DocumentClassList | DocumentClassList [ ] > > {
185
+ ) : Promise < DocumentClassList [ ] > {
202
186
const text = doc . getText ( range )
203
187
204
188
const matches = matchClassAttributes (
205
189
text ,
206
190
( await state . editor . getConfiguration ( doc . uri ) ) . tailwindCSS . classAttributes
207
191
)
208
192
209
- const result : Array < DocumentClassList | DocumentClassList [ ] > = [ ]
193
+ const result : DocumentClassList [ ] = [ ]
210
194
211
195
matches . forEach ( ( match ) => {
212
196
const subtext = text . substr ( match . index + match [ 0 ] . length - 1 )
@@ -217,11 +201,9 @@ export async function findClassListsInHtmlRange(
217
201
: getClassAttributeLexer ( )
218
202
lexer . reset ( subtext )
219
203
220
- let classLists : Array < { value : string ; offset : number } | { value : string ; offset : number } [ ] > =
221
- [ ]
222
- let rootClassList : { value : string ; offset : number } [ ] = [ ]
204
+ let classLists : { value : string ; offset : number } [ ] = [ ]
205
+ let token : moo . Token
223
206
let currentClassList : { value : string ; offset : number }
224
- let depth = 0
225
207
226
208
try {
227
209
for ( let token of lexer ) {
@@ -236,53 +218,56 @@ export async function findClassListsInHtmlRange(
236
218
}
237
219
} else {
238
220
if ( currentClassList ) {
239
- if ( depth === 0 ) {
240
- rootClassList . push ( {
241
- value : currentClassList . value ,
242
- offset : currentClassList . offset ,
243
- } )
244
- } else {
245
- classLists . push ( {
246
- value : currentClassList . value ,
247
- offset : currentClassList . offset ,
248
- } )
249
- }
221
+ classLists . push ( {
222
+ value : currentClassList . value ,
223
+ offset : currentClassList . offset ,
224
+ } )
250
225
}
251
226
currentClassList = undefined
252
227
}
253
- if ( token . type === 'lbrace' ) {
254
- depth += 1
255
- } else if ( token . type === 'rbrace' ) {
256
- depth -= 1
257
- }
258
228
}
259
229
} catch ( _ ) { }
260
230
261
231
if ( currentClassList ) {
262
- if ( depth === 0 ) {
263
- rootClassList . push ( {
264
- value : currentClassList . value ,
265
- offset : currentClassList . offset ,
266
- } )
267
- } else {
268
- classLists . push ( {
269
- value : currentClassList . value ,
270
- offset : currentClassList . offset ,
271
- } )
272
- }
232
+ classLists . push ( {
233
+ value : currentClassList . value ,
234
+ offset : currentClassList . offset ,
235
+ } )
273
236
}
274
237
275
- classLists . push ( rootClassList )
276
-
277
238
result . push (
278
239
...classLists
279
- . map ( ( classList ) => {
280
- if ( Array . isArray ( classList ) ) {
281
- return classList
282
- . map ( ( classList ) => resolveClassList ( classList , text , match , range ) )
283
- . filter ( ( x ) => x !== null )
284
- } else {
285
- return resolveClassList ( classList , text , match , range )
240
+ . map ( ( { value, offset } ) => {
241
+ if ( value . trim ( ) === '' ) {
242
+ return null
243
+ }
244
+
245
+ const before = value . match ( / ^ \s * / )
246
+ const beforeOffset = before === null ? 0 : before [ 0 ] . length
247
+ const after = value . match ( / \s * $ / )
248
+ const afterOffset = after === null ? 0 : - after [ 0 ] . length
249
+
250
+ const start = indexToPosition (
251
+ text ,
252
+ match . index + match [ 0 ] . length - 1 + offset + beforeOffset
253
+ )
254
+ const end = indexToPosition (
255
+ text ,
256
+ match . index + match [ 0 ] . length - 1 + offset + value . length + afterOffset
257
+ )
258
+
259
+ return {
260
+ classList : value . substr ( beforeOffset , value . length + afterOffset ) ,
261
+ range : {
262
+ start : {
263
+ line : ( range ?. start . line || 0 ) + start . line ,
264
+ character : ( end . line === 0 ? range ?. start . character || 0 : 0 ) + start . character ,
265
+ } ,
266
+ end : {
267
+ line : ( range ?. start . line || 0 ) + end . line ,
268
+ character : ( end . line === 0 ? range ?. start . character || 0 : 0 ) + end . character ,
269
+ } ,
270
+ } ,
286
271
}
287
272
} )
288
273
. filter ( ( x ) => x !== null )
@@ -292,51 +277,14 @@ export async function findClassListsInHtmlRange(
292
277
return result
293
278
}
294
279
295
- function resolveClassList (
296
- classList : { value : string ; offset : number } ,
297
- text : string ,
298
- match : RegExpMatchArray ,
299
- range ?: Range
300
- ) : DocumentClassList {
301
- let { value, offset } = classList
302
- if ( value . trim ( ) === '' ) {
303
- return null
304
- }
305
-
306
- const before = value . match ( / ^ \s * / )
307
- const beforeOffset = before === null ? 0 : before [ 0 ] . length
308
- const after = value . match ( / \s * $ / )
309
- const afterOffset = after === null ? 0 : - after [ 0 ] . length
310
-
311
- const start = indexToPosition ( text , match . index + match [ 0 ] . length - 1 + offset + beforeOffset )
312
- const end = indexToPosition (
313
- text ,
314
- match . index + match [ 0 ] . length - 1 + offset + value . length + afterOffset
315
- )
316
-
317
- return {
318
- classList : value . substr ( beforeOffset , value . length + afterOffset ) ,
319
- range : {
320
- start : {
321
- line : ( range ?. start . line || 0 ) + start . line ,
322
- character : ( end . line === 0 ? range ?. start . character || 0 : 0 ) + start . character ,
323
- } ,
324
- end : {
325
- line : ( range ?. start . line || 0 ) + end . line ,
326
- character : ( end . line === 0 ? range ?. start . character || 0 : 0 ) + end . character ,
327
- } ,
328
- } ,
329
- }
330
- }
331
-
332
280
export async function findClassListsInRange (
333
281
state : State ,
334
282
doc : TextDocument ,
335
283
range ?: Range ,
336
284
mode ?: 'html' | 'css' ,
337
285
includeCustom : boolean = true
338
- ) : Promise < Array < DocumentClassList | DocumentClassList [ ] > > {
339
- let classLists : Array < DocumentClassList | DocumentClassList [ ] >
286
+ ) : Promise < DocumentClassList [ ] > {
287
+ let classLists : DocumentClassList [ ]
340
288
if ( mode === 'css' ) {
341
289
classLists = findClassListsInCssRange ( doc , range )
342
290
} else {
@@ -348,7 +296,7 @@ export async function findClassListsInRange(
348
296
export async function findClassListsInDocument (
349
297
state : State ,
350
298
doc : TextDocument
351
- ) : Promise < Array < DocumentClassList | DocumentClassList [ ] > > {
299
+ ) : Promise < DocumentClassList [ ] > {
352
300
if ( isCssDoc ( state , doc ) ) {
353
301
return findClassListsInCssRange ( doc )
354
302
}
0 commit comments