(commands: Command[])
| 28 | } | null = null |
| 29 | |
| 30 | function getCommandFuse(commands: Command[]): Fuse<CommandSearchItem> { |
| 31 | if (fuseCache?.commands === commands) { |
| 32 | return fuseCache.fuse |
| 33 | } |
| 34 | |
| 35 | const commandData: CommandSearchItem[] = commands |
| 36 | .filter(cmd => !cmd.isHidden) |
| 37 | .map(cmd => { |
| 38 | const commandName = getCommandName(cmd) |
| 39 | const parts = commandName.split(SEPARATORS).filter(Boolean) |
| 40 | |
| 41 | return { |
| 42 | descriptionKey: (cmd.description ?? '') |
| 43 | .split(' ') |
| 44 | .map(word => cleanWord(word)) |
| 45 | .filter(Boolean), |
| 46 | partKey: parts.length > 1 ? parts : undefined, |
| 47 | commandName, |
| 48 | command: cmd, |
| 49 | aliasKey: cmd.aliases, |
| 50 | } |
| 51 | }) |
| 52 | |
| 53 | const fuse = new Fuse(commandData, { |
| 54 | includeScore: true, |
| 55 | threshold: 0.3, // relatively strict matching |
| 56 | location: 0, // prefer matches at the beginning of strings |
| 57 | distance: 100, // increased to allow matching in descriptions |
| 58 | keys: [ |
| 59 | { |
| 60 | name: 'commandName', |
| 61 | weight: 3, // Highest priority for command names |
| 62 | }, |
| 63 | { |
| 64 | name: 'partKey', |
| 65 | weight: 2, // Next highest priority for command parts |
| 66 | }, |
| 67 | { |
| 68 | name: 'aliasKey', |
| 69 | weight: 2, // Same high priority for aliases |
| 70 | }, |
| 71 | { |
| 72 | name: 'descriptionKey', |
| 73 | weight: 0.5, // Lower priority for descriptions |
| 74 | }, |
| 75 | ], |
| 76 | }) |
| 77 | |
| 78 | fuseCache = { commands, fuse } |
| 79 | return fuse |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Type guard to check if a suggestion's metadata is a Command. |
no test coverage detected