fix(tui): prioritize slash command name matches

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-08 10:47:10 +08:00
1 parent 33aa70e4a5
commit 5c71b87a3f
3 files changed
+85 -2

No files matched your search

+15 -2
View File
@@ -32,6 +32,14 @@ function descriptor(choice: CommandChoice): SlashCommand | TuiCommand {
return choice.command
}
function commandMatchRank(choice: CommandChoice, query: string): number {
const name = descriptor(choice).command.slice(1).toLocaleLowerCase()
if (name === query) return 0
if (name.startsWith(query)) return 1
if (name.includes(query)) return 2
return 3
}
export function resolveSlashCommandLifecycle(
input: string,
command: SlashCommand,
@@ -108,8 +116,13 @@ export class CommandMenu {
this.hide()
return
}
if (changed || !this.picker.visible) this.picker.show(this.commands, token.slice(1), limit)
else this.picker.update(token.slice(1), limit)
const query = token.slice(1)
if (changed || !this.picker.visible) {
const commands = [...this.commands].sort((left, right) => (
commandMatchRank(left, query) - commandMatchRank(right, query)
))
this.picker.show(commands, query, limit)
} else this.picker.update(query, limit)
}
move(direction: -1 | 1): boolean {