use borrows for hints

This commit is contained in:
Simon Gardling 2022-04-07 10:32:52 -04:00
parent 3df2c166b1
commit 3521ff3a13
3 changed files with 24 additions and 27 deletions

View File

@ -58,7 +58,7 @@ pub struct FunctionEntry {
extrema_data: Vec<Value>,
roots_data: Vec<Value>,
autocomplete: AutoComplete,
autocomplete: AutoComplete<'static>,
test_result: Option<String>,
}

View File

@ -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<String>`
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<char> = input.chars().collect::<Vec<char>>();
@ -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);
}
}

View File

@ -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<String>,
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);