@@ -286,6 +286,79 @@ function provideCssHelperCompletions(
286
286
}
287
287
}
288
288
289
+ function provideVariantsDirectiveCompletions (
290
+ state : State ,
291
+ { position, textDocument } : CompletionParams
292
+ ) : CompletionList {
293
+ let doc = state . editor . documents . get ( textDocument . uri )
294
+
295
+ if ( ! isCssContext ( doc , position ) ) {
296
+ return null
297
+ }
298
+
299
+ let text = doc . getText ( {
300
+ start : { line : position . line , character : 0 } ,
301
+ end : position ,
302
+ } )
303
+
304
+ const match = text . match ( / ^ \s * @ v a r i a n t s \s + (?< partial > [ ^ } ] * ) $ / i)
305
+
306
+ if ( match === null ) return null
307
+
308
+ const parts = match . groups . partial . split ( / \s * , \s * / )
309
+
310
+ if ( / \s + / . test ( parts [ parts . length - 1 ] ) ) return null
311
+
312
+ // TODO: move this to tailwindcss-class-names?
313
+ let variants = dlv (
314
+ state . config ,
315
+ [ 'variants' ] ,
316
+ dlv ( state . config , [ 'modules' ] , { } )
317
+ )
318
+ if ( ! isObject ( variants ) && ! Array . isArray ( variants ) ) {
319
+ variants = [ ]
320
+ }
321
+ let enabledVariants : string [ ]
322
+ if ( Array . isArray ( variants ) ) {
323
+ enabledVariants = variants
324
+ } else {
325
+ const uniqueVariants : Set < string > = new Set ( )
326
+ for ( const mod in variants ) {
327
+ if ( ! Array . isArray ( variants [ mod ] ) ) continue
328
+ variants [ mod ] . forEach ( ( v : string ) => uniqueVariants . add ( v ) )
329
+ }
330
+ enabledVariants = [ ...uniqueVariants ]
331
+ }
332
+
333
+ enabledVariants = state . variants . filter (
334
+ ( x ) => enabledVariants . indexOf ( x ) !== - 1 || x === 'default'
335
+ )
336
+
337
+ const existingVariants = parts . slice ( 0 , parts . length - 1 )
338
+
339
+ return {
340
+ isIncomplete : false ,
341
+ items : enabledVariants
342
+ . filter ( ( v ) => existingVariants . indexOf ( v ) === - 1 )
343
+ . map ( ( variant ) => ( {
344
+ // TODO: detail
345
+ label : variant ,
346
+ kind : CompletionItemKind . Constant ,
347
+ data : 'variant' ,
348
+ textEdit : {
349
+ newText : variant ,
350
+ range : {
351
+ start : {
352
+ line : position . line ,
353
+ character : position . character - parts [ parts . length - 1 ] . length ,
354
+ } ,
355
+ end : position ,
356
+ } ,
357
+ } ,
358
+ } ) ) ,
359
+ }
360
+ }
361
+
289
362
function provideScreenDirectiveCompletions (
290
363
state : State ,
291
364
{ position, textDocument } : CompletionParams
@@ -424,15 +497,20 @@ export function provideCompletions(
424
497
provideClassNameCompletions ( state , params ) ||
425
498
provideCssHelperCompletions ( state , params ) ||
426
499
provideCssDirectiveCompletions ( state , params ) ||
427
- provideScreenDirectiveCompletions ( state , params )
500
+ provideScreenDirectiveCompletions ( state , params ) ||
501
+ provideVariantsDirectiveCompletions ( state , params )
428
502
)
429
503
}
430
504
431
505
export function resolveCompletionItem (
432
506
state : State ,
433
507
item : CompletionItem
434
508
) : CompletionItem {
435
- if ( item . data === 'helper' || item . data === 'directive' ) {
509
+ if (
510
+ item . data === 'helper' ||
511
+ item . data === 'directive' ||
512
+ item . data === 'variant'
513
+ ) {
436
514
return item
437
515
}
438
516
0 commit comments