fix suggestions

This commit is contained in:
Simon Gardling 2022-03-29 13:40:07 -04:00
parent c653c5a1c0
commit bb91dc5983
2 changed files with 40 additions and 35 deletions

View File

@ -490,7 +490,7 @@ impl MathApp {
.clicked(); .clicked();
// Contains the function string in a text box that the user can edit // Contains the function string in a text box that the user can edit
let hint = generate_hint(&self.func_strs[i]); let hint = generate_hint(&self.func_strs[i]).unwrap_or_default();
let func_edit_focus = TextEdit::singleline(&mut self.func_strs[i]) let func_edit_focus = TextEdit::singleline(&mut self.func_strs[i])
.hint_text(&hint, true) .hint_text(&hint, true)

View File

@ -1,9 +1,9 @@
use crate::misc::{chars_take, common_substring}; use crate::misc::{chars_take, common_substring};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
pub fn generate_hint(input: &str) -> String { pub fn generate_hint(input: &str) -> Option<String> {
if input.is_empty() { if input.is_empty() {
return "x^2".to_owned(); return Some("x^2".to_owned());
} }
let chars: Vec<char> = input.chars().collect(); let chars: Vec<char> = input.chars().collect();
@ -17,50 +17,47 @@ pub fn generate_hint(input: &str) -> String {
}); });
if open_parens > closed_parens { if open_parens > closed_parens {
return ")".to_owned(); return Some(")".to_owned());
} }
let len = chars.len(); let len = chars.len();
if len >= 5 { if len >= 5 {
let result_five = get_completion(chars_take(&chars, 5)); let result_five = get_completion(chars_take(&chars, 5));
if result_five.is_some() {
if let Some(output) = result_five { return result_five;
return output;
} }
} }
if len >= 4 { if len >= 4 {
let result_four = get_completion(chars_take(&chars, 4)); let result_four = get_completion(chars_take(&chars, 4));
if result_four.is_some() {
if let Some(output) = result_four { return result_four;
return output;
} }
} }
if len >= 3 { if len >= 3 {
let result_three = get_completion(chars_take(&chars, 3)); let result_three = get_completion(chars_take(&chars, 3));
if result_three.is_some() {
if let Some(output) = result_three { return result_three;
return output;
} }
} }
if len >= 2 { if len >= 2 {
let result_two = get_completion(chars_take(&chars, 2)); let result_two = get_completion(chars_take(&chars, 2));
if let Some(output) = result_two { if result_two.is_some() {
return output; return result_two;
} }
} }
String::new() None
} }
fn gen_completion_hashmap(input: Vec<String>) -> HashMap<String, String> { fn gen_completion_hashmap(input: Vec<String>) -> HashMap<String, String> {
let mut tuple_list: Vec<(String, String)> = Vec::new(); let mut tuple_list: Vec<(String, String)> = Vec::new();
for entry in input { for entry in input {
for i in 1..=entry.len() { for i in 1..entry.len() {
let (first, last) = entry.split_at(i); let (first, last) = entry.split_at(i);
tuple_list.push((first.to_string(), last.to_string())); tuple_list.push((first.to_string(), last.to_string()));
} }
@ -106,10 +103,11 @@ fn gen_completion_hashmap(input: Vec<String>) -> HashMap<String, String> {
} }
if let Some(common_substr_unwrapped) = common_substr { if let Some(common_substr_unwrapped) = common_substr {
if !common_substr_unwrapped.is_empty() {
output.insert(key.clone(), common_substr_unwrapped.replace(&key, "")); output.insert(key.clone(), common_substr_unwrapped.replace(&key, ""));
} }
} }
// println!("{:?}", output); }
output output
} }
@ -153,8 +151,8 @@ mod tests {
("sin(x)", ""), ("sin(x)", ""),
("x^x", ""), ("x^x", ""),
("(x+1)(x-1", ")"), ("(x+1)(x-1", ")"),
// ("lo", "g("), ("lo", "g"),
// ("log", "("), ("log", ""), // because there are multiple log functions
("asi", "n("), ("asi", "n("),
("asin", "("), ("asin", "("),
("fl", "oor("), ("fl", "oor("),
@ -167,12 +165,31 @@ mod tests {
for (key, value) in values { for (key, value) in values {
println!("{} + {}", key, value); println!("{} + {}", key, value);
assert_eq!(generate_hint(key), value.to_owned()); assert_eq!(
generate_hint(key).unwrap_or_default(),
value.to_owned()
);
} }
} }
#[test] #[test]
fn completion_hashmap_test() { fn completion_hashmap_test() {
let values = hashmap_test_gen();
for (key, value) in values {
println!(
"{} + {}",
key,
match value.clone() {
Some(x) => x.clone(),
None => "(No completion)".to_string(),
}
);
assert_eq!(get_completion(key.to_string()), value);
}
}
fn hashmap_test_gen() -> HashMap<String, Option<String>> {
let mut values: HashMap<String, Option<String>> = HashMap::new(); let mut values: HashMap<String, Option<String>> = HashMap::new();
let processed_func: Vec<String> = SUPPORTED_FUNCTIONS let processed_func: Vec<String> = SUPPORTED_FUNCTIONS
@ -213,18 +230,6 @@ mod tests {
for (key, value) in manual_values { for (key, value) in manual_values {
values.insert(key.to_string(), value.map(|x| x.to_string())); values.insert(key.to_string(), value.map(|x| x.to_string()));
} }
values
for (key, value) in values {
println!(
"{} + {}",
key,
match value.clone() {
Some(x) => x.clone(),
None => "(No completion)".to_string(),
}
);
assert_eq!(get_completion(key.to_string()), value);
}
} }
} }