@@ -180,6 +180,59 @@ function createCompletionItemProvider({
180
180
)
181
181
}
182
182
183
+ function createConfigItemProvider ( {
184
+ languages,
185
+ items,
186
+ enable = ( ) => true
187
+ } : {
188
+ languages ?: string [ ]
189
+ items ?: vscode . CompletionItem [ ]
190
+ enable ?: ( text : string ) => boolean
191
+ } = { } ) {
192
+ return vscode . languages . registerCompletionItemProvider (
193
+ languages ,
194
+ {
195
+ provideCompletionItems : (
196
+ document : vscode . TextDocument ,
197
+ position : vscode . Position
198
+ ) : vscode . CompletionItem [ ] => {
199
+ const range : vscode . Range = new vscode . Range (
200
+ new vscode . Position ( 0 , 0 ) ,
201
+ position
202
+ )
203
+ const text : string = document . getText ( range )
204
+
205
+ if ( ! enable ( text ) ) return [ ]
206
+
207
+ let lines = text . split ( / [ \n \r ] / )
208
+
209
+ let matches = lines
210
+ . slice ( - 5 )
211
+ . join ( '\n' )
212
+ . match ( / c o n f i g \( [ " ' ] ( [ ^ " ' ] * ) $ / )
213
+
214
+ if ( ! matches ) return [ ]
215
+
216
+ let objPath =
217
+ matches [ 1 ]
218
+ . replace ( / \. [ ^ . ] * $ / , '' )
219
+ . replace ( '.' , '.children.' )
220
+ . trim ( ) + '.children'
221
+ let foo = dlv ( items , objPath )
222
+
223
+ if ( foo ) {
224
+ return Object . keys ( foo ) . map ( x => foo [ x ] . item )
225
+ }
226
+
227
+ return Object . keys ( items ) . map ( x => items [ x ] . item )
228
+ }
229
+ } ,
230
+ "'" ,
231
+ '"' ,
232
+ '.'
233
+ )
234
+ }
235
+
183
236
function prefixItems ( items , str , prefix ) {
184
237
const addPrefix =
185
238
typeof prefix !== 'undefined' && prefix !== '' && str === prefix
@@ -274,7 +327,7 @@ function createItems(classNames, separator, config, parent = '') {
274
327
return items
275
328
}
276
329
277
- function createConfigItems ( config ) {
330
+ function createConfigItems ( config , prefix = '' ) {
278
331
let items = { }
279
332
let i = 0
280
333
@@ -287,7 +340,7 @@ function createConfigItems(config) {
287
340
if ( depthOf ( config [ key ] ) === 0 ) {
288
341
if ( key === 'plugins' ) return
289
342
290
- item . filterText = item . insertText = `. ${ key } `
343
+ item . filterText = item . insertText = `${ prefix } ${ key } `
291
344
item . sortText = naturalExpand ( i . toString ( ) )
292
345
if ( typeof config [ key ] === 'string' || typeof config [ key ] === 'number' ) {
293
346
item . detail = config [ key ]
@@ -307,7 +360,7 @@ function createConfigItems(config) {
307
360
item . filterText = item . insertText = `${ key } .`
308
361
item . sortText = naturalExpand ( i . toString ( ) )
309
362
item . command = { title : '' , command : 'editor.action.triggerSuggest' }
310
- items [ key ] = { item, children : createConfigItems ( config [ key ] ) }
363
+ items [ key ] = { item, children : createConfigItems ( config [ key ] , prefix ) }
311
364
}
312
365
313
366
i ++
@@ -322,6 +375,7 @@ class TailwindIntellisense {
322
375
private _tailwind
323
376
private _items
324
377
private _configItems
378
+ private _prefixedConfigItems
325
379
326
380
constructor ( tailwind ) {
327
381
if ( tailwind ) {
@@ -339,6 +393,7 @@ class TailwindIntellisense {
339
393
340
394
this . _items = createItems ( tailwind . classNames , separator , tailwind . config )
341
395
this . _configItems = createConfigItems ( tailwind . config )
396
+ this . _prefixedConfigItems = createConfigItems ( tailwind . config , '.' )
342
397
343
398
this . _providers = [ ]
344
399
@@ -415,44 +470,26 @@ class TailwindIntellisense {
415
470
)
416
471
417
472
this . _providers . push (
418
- vscode . languages . registerCompletionItemProvider (
419
- CSS_TYPES ,
420
- {
421
- provideCompletionItems : (
422
- document : vscode . TextDocument ,
423
- position : vscode . Position
424
- ) : vscode . CompletionItem [ ] => {
425
- const range : vscode . Range = new vscode . Range (
426
- new vscode . Position ( Math . max ( position . line - 5 , 0 ) , 0 ) ,
427
- position
428
- )
429
- const text : string = document . getText ( range )
430
-
431
- let matches = text . match ( / c o n f i g \( [ " ' ] ( [ ^ " ' ] * ) $ / )
432
-
433
- if ( ! matches ) return [ ]
434
-
435
- let objPath =
436
- matches [ 1 ]
437
- . replace ( / \. [ ^ . ] * $ / , '' )
438
- . replace ( '.' , '.children.' )
439
- . trim ( ) + '.children'
440
- let foo = dlv ( this . _configItems , objPath )
441
-
442
- if ( foo ) {
443
- console . log ( Object . keys ( foo ) . map ( x => foo [ x ] . item ) )
444
- return Object . keys ( foo ) . map ( x => foo [ x ] . item )
445
- }
473
+ createConfigItemProvider ( {
474
+ languages : CSS_TYPES ,
475
+ items : this . _prefixedConfigItems
476
+ } )
477
+ )
446
478
447
- return Object . keys ( this . _configItems ) . map (
448
- x => this . _configItems [ x ] . item
449
- )
479
+ this . _providers . push (
480
+ createConfigItemProvider ( {
481
+ languages : [ 'vue' ] ,
482
+ items : this . _configItems ,
483
+ enable : text => {
484
+ if (
485
+ text . indexOf ( '<style' ) !== - 1 &&
486
+ text . indexOf ( '</style>' ) === - 1
487
+ ) {
488
+ return true
450
489
}
451
- } ,
452
- "'" ,
453
- '"' ,
454
- '.'
455
- )
490
+ return false
491
+ }
492
+ } )
456
493
)
457
494
458
495
this . _providers . push (
0 commit comments