Skip to content

Commit

Permalink
luggestion: added display
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Feb 9, 2024
1 parent ab1b47e commit a5e0b9f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Optional description for the replacement
pub description: Option<String>,
/// Optional style for the replacement
Expand All @@ -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)
}
}
25 changes: 16 additions & 9 deletions src/completion/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\r\nbat",11),
/// vec![
/// Suggestion {value: "batcave".into(), description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// Suggestion {value: "batcave".into(), display: None, description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), display: None, description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), display: None, description: None, style: None, extra: None, span: Span { start: 8, end: 11 }, append_whitespace: false},
/// ]);
/// ```
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
Expand Down Expand Up @@ -105,6 +105,7 @@ impl Completer for DefaultCompleter {

Suggestion {
value: format!("{span_line}{ext}"),
display: None,
description: None,
style: None,
extra: None,
Expand Down Expand Up @@ -182,15 +183,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 {
Expand Down Expand Up @@ -379,6 +380,7 @@ mod tests {
[
Suggestion {
value: "null".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -387,6 +389,7 @@ mod tests {
},
Suggestion {
value: "number".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -395,6 +398,7 @@ mod tests {
},
Suggestion {
value: "nushell".into(),
display: None,
description: None,
style: None,
extra: None,
Expand Down Expand Up @@ -423,6 +427,7 @@ mod tests {
[
Suggestion {
value: "test".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -431,6 +436,7 @@ mod tests {
},
Suggestion {
value: "this is the reedline crate".into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -439,6 +445,7 @@ mod tests {
},
Suggestion {
value: "this is the reedline crate".into(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
1 change: 1 addition & 0 deletions src/completion/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<'menu> HistoryCompleter<'menu> {

Suggestion {
value: value.to_string(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
23 changes: 16 additions & 7 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ impl ColumnarMenu {
let match_len = self.working_details.shortest_base_string.len();

// Split string so the match text can be styled
let (match_str, remaining_str) = suggestion.value.split_at(match_len);
let (match_str, remaining_str) = if let Some(display) = suggestion.display.as_ref() {
("", display.as_str())
} else {
suggestion.value.split_at(match_len)
};

let suggestion_style_prefix = suggestion
.style
Expand Down Expand Up @@ -397,7 +401,7 @@ impl ColumnarMenu {
format!(
"{}{:max$}{}{}",
marker,
&suggestion.value,
suggestion.display_or_value(),
description
.chars()
.take(empty_space)
Expand All @@ -414,7 +418,7 @@ impl ColumnarMenu {
format!(
"{}{}{:>empty$}{}",
marker,
&suggestion.value,
suggestion.display_or_value(),
"",
self.end_of_line(column),
empty = empty_space.saturating_sub(marker.len()),
Expand Down Expand Up @@ -530,15 +534,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 {
Expand Down Expand Up @@ -654,7 +660,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)
})
Expand Down Expand Up @@ -745,6 +753,7 @@ mod tests {
fn fake_suggestion(name: &str, pos: usize) -> Suggestion {
Suggestion {
value: name.to_string(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
1 change: 1 addition & 0 deletions src/menu/ide_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ mod tests {
fn fake_suggestion(name: &str, pos: usize) -> Suggestion {
Suggestion {
value: name.to_string(),
display: None,
description: None,
style: None,
extra: None,
Expand Down
2 changes: 2 additions & 0 deletions src/menu/menu_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ mod tests {
.into_iter()
.map(|s| Suggestion {
value: s.into(),
display: None,
description: None,
style: None,
extra: None,
Expand All @@ -604,6 +605,7 @@ mod tests {
.into_iter()
.map(|s| Suggestion {
value: s.into(),
display: None,
description: None,
style: None,
extra: None,
Expand Down

0 comments on commit a5e0b9f

Please sign in to comment.