Skip to content

Commit 934b0d6

Browse files
committed
implement completions() on the context
1 parent 0e36ecb commit 934b0d6

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
@@ -532,6 +532,36 @@ function registerPlugins(plugins, context) {
532532
variantFunctions.map((variantFunction, idx) => [sort << BigInt(idx), variantFunction])
533533
)
534534
}
535+
536+
// Generate a list of strings for autocompletion purposes. Colors will have a
537+
// tuple with options, e.g.:
538+
// ['uppercase', 'lowercase', ['bg-red', { color: 'rgb(255 0 0)' }]]
539+
context.completions = function () {
540+
// TODO: Try and detect color from components?
541+
// TODO: Should we provide a simple "public api" file with functions?
542+
let output = []
543+
544+
for (let util of classList) {
545+
if (Array.isArray(util)) {
546+
let [utilName, options] = util
547+
let isColor = [].concat(options.type).includes('color')
548+
549+
if (isColor) {
550+
for (let [value, color] of Object.entries(options?.values ?? {})) {
551+
output.push([formatClass(utilName, value), { color }])
552+
}
553+
} else {
554+
for (let value of Object.keys(options?.values ?? {})) {
555+
output.push(formatClass(utilName, value))
556+
}
557+
}
558+
} else {
559+
output.push(util)
560+
}
561+
}
562+
563+
return output
564+
}
535565
}
536566

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