From 5e7a9ea1a2bd1f8beac704bdb56b7ab54fe8613b Mon Sep 17 00:00:00 2001 From: rsteube Date: Wed, 24 Jan 2024 15:40:25 +0100 Subject: [PATCH] suggestion: added display --- src/completion/base.rs | 9 +++++++++ src/completion/default.rs | 25 ++++++++++++++++--------- src/completion/history.rs | 1 + src/menu/columnar_menu.rs | 25 +++++++++++++++---------- src/menu/ide_menu.rs | 1 + src/menu/menu_functions.rs | 2 ++ 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/completion/base.rs b/src/completion/base.rs index 909c467b..c80f2e1a 100644 --- a/src/completion/base.rs +++ b/src/completion/base.rs @@ -78,6 +78,8 @@ pub trait Completer: Send { pub struct Suggestion { /// String replacement that will be introduced to the the buffer pub value: String, + /// Optional display value for the replacement + pub display: Option, /// Optional description for the replacement pub description: Option, /// Optional style for the replacement @@ -91,3 +93,10 @@ pub struct Suggestion { /// This helps to avoid that a completer repeats the complete suggestion. pub append_whitespace: bool, } + +impl Suggestion { + /// Returns display if set, otherwise value. + pub fn display_or_value(&self) -> &String { + self.display.as_ref().unwrap_or(&self.value) + } +} diff --git a/src/completion/default.rs b/src/completion/default.rs index c8823c24..88a814af 100644 --- a/src/completion/default.rs +++ b/src/completion/default.rs @@ -55,17 +55,17 @@ impl Completer for DefaultCompleter { /// assert_eq!( /// completions.complete("bat",3), /// vec![ - /// Suggestion {value: "batcave".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, - /// Suggestion {value: "batman".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, - /// Suggestion {value: "batmobile".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, + /// Suggestion {value: "batcave".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, + /// Suggestion {value: "batman".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, + /// Suggestion {value: "batmobile".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false}, /// ]); /// /// assert_eq!( /// completions.complete("to the bat",10), /// vec![ - /// Suggestion {value: "batcave".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, - /// Suggestion {value: "batman".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, - /// Suggestion {value: "batmobile".into(), description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, + /// Suggestion {value: "batcave".into(), display: None, description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, + /// Suggestion {value: "batman".into(), display: None, description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, + /// Suggestion {value: "batmobile".into(), display: None, description: None, style: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false}, /// ]); /// ``` fn complete(&mut self, line: &str, pos: usize) -> Vec { @@ -101,6 +101,7 @@ impl Completer for DefaultCompleter { Suggestion { value: format!("{span_line}{ext}"), + display: None, description: None, style: None, extra: None, @@ -177,15 +178,15 @@ impl DefaultCompleter { /// completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect()); /// assert_eq!( /// completions.complete("te",2), - /// vec![Suggestion {value: "test".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]); + /// vec![Suggestion {value: "test".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]); /// /// let mut completions = DefaultCompleter::with_inclusions(&['-', '_']); /// completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect()); /// assert_eq!( /// completions.complete("te",2), /// vec![ - /// Suggestion {value: "test-hyphen".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}, - /// Suggestion {value: "test_underscore".into(), description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}, + /// Suggestion {value: "test-hyphen".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}, + /// Suggestion {value: "test_underscore".into(), display: None, description: None, style: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}, /// ]); /// ``` pub fn with_inclusions(incl: &[char]) -> Self { @@ -374,6 +375,7 @@ mod tests { [ Suggestion { value: "null".into(), + display: None, description: None, style: None, extra: None, @@ -382,6 +384,7 @@ mod tests { }, Suggestion { value: "number".into(), + display: None, description: None, style: None, extra: None, @@ -390,6 +393,7 @@ mod tests { }, Suggestion { value: "nushell".into(), + display: None, description: None, style: None, extra: None, @@ -418,6 +422,7 @@ mod tests { [ Suggestion { value: "test".into(), + display: None, description: None, style: None, extra: None, @@ -426,6 +431,7 @@ mod tests { }, Suggestion { value: "this is the reedline crate".into(), + display: None, description: None, style: None, extra: None, @@ -434,6 +440,7 @@ mod tests { }, Suggestion { value: "this is the reedline crate".into(), + display: None, description: None, style: None, extra: None, diff --git a/src/completion/history.rs b/src/completion/history.rs index 015381ac..7cf0502b 100644 --- a/src/completion/history.rs +++ b/src/completion/history.rs @@ -58,6 +58,7 @@ impl<'menu> HistoryCompleter<'menu> { Suggestion { value: value.to_string(), + display: None, description: None, style: None, extra: None, diff --git a/src/menu/columnar_menu.rs b/src/menu/columnar_menu.rs index c32d1f71..c86de0ad 100644 --- a/src/menu/columnar_menu.rs +++ b/src/menu/columnar_menu.rs @@ -351,7 +351,7 @@ impl ColumnarMenu { .reverse() .prefix(), self.color.selected_text_style.prefix(), - &suggestion.value, + suggestion.display_or_value(), RESET, self.color.description_style.reverse().prefix(), self.color.selected_text_style.prefix(), @@ -373,7 +373,7 @@ impl ColumnarMenu { .reverse() .prefix(), self.color.selected_text_style.prefix(), - &suggestion.value, + suggestion.display_or_value(), RESET, "", self.end_of_line(column), @@ -386,7 +386,7 @@ impl ColumnarMenu { format!( "{}{:max$}{}{}{}{}{}", suggestion.style.unwrap_or(self.color.text_style).prefix(), - &suggestion.value, + suggestion.display_or_value(), RESET, self.color.description_style.prefix(), description @@ -402,7 +402,7 @@ impl ColumnarMenu { format!( "{}{}{}{}{:>empty$}{}{}", suggestion.style.unwrap_or(self.color.text_style).prefix(), - &suggestion.value, + suggestion.display_or_value(), RESET, self.color.description_style.prefix(), "", @@ -419,7 +419,7 @@ impl ColumnarMenu { format!( "{}{:max$}{}{}", marker, - &suggestion.value, + suggestion.display_or_value(), description .chars() .take(empty_space) @@ -436,7 +436,7 @@ impl ColumnarMenu { format!( "{}{}{:>empty$}{}", marker, - &suggestion.value, + suggestion.display_or_value(), "", self.end_of_line(column), empty = empty_space.saturating_sub(marker.len()), @@ -590,15 +590,17 @@ impl Menu for ColumnarMenu { self.working_details.col_width = painter.screen_width() as usize; self.longest_suggestion = self.get_values().iter().fold(0, |prev, suggestion| { - if prev >= suggestion.value.len() { + let suggestion_length = suggestion.display_or_value().len(); + if prev >= suggestion_length { prev } else { - suggestion.value.len() + suggestion_length } }); } else { let max_width = self.get_values().iter().fold(0, |acc, suggestion| { - let str_len = suggestion.value.len() + self.default_details.col_padding; + let str_len = + suggestion.display_or_value().len() + self.default_details.col_padding; if str_len > acc { str_len } else { @@ -734,7 +736,9 @@ impl Menu for ColumnarMenu { // Correcting the enumerate index based on the number of skipped values let index = index + skip_values; let column = index as u16 % self.get_cols(); - let empty_space = self.get_width().saturating_sub(suggestion.value.len()); + let empty_space = self + .get_width() + .saturating_sub(suggestion.display_or_value().len()); self.create_string(suggestion, index, column, empty_space, use_ansi_coloring) }) @@ -825,6 +829,7 @@ mod tests { fn fake_suggestion(name: &str, pos: usize) -> Suggestion { Suggestion { value: name.to_string(), + display: None, description: None, style: None, extra: None, diff --git a/src/menu/ide_menu.rs b/src/menu/ide_menu.rs index c2ef4b64..764997ff 100644 --- a/src/menu/ide_menu.rs +++ b/src/menu/ide_menu.rs @@ -1472,6 +1472,7 @@ mod tests { fn fake_suggestion(name: &str, pos: usize) -> Suggestion { Suggestion { value: name.to_string(), + display: None, description: None, style: None, extra: None, diff --git a/src/menu/menu_functions.rs b/src/menu/menu_functions.rs index 247b8736..1a303e5f 100644 --- a/src/menu/menu_functions.rs +++ b/src/menu/menu_functions.rs @@ -496,6 +496,7 @@ mod tests { .into_iter() .map(|s| Suggestion { value: s.into(), + display: None, description: None, style: None, extra: None, @@ -516,6 +517,7 @@ mod tests { .into_iter() .map(|s| Suggestion { value: s.into(), + display: None, description: None, style: None, extra: None,