From c7e8865d7ee399074f36c06a58cb6552c59a6f0b Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 16 May 2022 13:28:23 -0400 Subject: [PATCH] add Hint accessors --- parsing/src/suggestions.rs | 20 ++++++++++++++++++++ src/function_manager.rs | 5 ++--- tests/parsing.rs | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/parsing/src/suggestions.rs b/parsing/src/suggestions.rs index a1ccb83..0bd4825 100644 --- a/parsing/src/suggestions.rs +++ b/parsing/src/suggestions.rs @@ -249,6 +249,26 @@ impl<'a> Hint<'a> { #[allow(dead_code)] pub const fn is_single(&self) -> bool { matches!(&self, &Hint::Single(_)) } + + #[inline] + #[allow(dead_code)] + pub const fn single(&self) -> Option<&&str> { + if let Hint::Single(data) = self { + Some(data) + } else { + None + } + } + + #[inline] + #[allow(dead_code)] + pub const fn many(&self) -> Option<&&[&str]> { + if let Hint::Many(data) = self { + Some(data) + } else { + None + } + } } include!(concat!(env!("OUT_DIR"), "/codegen.rs")); diff --git a/src/function_manager.rs b/src/function_manager.rs index 2c422e4..24a3b0e 100644 --- a/src/function_manager.rs +++ b/src/function_manager.rs @@ -3,7 +3,6 @@ use crate::function_entry::FunctionEntry; use crate::widgets::widgets_ontop; use egui::{Button, Id, Key, Modifiers, TextEdit, WidgetText}; use emath::vec2; -use parsing::Hint; use parsing::Movement; use serde::ser::SerializeStruct; use serde::Deserialize; @@ -104,7 +103,7 @@ impl FunctionManager { .id(*te_id) // Set widget's id to `te_id` .hint_text({ // If there's a single hint, go ahead and apply the hint here, if not, set the hint to an empty string - if let Hint::Single(single_hint) = function.autocomplete.hint { + if let Some(single_hint) = function.autocomplete.hint.single() { *single_hint } else { "" @@ -138,7 +137,7 @@ impl FunctionManager { function.autocomplete.register_movement(&movement); - if movement != Movement::Complete && let Hint::Many(hints) = function.autocomplete.hint { + if movement != Movement::Complete && let Some(hints) = function.autocomplete.hint.many() { // Doesn't need to have a number in id as there should only be 1 autocomplete popup in the entire gui let popup_id = ui.make_persistent_id("autocomplete_popup"); diff --git a/tests/parsing.rs b/tests/parsing.rs index 7207369..42b3734 100644 --- a/tests/parsing.rs +++ b/tests/parsing.rs @@ -273,3 +273,18 @@ fn get_last_term() { ); } } + +#[test] +fn hint_accessor() { + assert_eq!(Hint::Single("hint").many(), None); + assert_eq!(Hint::Single("hint").single(), Some(&"hint")); + + assert_eq!(Hint::Many(&["hint", "hint2"]).single(), None); + assert_eq!( + Hint::Many(&["hint", "hint2"]).many(), + Some(&["hint", "hint2"].as_slice()) + ); + + assert_eq!(Hint::None.single(), None); + assert_eq!(Hint::None.many(), None); +}