Skip to content

Commit 0f2016a

Browse files
committed
fix: add arbitrary value to completion list
1 parent b6fc6d5 commit 0f2016a

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/language-service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ const baseSort: NonNullable<MatchSorterOptions<CompletionToken>['baseSort']> = (
7171
return -1
7272
}
7373

74+
// Sort arbitary values last
75+
if (a.value.endsWith('[') && !b.value.endsWith('[')) {
76+
return 1
77+
}
78+
79+
if (!a.value.endsWith('[') && b.value.endsWith('[')) {
80+
return -1
81+
}
82+
7483
return collator.compare(a.label, b.label)
7584
}
7685

src/twind.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,15 @@ export class Twind {
359359
for (const parsed of parse(rule)) {
360360
if (/\${x*}/.test(parsed.name)) continue
361361

362-
const [, arbitrayValue] = parsed.name.match(/-(\[[^\]]+])/) || []
362+
const hasArbitrayValue = /-(\[[^\]]+])/.test(parsed.name)
363363

364364
const utilitiyExists =
365365
!parsed.name ||
366366
completions.tokens.some((completion) => {
367367
if (completion.kind != 'utility') return false
368368

369-
if (arbitrayValue) {
370-
return (
371-
completion.theme &&
372-
completion.raw.replace(`{{theme(${completion.theme?.section})}}`, arbitrayValue) ===
373-
parsed.name
374-
)
369+
if (hasArbitrayValue) {
370+
return parsed.name.startsWith(completion.value) && parsed.name != completion.value
375371
}
376372

377373
switch (completion.interpolation) {
@@ -536,6 +532,7 @@ export class Twind {
536532
// | `nonzero` // PositiveNumber
537533
if (value.startsWith('theme(') && value.endsWith(')')) {
538534
const sectionKey = value.slice(6, -1)
535+
539536
const section = theme(sectionKey as keyof Theme)(context)
540537

541538
Object.keys(section)
@@ -547,6 +544,7 @@ export class Twind {
547544

548545
// Is this the base object for nested values
549546
const value = section[key]
547+
550548
if (
551549
typeof value === 'object' &&
552550
Object.keys(value).every((nestedKey) => keys.includes(`${key}-${nestedKey}`))
@@ -556,22 +554,33 @@ export class Twind {
556554

557555
return true
558556
})
557+
// Add marker for arbitrary value
558+
.concat('[')
559559
.forEach((key) => {
560+
if (key == '[' && suffix) {
561+
return
562+
}
563+
560564
let className = prefix
561565
if (key && key != 'DEFAULT') {
562566
className += key
563567
}
564568
if (className.endsWith('-')) {
565569
className = className.slice(0, -1)
566570
}
571+
567572
className += suffix
568573

569574
completionTokens.set(
570575
className,
571576
createCompletionToken(className, {
572577
kind: screens.has(className) ? 'screen' : undefined,
573578
raw: directive,
574-
theme: { section: sectionKey as keyof Theme, key, value: section[key] },
579+
detail: key == '[' ? 'arbitrary value' : undefined,
580+
theme:
581+
key == '['
582+
? { section: sectionKey as keyof Theme, key: '', value: '' }
583+
: { section: sectionKey as keyof Theme, key, value: section[key] },
575584
}),
576585
)
577586
})

0 commit comments

Comments
 (0)