@@ -6,6 +6,7 @@ const htmlElements = require('./htmlElements.js')
6
6
const tailwindClassNames = require ( 'tailwind-class-names' )
7
7
const dlv = require ( 'dlv' )
8
8
const Color = require ( 'color' )
9
+ const lineColumn = require ( 'line-column' )
9
10
10
11
const CONFIG_GLOB =
11
12
'**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js'
@@ -409,6 +410,102 @@ class TailwindIntellisense {
409
410
if ( tailwind ) {
410
411
this . _tailwind = tailwind
411
412
this . reload ( tailwind )
413
+
414
+ let fade = vscode . window . createTextEditorDecorationType ( {
415
+ opacity : '0.5'
416
+ } )
417
+
418
+ this . _providers . push (
419
+ vscode . window . onDidChangeTextEditorSelection ( e => {
420
+ if (
421
+ e . selections . length &&
422
+ e . selections [ 0 ] . start . line === e . selections [ 0 ] . end . line &&
423
+ e . selections [ 0 ] . start . character === e . selections [ 0 ] . end . character
424
+ ) {
425
+ let position = e . selections [ 0 ] . start
426
+ let searchStart = new vscode . Position (
427
+ Math . max ( position . line - 5 , 0 ) ,
428
+ 0
429
+ )
430
+ let searchEnd = new vscode . Position ( position . line + 5 , 0 ) // TODO: cap to end of file?
431
+ let range : vscode . Range = new vscode . Range ( searchStart , searchEnd )
432
+ let text : string = e . textEditor . document . getText ( range )
433
+
434
+ let regex = / ( c l a s s = " ) ( [ ^ " ] + ) " / g
435
+ let matches
436
+ let indexes = [ ]
437
+ let strings = [ ]
438
+
439
+ while ( ( matches = regex . exec ( text ) ) !== null ) {
440
+ strings . push ( matches [ 2 ] )
441
+ indexes . push (
442
+ regex . lastIndex - matches [ 0 ] . length + matches [ 1 ] . length
443
+ )
444
+ }
445
+
446
+ let addedDecoration = strings . some ( ( str , i ) => {
447
+ let { line, col } = lineColumn ( text ) . fromIndex ( indexes [ i ] )
448
+
449
+ let strStart = searchStart . translate ( {
450
+ characterDelta : col - 1 ,
451
+ lineDelta : line - 1
452
+ } )
453
+ let { line : line1 , col : col1 } = lineColumn ( text ) . fromIndex (
454
+ indexes [ i ] + str . length
455
+ )
456
+ let strEnd = searchStart . translate ( {
457
+ characterDelta : col1 - 1 ,
458
+ lineDelta : line1 - 1
459
+ } )
460
+ if (
461
+ position . isAfterOrEqual ( strStart ) &&
462
+ position . isBeforeOrEqual ( strEnd )
463
+ ) {
464
+ let classNames = [ ]
465
+ regex = / \S + / g
466
+ while ( ( matches = regex . exec ( str ) ) !== null ) {
467
+ // strings.push(matches[2])
468
+ // indexes.push(
469
+ // regex.lastIndex - matches[0].length + matches[1].length
470
+ // )
471
+ let startIndex = regex . lastIndex - matches [ 0 ] . length
472
+ let endIndex = startIndex + matches [ 0 ] . length
473
+ let foo = lineColumn ( str ) . fromIndex ( startIndex )
474
+ let bar = lineColumn ( str ) . fromIndex ( endIndex )
475
+ classNames . push ( {
476
+ className : matches [ 0 ] ,
477
+ start : strStart . translate ( {
478
+ lineDelta : foo . line - 1 ,
479
+ characterDelta : foo . col - 1
480
+ } ) ,
481
+ end : bar
482
+ ? strStart . translate ( {
483
+ lineDelta : bar . line - 1 ,
484
+ characterDelta : bar . col - 1
485
+ } )
486
+ : strEnd
487
+ } )
488
+ }
489
+ let toFade = classNames
490
+ . filter ( x => {
491
+ return ! (
492
+ position . isAfterOrEqual ( x . start ) &&
493
+ position . isBeforeOrEqual ( x . end )
494
+ )
495
+ } )
496
+ . map ( x => ( { range : new vscode . Range ( x . start , x . end ) } ) )
497
+ vscode . window . activeTextEditor . setDecorations ( fade , toFade )
498
+ return true
499
+ }
500
+ return false
501
+ } )
502
+
503
+ if ( ! addedDecoration ) {
504
+ vscode . window . activeTextEditor . setDecorations ( fade , [ ] )
505
+ }
506
+ }
507
+ } )
508
+ )
412
509
}
413
510
}
414
511
0 commit comments