Skip to content

Commit 3ed7a35

Browse files
committed
add config helper completions for .vue files
1 parent e3bbd69 commit 3ed7a35

File tree

1 file changed

+76
-39
lines changed

1 file changed

+76
-39
lines changed

src/extension.ts

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,59 @@ function createCompletionItemProvider({
180180
)
181181
}
182182

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(/config\(["']([^"']*)$/)
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+
183236
function prefixItems(items, str, prefix) {
184237
const addPrefix =
185238
typeof prefix !== 'undefined' && prefix !== '' && str === prefix
@@ -274,7 +327,7 @@ function createItems(classNames, separator, config, parent = '') {
274327
return items
275328
}
276329

277-
function createConfigItems(config) {
330+
function createConfigItems(config, prefix = '') {
278331
let items = {}
279332
let i = 0
280333

@@ -287,7 +340,7 @@ function createConfigItems(config) {
287340
if (depthOf(config[key]) === 0) {
288341
if (key === 'plugins') return
289342

290-
item.filterText = item.insertText = `.${key}`
343+
item.filterText = item.insertText = `${prefix}${key}`
291344
item.sortText = naturalExpand(i.toString())
292345
if (typeof config[key] === 'string' || typeof config[key] === 'number') {
293346
item.detail = config[key]
@@ -307,7 +360,7 @@ function createConfigItems(config) {
307360
item.filterText = item.insertText = `${key}.`
308361
item.sortText = naturalExpand(i.toString())
309362
item.command = { title: '', command: 'editor.action.triggerSuggest' }
310-
items[key] = { item, children: createConfigItems(config[key]) }
363+
items[key] = { item, children: createConfigItems(config[key], prefix) }
311364
}
312365

313366
i++
@@ -322,6 +375,7 @@ class TailwindIntellisense {
322375
private _tailwind
323376
private _items
324377
private _configItems
378+
private _prefixedConfigItems
325379

326380
constructor(tailwind) {
327381
if (tailwind) {
@@ -339,6 +393,7 @@ class TailwindIntellisense {
339393

340394
this._items = createItems(tailwind.classNames, separator, tailwind.config)
341395
this._configItems = createConfigItems(tailwind.config)
396+
this._prefixedConfigItems = createConfigItems(tailwind.config, '.')
342397

343398
this._providers = []
344399

@@ -415,44 +470,26 @@ class TailwindIntellisense {
415470
)
416471

417472
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(/config\(["']([^"']*)$/)
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+
)
446478

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
450489
}
451-
},
452-
"'",
453-
'"',
454-
'.'
455-
)
490+
return false
491+
}
492+
})
456493
)
457494

458495
this._providers.push(

0 commit comments

Comments
 (0)