Skip to content

Commit c3906b4

Browse files
authored
Expose completions API (#5520)
1 parent 00b6ed0 commit c3906b4

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
@@ -624,6 +624,36 @@ function registerPlugins(plugins, context) {
624624

625625
return output.map((value) => ({ raw: value, extension: 'html' }))
626626
}
627+
628+
// Generate a list of strings for autocompletion purposes. Colors will have a
629+
// tuple with options, e.g.:
630+
// ['uppercase', 'lowercase', ['bg-red', { color: 'rgb(255 0 0)' }]]
631+
context.completions = function () {
632+
// TODO: Try and detect color from components?
633+
// TODO: Should we provide a simple "public api" file with functions?
634+
let output = []
635+
636+
for (let util of classList) {
637+
if (Array.isArray(util)) {
638+
let [utilName, options] = util
639+
let isColor = [].concat(options.type).includes('color')
640+
641+
if (isColor) {
642+
for (let [value, color] of Object.entries(options?.values ?? {})) {
643+
output.push([formatClass(utilName, value), { color }])
644+
}
645+
} else {
646+
for (let value of Object.keys(options?.values ?? {})) {
647+
output.push(formatClass(utilName, value))
648+
}
649+
}
650+
} else {
651+
output.push(util)
652+
}
653+
}
654+
655+
return output
656+
}
627657
}
628658

629659
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)