Skip to content

Commit 8d677d0

Browse files
committed
improve postcss
1 parent 58c2c9e commit 8d677d0

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/extension.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const HTML_TYPES = [
2424
'nunjucks',
2525
'haml'
2626
]
27-
const CSS_TYPES = ['css', 'sass', 'scss', 'less', 'postcss', 'stylus']
27+
const CSS_TYPES = ['css', 'sass', 'scss', 'less', 'stylus']
2828

2929
export async function activate(context: vscode.ExtensionContext) {
3030
let tw
@@ -103,15 +103,16 @@ export function deactivate() {}
103103

104104
function createCompletionItemProvider({
105105
items,
106+
prefixedItems,
106107
languages,
107108
regex,
108109
triggerCharacters,
109110
config,
110-
prefix = '',
111111
enable = () => true,
112112
emmet = false
113113
}: {
114114
items?
115+
prefixedItems?
115116
languages?: string[]
116117
regex?: RegExp
117118
triggerCharacters?: string[]
@@ -177,14 +178,21 @@ function createCompletionItemProvider({
177178
.replace(/\./g, '.children.')
178179

179180
if (pth !== '') {
180-
const itms = dlv(items, pth)
181+
const itms =
182+
prefixedItems &&
183+
str.indexOf('.') === 0 &&
184+
str.indexOf(separator) === -1
185+
? dlv(prefixedItems, pth)
186+
: dlv(items, pth)
181187
if (itms) {
182-
return prefixItems(itms.children, str, prefix)
188+
return Object.keys(itms.children).map(x => itms.children[x].item)
183189
}
184190
}
185191

186192
if (str.indexOf(separator) === -1) {
187-
return prefixItems(items, str, prefix)
193+
return prefixedItems && str.indexOf('.') === 0
194+
? Object.keys(prefixedItems).map(x => prefixedItems[x].item)
195+
: Object.keys(items).map(x => items[x].item)
188196
}
189197

190198
return []
@@ -282,7 +290,7 @@ function depthOf(obj) {
282290
return level
283291
}
284292

285-
function createItems(classNames, separator, config, parent = '') {
293+
function createItems(classNames, separator, config, prefix = '', parent = '') {
286294
let items = {}
287295
let i = 0
288296

@@ -292,6 +300,7 @@ function createItems(classNames, separator, config, parent = '') {
292300
key,
293301
vscode.CompletionItemKind.Constant
294302
)
303+
item.filterText = item.insertText = `${prefix}${key}`
295304
item.sortText = naturalExpand(i.toString())
296305
if (key !== 'container' && key !== 'group') {
297306
if (parent) {
@@ -318,6 +327,7 @@ function createItems(classNames, separator, config, parent = '') {
318327
`${key}${separator}`,
319328
vscode.CompletionItemKind.Constant
320329
)
330+
item.filterText = item.insertText = `${prefix}${key}${separator}`
321331
item.sortText = naturalExpand(i.toString())
322332
item.command = { title: '', command: 'editor.action.triggerSuggest' }
323333
if (key === 'hover' || key === 'focus' || key === 'active') {
@@ -335,7 +345,7 @@ function createItems(classNames, separator, config, parent = '') {
335345
}
336346
items[key] = {
337347
item,
338-
children: createItems(classNames[key], separator, config, key)
348+
children: createItems(classNames[key], separator, config, prefix, key)
339349
}
340350
i++
341351
}
@@ -391,6 +401,7 @@ class TailwindIntellisense {
391401
private _disposable: vscode.Disposable
392402
private _tailwind
393403
private _items
404+
private _prefixedItems
394405
private _configItems
395406
private _prefixedConfigItems
396407

@@ -409,6 +420,12 @@ class TailwindIntellisense {
409420
if (separator !== ':') return
410421

411422
this._items = createItems(tailwind.classNames, separator, tailwind.config)
423+
this._prefixedItems = createItems(
424+
tailwind.classNames,
425+
separator,
426+
tailwind.config,
427+
'.'
428+
)
412429
this._configItems = createConfigItems(tailwind.config)
413430
this._prefixedConfigItems = createConfigItems(tailwind.config, '.')
414431

@@ -427,11 +444,21 @@ class TailwindIntellisense {
427444
this._providers.push(
428445
createCompletionItemProvider({
429446
items: this._items,
447+
prefixedItems: this._prefixedItems,
430448
languages: CSS_TYPES,
431449
regex: /@apply ([^;}]*)$/,
432450
triggerCharacters: ['.', separator],
433-
config: tailwind.config,
434-
prefix: '.'
451+
config: tailwind.config
452+
})
453+
)
454+
this._providers.push(
455+
createCompletionItemProvider({
456+
items: this._items,
457+
prefixedItems: this._items,
458+
languages: ['postcss'],
459+
regex: /@apply ([^;}]*)$/,
460+
triggerCharacters: ['.', separator],
461+
config: tailwind.config
435462
})
436463
)
437464

@@ -538,6 +565,12 @@ class TailwindIntellisense {
538565
items: this._prefixedConfigItems
539566
})
540567
)
568+
this._providers.push(
569+
createConfigItemProvider({
570+
languages: ['postcss'],
571+
items: this._configItems
572+
})
573+
)
541574

542575
this._providers.push(
543576
createConfigItemProvider({

0 commit comments

Comments
 (0)