autocompletion kinda maybe

This commit is contained in:
Simon Gardling 2022-03-28 20:57:51 -04:00
parent 6d57d96bb2
commit f4aeb80cf8
2 changed files with 87 additions and 3 deletions

View File

@ -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]);

View File

@ -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());
}
}
}