This commit is contained in:
Simon Gardling
2022-05-04 23:43:50 -04:00
parent 5e5dd0f7a6
commit 243135b3dc
8 changed files with 47 additions and 30 deletions

View File

@@ -34,19 +34,25 @@ impl<'a> const Default for AutoComplete<'a> {
}
impl<'a> AutoComplete<'a> {
#[allow(dead_code)]
pub fn update_string(&mut self, string: &str) {
if self.string != string {
// catch empty strings here to avoid call to `generate_hint` and unnecessary logic
if string.is_empty() {
*self = Self::default();
} else {
self.i = 0;
self.string = string.to_string();
self.hint = generate_hint(string);
self.do_update_logic();
}
}
}
/// Runs update logic assuming that a change to `self.string` has been made
fn do_update_logic(&mut self) {
self.i = 0;
self.hint = generate_hint(&self.string);
}
pub fn register_movement(&mut self, movement: &Movement) {
if movement == &Movement::None {
return;
@@ -90,8 +96,8 @@ impl<'a> AutoComplete<'a> {
}
pub fn apply_hint(&mut self, hint: &str) {
let new_string = self.string.clone() + hint;
self.update_string(&new_string);
self.string.push_str(hint);
self.do_update_logic();
}
}