This commit is contained in:
Simon Gardling 2022-04-11 13:44:35 -04:00
parent da0c3ebb78
commit 3120f8781d
2 changed files with 26 additions and 23 deletions

View File

@ -71,6 +71,13 @@ impl<'a> HintEnum<'a> {
#[allow(dead_code)] #[allow(dead_code)]
pub fn is_some(&self) -> bool { !self.is_none() } pub fn is_some(&self) -> bool { !self.is_none() }
pub fn is_single(&self) -> bool {
match self {
HintEnum::Single(_) => true,
_ => false,
}
}
} }
include!(concat!(env!("OUT_DIR"), "/codegen.rs")); include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

View File

@ -15,7 +15,8 @@ enum Movement {
pub struct AutoComplete<'a> { pub struct AutoComplete<'a> {
pub i: usize, pub i: usize,
pub hint: &'a HintEnum<'a>, pub hint: &'a HintEnum<'a>,
pub string: Option<String>, pub string: String,
pub first: bool,
} }
impl Default for Movement { impl Default for Movement {
@ -27,17 +28,18 @@ impl<'a> Default for AutoComplete<'a> {
AutoComplete { AutoComplete {
i: 0, i: 0,
hint: &HintEnum::None, hint: &HintEnum::None,
string: None, string: String::new(),
first: true,
} }
} }
} }
impl<'a> AutoComplete<'a> { impl<'a> AutoComplete<'a> {
fn update(&mut self, string: &str) { fn update(&mut self, string: &str) {
let new_func_option = Some(string.to_string()); if (&self.string != string) | self.first {
if self.string != new_func_option { self.string = string.to_string();
self.string = new_func_option;
self.hint = generate_hint(string); self.hint = generate_hint(string);
self.first = false;
} }
} }
@ -46,16 +48,11 @@ impl<'a> AutoComplete<'a> {
return; return;
} }
// self.string needs to be Some for this to work __DO NOT REMOVE THIS ASSERT__
assert!(self.string.is_some());
match self.hint { match self.hint {
HintEnum::Many(hints) => { HintEnum::Many(hints) => {
if movement == &Movement::Complete { if movement == &Movement::Complete {
// use unwrap_unchecked as self.string is already asserted as Some println!("applying multicomplete");
unsafe { self.string += hints[self.i];
*self.string.as_mut().unwrap_unchecked() += hints[self.i];
}
return; return;
} }
@ -79,10 +76,7 @@ impl<'a> AutoComplete<'a> {
} }
HintEnum::Single(hint) => { HintEnum::Single(hint) => {
if movement == &Movement::Complete { if movement == &Movement::Complete {
// use unwrap_unchecked as self.string is already asserted as Some self.string += hint;
unsafe {
*self.string.as_mut().unwrap_unchecked() += hint;
}
} }
} }
HintEnum::None => {} HintEnum::None => {}
@ -110,7 +104,6 @@ impl<'a> AutoComplete<'a> {
let enter_pressed = ui.input_mut().consume_key(Modifiers::NONE, Key::Enter); let enter_pressed = ui.input_mut().consume_key(Modifiers::NONE, Key::Enter);
let tab_pressed = ui.input_mut().consume_key(Modifiers::NONE, Key::Tab); let tab_pressed = ui.input_mut().consume_key(Modifiers::NONE, Key::Tab);
if enter_pressed | tab_pressed | ui.input().key_pressed(Key::ArrowRight) { if enter_pressed | tab_pressed | ui.input().key_pressed(Key::ArrowRight) {
println!("complete");
movement = Movement::Complete; movement = Movement::Complete;
} }
@ -120,10 +113,12 @@ impl<'a> AutoComplete<'a> {
let re = func_edit.id(te_id).ui(ui); let re = func_edit.id(te_id).ui(ui);
if ui.input().key_pressed(Key::ArrowDown) { if !self.hint.is_single() {
movement = Movement::Down; if ui.input().key_pressed(Key::ArrowDown) {
} else if ui.input().key_pressed(Key::ArrowUp) { movement = Movement::Down;
movement = Movement::Up; } else if ui.input().key_pressed(Key::ArrowUp) {
movement = Movement::Up;
}
} }
self.interact_back(&movement); self.interact_back(&movement);
@ -144,7 +139,7 @@ impl<'a> AutoComplete<'a> {
}); });
if clicked { if clicked {
*string += hints[self.i]; self.string += hints[self.i];
// don't need this here as it simply won't be display next frame // don't need this here as it simply won't be display next frame
// ui.memory().close_popup(); // ui.memory().close_popup();
@ -171,6 +166,7 @@ impl<'a> AutoComplete<'a> {
}, },
}))); })));
TextEdit::store_state(ui.ctx(), te_id, state); TextEdit::store_state(ui.ctx(), te_id, state);
*string = self.string.clone();
} }
} }
} }
@ -185,7 +181,7 @@ mod tests {
auto_complete.interact_back(&movement); auto_complete.interact_back(&movement);
let output_string = auto_complete.clone().string; let output_string = auto_complete.clone().string;
(auto_complete, output_string.unwrap_or_default()) (auto_complete, output_string)
} }
#[test] #[test]