mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-11 00:07:55 +00:00
feat(commands): show command suggestions if a command was not found
This commit is contained in:
parent
ee45fca6ab
commit
f0d287b873
7 changed files with 58 additions and 23 deletions
|
|
@ -1,18 +1,17 @@
|
|||
use ordermap::OrderMap;
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use crate::{commands::Command, Token};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TreeBranch {
|
||||
current_command_key: Option<SmolStr>,
|
||||
current_command: Option<Command>,
|
||||
branches: OrderMap<Token, TreeBranch>,
|
||||
}
|
||||
|
||||
impl TreeBranch {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
current_command_key: None,
|
||||
current_command: None,
|
||||
branches: OrderMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -20,10 +19,10 @@ impl TreeBranch {
|
|||
pub fn register_command(&mut self, command: Command) {
|
||||
let mut current_branch = self;
|
||||
// iterate over tokens in command
|
||||
for token in command.tokens {
|
||||
for token in command.tokens.clone() {
|
||||
// recursively get or create a sub-branch for each token
|
||||
current_branch = current_branch.branches.entry(token).or_insert(TreeBranch {
|
||||
current_command_key: None,
|
||||
current_command: None,
|
||||
branches: OrderMap::new(),
|
||||
})
|
||||
}
|
||||
|
|
@ -31,20 +30,38 @@ impl TreeBranch {
|
|||
current_branch.branches.insert(
|
||||
Token::Empty,
|
||||
TreeBranch {
|
||||
current_command_key: Some(command.cb),
|
||||
current_command: Some(command),
|
||||
branches: OrderMap::new(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn callback(&self) -> Option<SmolStr> {
|
||||
self.current_command_key.clone()
|
||||
pub fn command(&self) -> Option<Command> {
|
||||
self.current_command.clone()
|
||||
}
|
||||
|
||||
pub fn possible_tokens(&self) -> impl Iterator<Item = &Token> {
|
||||
self.branches.keys()
|
||||
}
|
||||
|
||||
pub fn possible_commands(&self, max_depth: usize) -> Vec<Command> {
|
||||
if max_depth == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut commands = Vec::new();
|
||||
for token in self.possible_tokens() {
|
||||
if let Some(tree) = self.get_branch(token) {
|
||||
if let Some(command) = tree.command() {
|
||||
commands.push(command);
|
||||
// we dont need to look further if we found a command
|
||||
continue;
|
||||
}
|
||||
commands.append(&mut tree.possible_commands(max_depth - 1));
|
||||
}
|
||||
}
|
||||
commands
|
||||
}
|
||||
|
||||
pub fn get_branch(&self, token: &Token) -> Option<&TreeBranch> {
|
||||
self.branches.get(token)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue