diff --git a/src/function.rs b/src/function.rs index c8d326c..5b6eedf 100644 --- a/src/function.rs +++ b/src/function.rs @@ -58,7 +58,7 @@ pub struct FunctionEntry { extrema_data: Vec, roots_data: Vec, - autocomplete: AutoComplete, + autocomplete: AutoComplete<'static>, test_result: Option, } diff --git a/src/suggestions.rs b/src/suggestions.rs index 34564db..ca47063 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -1,9 +1,12 @@ use crate::misc::chars_take; +const HINTENUM_EMPTY: HintEnum = HintEnum::Single("x^2"); +const HINTENUM_CLOSED_PARENS: HintEnum = HintEnum::Single(")"); + /// Generate a hint based on the input `input`, returns an `Option` -pub fn generate_hint(input: &str) -> HintEnum<'static> { +pub fn generate_hint<'a>(input: &str) -> &'a HintEnum<'a> { if input.is_empty() { - return HintEnum::Single("x^2"); + return &HINTENUM_EMPTY; } let chars: Vec = input.chars().collect::>(); @@ -17,18 +20,23 @@ pub fn generate_hint(input: &str) -> HintEnum<'static> { }); if open_parens > closed_parens { - return HintEnum::Single(")"); + return &HINTENUM_CLOSED_PARENS; } let len = chars.len(); - for i in (1..=MAX_COMPLETION_LEN).rev().filter(|i| len >= *i) { - if let Some(output) = get_completion(&chars_take(&chars, i)) { - return output.clone(); + for key in (1..=MAX_COMPLETION_LEN) + .rev() + .filter(|i| len >= *i) + .map(|i| chars_take(&chars, i)) + .filter(|cut_string| !cut_string.is_empty()) + { + if let Some(output) = COMPLETION_HASHMAP.get(&key) { + return output; } } - HintEnum::None + &HintEnum::None } #[derive(Clone, PartialEq)] @@ -66,17 +74,6 @@ impl HintEnum<'static> { include!(concat!(env!("OUT_DIR"), "/codegen.rs")); -/// Gets completion from `COMPLETION_HASHMAP` -pub fn get_completion(key: &str) -> Option<&HintEnum<'static>> { - // If key is empty, just return None - if key.is_empty() { - return None; - } - - // Get and clone the recieved data - COMPLETION_HASHMAP.get(key) -} - #[cfg(test)] mod tests { use std::collections::HashMap; @@ -97,7 +94,7 @@ mod tests { for (key, value) in values { println!("{} + {:?}", key, value); - assert_eq!(generate_hint(key), value); + assert_eq!(generate_hint(key), &value); } } diff --git a/src/widgets.rs b/src/widgets.rs index 7aafafa..ce94b9a 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -4,25 +4,25 @@ use egui::{text::CCursor, text_edit::CursorRange, Key, Modifiers, TextEdit, Widg use epaint::text::cursor::{Cursor, PCursor, RCursor}; #[derive(Clone)] -pub struct AutoComplete { +pub struct AutoComplete<'a> { pub i: usize, - pub hint: HintEnum<'static>, + pub hint: &'a HintEnum<'static>, pub func_str: Option, pub changed: bool, } -impl Default for AutoComplete { - fn default() -> AutoComplete { +impl Default for AutoComplete<'static> { + fn default() -> AutoComplete<'static> { AutoComplete { i: 0, - hint: HintEnum::None, + hint: &HintEnum::None, func_str: None, changed: true, } } } -impl AutoComplete { +impl<'a> AutoComplete<'a> { fn changed(&mut self, string: &str) { if self.func_str != Some(string.to_string()) { self.changed = true; @@ -57,7 +57,7 @@ impl AutoComplete { if let HintEnum::Single(single_hint) = self.hint { let func_edit_2 = func_edit; - func_edit = func_edit_2.hint_text(single_hint); + func_edit = func_edit_2.hint_text(*single_hint); } let re = func_edit.id(te_id).ui(ui);