Skip to content

Commit b6e6f21

Browse files
committed
implement completions() on the context
1 parent 6c0b7bc commit b6e6f21

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/lib/setupContextUtils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,36 @@ function registerPlugins(plugins, context) {
610610

611611
return output.map((value) => ({ raw: value, extension: 'html' }))
612612
}
613+
614+
// Generate a list of strings for autocompletion purposes. Colors will have a
615+
// tuple with options, e.g.:
616+
// ['uppercase', 'lowercase', ['bg-red', { color: 'rgb(255 0 0)' }]]
617+
context.completions = function () {
618+
// TODO: Try and detect color from components?
619+
// TODO: Should we provide a simple "public api" file with functions?
620+
let output = []
621+
622+
for (let util of classList) {
623+
if (Array.isArray(util)) {
624+
let [utilName, options] = util
625+
let isColor = [].concat(options.type).includes('color')
626+
627+
if (isColor) {
628+
for (let [value, color] of Object.entries(options?.values ?? {})) {
629+
output.push([formatClass(utilName, value), { color }])
630+
}
631+
} else {
632+
for (let value of Object.keys(options?.values ?? {})) {
633+
output.push(formatClass(utilName, value))
634+
}
635+
}
636+
} else {
637+
output.push(util)
638+
}
639+
}
640+
641+
return output
642+
}
613643
}
614644

615645
export function createContext(

tests/completions.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import resolveConfig from '../resolveConfig'
2+
import { createContext } from '../src/lib/setupContextUtils'
3+
4+
it('should generate completions for every possible class, without variants', () => {
5+
let config = {}
6+
7+
let context = createContext(resolveConfig(config))
8+
expect(context.completions()).toBeInstanceOf(Array)
9+
10+
// Verify we have a `container` for the 'components' section.
11+
expect(context.completions()).toContain('container')
12+
13+
// Verify we handle the DEFAULT case correctly
14+
expect(context.completions()).toContain('border')
15+
16+
// Verify we handle negative values correctly
17+
expect(context.completions()).toContain('-inset-1/4')
18+
19+
// Verify we list extra information for colors (!tuples)
20+
let fromBlack = context
21+
.completions()
22+
.find((value) => Array.isArray(value) && value[0] === 'from-black')
23+
24+
expect(fromBlack).toMatchObject(['from-black', { color: '#000' }])
25+
})

0 commit comments

Comments
 (0)