From f4aeb80cf86c547894f5ba236f05ac5159c37ae5 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 28 Mar 2022 20:57:51 -0400 Subject: [PATCH] autocompletion kinda maybe --- src/egui_app.rs | 12 ++++++-- src/parsing.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/egui_app.rs b/src/egui_app.rs index f3ce289..c76fe18 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -493,9 +493,15 @@ impl MathApp { // Contains the function string in a text box that the user can edit let hint = generate_hint(&self.func_strs[i]); - TextEdit::singleline(&mut self.func_strs[i]) - .hint_text(hint, true) - .ui(ui); + let func_edit_focus = TextEdit::singleline(&mut self.func_strs[i]) + .hint_text(hint.clone(), true) + .ui(ui) + .has_focus(); + + // If in focus and right arrow key was pressed, apply hint + if func_edit_focus && ui.input().key_down(Key::ArrowRight) { + self.func_strs[i] += &hint; + } }); let proc_func_str = process_func_str(&self.func_strs[i]); diff --git a/src/parsing.rs b/src/parsing.rs index eb0849b..fcd1119 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -218,6 +218,63 @@ pub fn generate_hint(input: &str) -> String { return ")".to_owned(); } + let len = chars.len(); + + if chars.len() >= 4 { + let result_two = match (chars[len - 4].to_string() + + &chars[len - 3].to_string() + + &chars[len - 2].to_string() + + &chars[len - 1].to_string()) + .as_str() + { + "asin" => Some("("), + + _ => None, + }; + + if let Some(output) = result_two { + return output.to_owned(); + } + } + + if chars.len() >= 3 { + let result_two = match (chars[len - 3].to_string() + + &chars[len - 2].to_string() + + &chars[len - 1].to_string()) + .as_str() + { + "log" => Some("("), + "sin" => Some("("), + "abs" => Some("("), + "cos" => Some("("), + "tan" => Some("("), + "asi" => Some("n("), + + _ => None, + }; + + if let Some(output) = result_two { + return output.to_owned(); + } + } + + if chars.len() >= 2 { + let result_two = match (chars[len - 2].to_string() + &chars[len - 1].to_string()).as_str() { + "lo" => Some("g("), + "si" => Some("n("), + "ab" => Some("s("), + "co" => Some("s("), + "ta" => Some("n("), + "as" => Some("in("), + + _ => None, + }; + + if let Some(output) = result_two { + return output.to_owned(); + } + } + String::new() } @@ -329,4 +386,25 @@ mod tests { test_process_helper(key, value); } } + + /// Tests to make sure hints are properly outputed based on input + #[test] + fn hint_test() { + let values = HashMap::from([ + ("", "x^2"), + ("sin(x", ")"), + ("sin(x)", ""), + ("x^x", ""), + ("(x+1)(x-1", ")"), + ("lo", "g("), + ("log", "("), + ("asi", "n("), + ("si", "n("), + ("asin", "("), + ]); + + for (key, value) in values { + assert_eq!(generate_hint(key), value.to_owned()); + } + } }