1
1
import * as path from 'path'
2
2
import Module from 'module'
3
- import { fileURLToPath , pathToFileURL } from 'url'
3
+ import { fileURLToPath } from 'url'
4
4
5
5
import type { Logger } from 'typescript-template-language-service-decorator'
6
6
import type * as TS from 'typescript/lib/tsserverlibrary'
@@ -22,7 +22,7 @@ import type {
22
22
import { theme , create , silent } from 'twind'
23
23
import { VirtualSheet , virtualSheet } from 'twind/sheets'
24
24
import { getConfig , loadFile } from './load'
25
- import { getColor } from './colors'
25
+ import { getColor , KNOWN_COLORS } from './colors'
26
26
27
27
import type { ConfigurationManager } from './configuration'
28
28
import { watch } from './watch'
@@ -102,19 +102,17 @@ const detailsFromThemeValue = <Section extends keyof Theme>(
102
102
. filter ( Boolean )
103
103
. join ( '/' )
104
104
}
105
- }
106
105
107
- if ( typeof value == 'string' ) return value
108
- if ( typeof value == 'number' ) return '' + value
106
+ case 'fontFamily' : {
107
+ return Array . isArray ( value ) ? value . filter ( Boolean ) . join ( ', ' ) : ( value as string )
108
+ }
109
+ }
109
110
110
- // https://github.com/tailwindlabs/tailwindcss/blob/master/src/util/transformThemeValue.js
111
- // only testing for sections that uses an array for values
112
111
if (
113
- Array . isArray ( value ) &&
114
- ! [ 'fontSize' , 'outline' ] . includes ( section ) &&
115
- value . every ( ( x ) => x == null || typeof x == 'string' || typeof x == 'number' )
112
+ typeof value == 'string' &&
113
+ ( ( / c o l o r / i. test ( section ) && ! KNOWN_COLORS . has ( value ) ) || / \s / . test ( value ) )
116
114
) {
117
- return value . filter ( Boolean ) . join ( ', ' )
115
+ return value
118
116
}
119
117
120
118
return undefined
@@ -566,7 +564,7 @@ export class Twind {
566
564
detailsFromThemeValue ( theme . section , theme . value ) ,
567
565
) ,
568
566
) ) ||
569
- interpolation ||
567
+ translateInterpolation ( interpolation ) ||
570
568
detailFromCSS ( sheet , tw , value , interpolation ) )
571
569
)
572
570
} ,
@@ -646,7 +644,7 @@ export class Twind {
646
644
createCompletionToken ( className , {
647
645
kind : screens . has ( className ) ? 'screen' : undefined ,
648
646
raw : directive ,
649
- detail : key == '[' ? 'arbitrary value' : undefined ,
647
+ label : className . endsWith ( '[' ) && key === '[' ? ` ${ className } …]` : undefined ,
650
648
theme :
651
649
key == '['
652
650
? { section : sectionKey as keyof Theme , key : '' , value : '' }
@@ -671,6 +669,7 @@ export class Twind {
671
669
prefix ,
672
670
createCompletionToken ( prefix , {
673
671
raw : directive ,
672
+ label : `${ prefix } …${ suffix } ` ,
674
673
interpolation : value as CompletionToken [ 'interpolation' ] ,
675
674
} ) ,
676
675
)
@@ -702,7 +701,11 @@ function generateCSS(
702
701
sheet . reset ( )
703
702
704
703
if ( interpolation ) {
705
- value += getSampleInterpolation ( interpolation )
704
+ value = value . replace ( / … / g, getSampleInterpolation ( interpolation ) )
705
+ }
706
+
707
+ if ( value . endsWith ( '-[' ) ) {
708
+ value += '…]'
706
709
}
707
710
708
711
if ( value . endsWith ( ':' ) ) {
@@ -730,14 +733,21 @@ function generateCSS(
730
733
. trim ( )
731
734
}
732
735
736
+ // TODO do not match @keyframes
737
+ const CSS_DECLARATION_RE = / [ { ; ] \s * ( [ A - Z \d - ] + ) \s * : \s * ( [ ^ ; } ] + ) / gi
738
+
733
739
function detailFromCSS (
734
740
sheet : VirtualSheet ,
735
741
tw : TW ,
736
742
value : string ,
737
743
interpolation ?: CompletionToken [ 'interpolation' ] ,
738
744
) : string {
739
745
if ( interpolation ) {
740
- value += getSampleInterpolation ( interpolation )
746
+ value = value . replace ( / … / g, getSampleInterpolation ( interpolation ) )
747
+ }
748
+
749
+ if ( value . endsWith ( '-[' ) ) {
750
+ value += '…]'
741
751
}
742
752
743
753
let style : CSSRules = { }
@@ -757,7 +767,7 @@ function detailFromCSS(
757
767
758
768
// - if at-rule use at rule
759
769
// - if variant: use &:hover, &>*
760
- if ( key && / ^ @ | & / . test ( key ) ) {
770
+ if ( value . endsWith ( ':' ) && key && / ^ @ | & / . test ( key ) ) {
761
771
// TODO beautify children: siblings
762
772
// TODO order of suggestions
763
773
// TODO grouping prefix is ommited
@@ -768,9 +778,14 @@ function detailFromCSS(
768
778
const css = generateCSS ( sheet , tw , value )
769
779
770
780
let result = ''
771
- for ( const [ , property , value ] of css . matchAll ( / [ { ; ] \s * ( [ A - Z \d - ] + ) \s * : \s * ( [ ^ ; } ] + ) / gi) ) {
781
+
782
+ // Reset as we break early
783
+ CSS_DECLARATION_RE . lastIndex = 0
784
+ for ( let match : RegExpExecArray | null ; ( match = CSS_DECLARATION_RE . exec ( css ) ) ; ) {
785
+ const [ , property , value ] = match
786
+
772
787
if ( result . length < 30 ) {
773
- result += ( result && '; ' ) + `${ property } : ${ value } `
788
+ result += ( result && '; ' ) + `${ property } : ${ convertRem ( value ) } `
774
789
} else {
775
790
result += '; …'
776
791
break
@@ -779,3 +794,18 @@ function detailFromCSS(
779
794
780
795
return result
781
796
}
797
+
798
+ function translateInterpolation (
799
+ interpolation ?: CompletionToken [ 'interpolation' ] ,
800
+ ) : string | undefined {
801
+ switch ( interpolation ) {
802
+ case `string` : // NonEmptyString
803
+ return 'any string'
804
+ case `number` : // NonNegativeNumber
805
+ return 'a number greater or equal zero'
806
+ case `nonzero` : // PositiveNumber
807
+ return 'a number greater zero'
808
+ }
809
+
810
+ return interpolation
811
+ }
0 commit comments