Skip to content

Commit 62be68d

Browse files
committed
fix: use kind order only for same ranked items and sort screens by their min-width
1 parent 175f793 commit 62be68d

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/language-service.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,33 @@ const collator = new Intl.Collator(undefined, {
4545
caseFirst: 'false',
4646
})
4747

48-
const baseSort: NonNullable<MatchSorterOptions<CompletionToken>['baseSort']> = (a, b) => {
48+
const KIND_SORTING: CompletionToken['kind'][] = ['screen', 'utility', 'variant']
49+
50+
const baseSort: NonNullable<MatchSorterOptions<CompletionToken>['baseSort']> = (
51+
{ item: a },
52+
{ item: b },
53+
) => {
54+
const kindOrder = KIND_SORTING.indexOf(a.kind) - KIND_SORTING.indexOf(b.kind)
55+
56+
if (kindOrder) {
57+
return kindOrder
58+
}
59+
60+
// Sort screens by their min width
61+
if (a.kind == 'screen') {
62+
return collator.compare(a.detail + ' ' + a.value, b.detail + ' ' + b.value)
63+
}
64+
4965
// Sort negated labels last
50-
if (a.item.label[0] == '-' && b.item.label[0] != '-') {
66+
if (a.label[0] == '-' && b.label[0] != '-') {
5167
return 1
5268
}
5369

54-
if (a.item.label[0] != '-' && b.item.label[0] == '-') {
70+
if (a.label[0] != '-' && b.label[0] == '-') {
5571
return -1
5672
}
5773

58-
return collator.compare(a.item.label, b.item.label)
74+
return collator.compare(a.label, b.label)
5975
}
6076

6177
// By default, match-sorter assumes spaces to be the word separator.
@@ -407,18 +423,16 @@ export class TwindLanguageService implements TemplateLanguageService {
407423

408424
// TODO Start a new directive group
409425
const needle = prepareText(rule.raw)
410-
const matched = [
411-
...matchSorter(screens, needle, {
412-
// threshold: matchSorter.rankings.MATCHES,
413-
keys: [(completion) => prepareText(completion.value + ' ' + completion.detail)],
414-
baseSort,
415-
}),
416-
...matchSorter([...utilities, ...variants], needle, {
417-
// threshold: matchSorter.rankings.MATCHES,
418-
keys: [(completion) => prepareText(completion.value)],
419-
baseSort,
420-
}),
421-
]
426+
const matched = matchSorter([...screens, ...utilities, ...variants], needle, {
427+
// threshold: matchSorter.rankings.MATCHES,
428+
keys: [
429+
(completion) =>
430+
completion.kind == 'screen'
431+
? prepareText(completion.value + ' ' + completion.detail)
432+
: prepareText(completion.value),
433+
],
434+
baseSort,
435+
})
422436

423437
if (rule.prefix && (!rule.raw || rule.raw === '&')) {
424438
const selfRef = completions.tokens.find(

0 commit comments

Comments
 (0)