@@ -7,6 +7,7 @@ import type {
7
7
CompletionList ,
8
8
TextDocument ,
9
9
Position ,
10
+ CompletionContext ,
10
11
} from 'vscode-languageserver'
11
12
const dlv = require ( 'dlv' )
12
13
import removeMeta from './util/removeMeta'
@@ -43,7 +44,8 @@ export function completionsFromClassList(
43
44
classList : string ,
44
45
classListRange : Range ,
45
46
filter ?: ( item : CompletionItem ) => boolean ,
46
- document ?: TextDocument
47
+ document ?: TextDocument ,
48
+ context ?: CompletionContext
47
49
) : CompletionList {
48
50
let classNames = classList . split ( / [ \s + ] / )
49
51
const partialClassName = classNames [ classNames . length - 1 ]
@@ -62,6 +64,58 @@ export function completionsFromClassList(
62
64
}
63
65
64
66
if ( state . jit ) {
67
+ if (
68
+ context &&
69
+ ( context . triggerKind === 1 ||
70
+ ( context . triggerKind === 2 && context . triggerCharacter === '/' ) ) &&
71
+ partialClassName . includes ( '/' )
72
+ ) {
73
+ let beforeSlash = partialClassName . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' )
74
+ let testClass = beforeSlash + '/[0]'
75
+ let { rules } = jit . generateRules ( state , [ testClass ] )
76
+ if ( rules . length > 0 ) {
77
+ let opacities = dlv ( state . config , 'theme.opacity' , { } )
78
+ if ( ! isObject ( opacities ) ) {
79
+ opacities = { }
80
+ }
81
+ return {
82
+ isIncomplete : false ,
83
+ items : Object . keys ( opacities ) . map ( ( opacity , index ) => {
84
+ let className = `${ beforeSlash } /${ opacity } `
85
+ let kind : CompletionItemKind = 21
86
+ let documentation : string = null
87
+
88
+ const color = getColor ( state , className )
89
+ if ( color !== null ) {
90
+ kind = 16
91
+ if ( typeof color !== 'string' ) {
92
+ documentation = color . toRgbString ( ) . replace ( / ( ^ r g b a \( [ ^ ) ] + ) 0 \) $ / , '$1 0.001)' )
93
+ }
94
+ }
95
+
96
+ return {
97
+ label : opacity ,
98
+ detail : stringifyConfigValue ( opacities [ opacity ] ) ,
99
+ documentation,
100
+ kind,
101
+ sortText : naturalExpand ( index ) ,
102
+ data : [ className ] ,
103
+ textEdit : {
104
+ newText : opacity ,
105
+ range : {
106
+ ...replacementRange ,
107
+ start : {
108
+ ...replacementRange . start ,
109
+ character : replacementRange . start . character + beforeSlash . length + 1 ,
110
+ } ,
111
+ } ,
112
+ } ,
113
+ }
114
+ } ) ,
115
+ }
116
+ }
117
+ }
118
+
65
119
let allVariants = Object . keys ( state . variants )
66
120
let { variants : existingVariants , offset } = getVariantsFromClassName ( state , partialClassName )
67
121
@@ -256,7 +310,8 @@ export function completionsFromClassList(
256
310
function provideClassAttributeCompletions (
257
311
state : State ,
258
312
document : TextDocument ,
259
- position : Position
313
+ position : Position ,
314
+ context ?: CompletionContext
260
315
) : CompletionList {
261
316
let str = document . getText ( {
262
317
start : { line : Math . max ( position . line - 10 , 0 ) , character : 0 } ,
@@ -299,7 +354,8 @@ function provideClassAttributeCompletions(
299
354
end : position ,
300
355
} ,
301
356
undefined ,
302
- document
357
+ document ,
358
+ context
303
359
)
304
360
}
305
361
} catch ( _ ) { }
@@ -417,10 +473,11 @@ function provideAtApplyCompletions(
417
473
function provideClassNameCompletions (
418
474
state : State ,
419
475
document : TextDocument ,
420
- position : Position
476
+ position : Position ,
477
+ context ?: CompletionContext
421
478
) : CompletionList {
422
479
if ( isHtmlContext ( state , document , position ) || isJsContext ( state , document , position ) ) {
423
- return provideClassAttributeCompletions ( state , document , position )
480
+ return provideClassAttributeCompletions ( state , document , position , context )
424
481
}
425
482
426
483
if ( isCssContext ( state , document , position ) ) {
@@ -925,11 +982,16 @@ async function provideEmmetCompletions(
925
982
} )
926
983
}
927
984
928
- export async function doComplete ( state : State , document : TextDocument , position : Position ) {
985
+ export async function doComplete (
986
+ state : State ,
987
+ document : TextDocument ,
988
+ position : Position ,
989
+ context ?: CompletionContext
990
+ ) {
929
991
if ( state === null ) return { items : [ ] , isIncomplete : false }
930
992
931
993
const result =
932
- provideClassNameCompletions ( state , document , position ) ||
994
+ provideClassNameCompletions ( state , document , position , context ) ||
933
995
provideCssHelperCompletions ( state , document , position ) ||
934
996
provideCssDirectiveCompletions ( state , document , position ) ||
935
997
provideScreenDirectiveCompletions ( state , document , position ) ||
@@ -958,8 +1020,13 @@ export async function resolveCompletionItem(
958
1020
return item
959
1021
}
960
1022
1023
+ if ( ! Array . isArray ( item . data ) ) {
1024
+ return item
1025
+ }
1026
+
961
1027
if ( state . jit ) {
962
1028
if ( item . kind === 9 ) return item
1029
+ if ( item . detail && item . documentation ) return item
963
1030
let { root, rules } = jit . generateRules ( state , [ item . data . join ( state . separator ) ] )
964
1031
if ( rules . length === 0 ) return item
965
1032
if ( ! item . detail ) {
0 commit comments