Skip to content

Commit

Permalink
suggestion: added style
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 29, 2023
1 parent f396223 commit c48ff32
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/completion/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use nu_ansi_term::Style;

/// A span of source code, with positions in bytes
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Span {
Expand Down Expand Up @@ -60,6 +62,8 @@ pub struct Suggestion {
pub value: String,
/// Optional description for the replacement
pub description: Option<String>,
/// Optional style for the value
pub style: Option<Style>,
/// Optional vector of strings in the suggestion. These can be used to
/// represent examples coming from a suggestion
pub extra: Option<Vec<String>>,
Expand Down
22 changes: 13 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, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
/// 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},
/// ]);
///
/// assert_eq!(
/// completions.complete("to the bat",10),
/// vec![
/// Suggestion {value: "batcave".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
/// 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},
/// ]);
/// ```
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
Expand Down Expand Up @@ -99,6 +99,7 @@ impl Completer for DefaultCompleter {
Suggestion {
value: format!("{span_line}{ext}"),
description: None,
style: None,
extra: None,
span,
append_whitespace: false,
Expand Down Expand Up @@ -173,15 +174,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, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]);
/// vec![Suggestion {value: "test".into(), 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, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// Suggestion {value: "test_underscore".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
/// 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},
/// ]);
/// ```
pub fn with_inclusions(incl: &[char]) -> Self {
Expand Down Expand Up @@ -371,20 +372,23 @@ mod tests {
Suggestion {
value: "null".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
},
Suggestion {
value: "number".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
},
Suggestion {
value: "nushell".into(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: 3 },
append_whitespace: false,
Expand Down
1 change: 1 addition & 0 deletions src/completion/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<'menu> HistoryCompleter<'menu> {
Suggestion {
value: value.to_string(),
description: None,
style: None,

Check warning on line 62 in src/completion/history.rs

View check run for this annotation

Codecov / codecov/patch

src/completion/history.rs#L62

Added line #L62 was not covered by tests
extra: None,
span,
append_whitespace: false,
Expand Down
24 changes: 17 additions & 7 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,14 @@ impl ColumnarMenu {
let left_text_size = self.longest_suggestion + self.default_details.col_padding;
let right_text_size = self.get_width().saturating_sub(left_text_size);
format!(
"{}{:max$}{}{}{}",
self.color.selected_text_style.prefix(),
"{}{:max$}{}{}{}{}",
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse() // TODO support selected_text_style
.prefix(),

Check warning on line 352 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L347-L352

Added lines #L347 - L352 were not covered by tests
&suggestion.value,
self.color.description_style.reverse().prefix(),

Check warning on line 354 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L354

Added line #L354 was not covered by tests
description
.chars()
.take(right_text_size)
Expand All @@ -359,7 +364,11 @@ impl ColumnarMenu {
} else {
format!(
"{}{}{}{:>empty$}{}",
self.color.selected_text_style.prefix(),
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse() // TODO support selected_text_style
.prefix(),

Check warning on line 371 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L367-L371

Added lines #L367 - L371 were not covered by tests
&suggestion.value,
RESET,
"",
Expand All @@ -372,9 +381,9 @@ impl ColumnarMenu {
let right_text_size = self.get_width().saturating_sub(left_text_size);
format!(
"{}{:max$}{}{}{}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),

Check warning on line 384 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L384

Added line #L384 was not covered by tests
&suggestion.value,
RESET,
RESET, // TODO affects space suffix of value

Check warning on line 386 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L386

Added line #L386 was not covered by tests
self.color.description_style.prefix(),
description
.chars()
Expand All @@ -388,9 +397,9 @@ impl ColumnarMenu {
} else {
format!(
"{}{}{}{}{:>empty$}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),

Check warning on line 400 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L400

Added line #L400 was not covered by tests
&suggestion.value,
RESET,
RESET, // TODO affects space suffix of value

Check warning on line 402 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L402

Added line #L402 was not covered by tests
self.color.description_style.prefix(),
"",
RESET,
Expand Down Expand Up @@ -806,6 +815,7 @@ mod tests {
Suggestion {
value: name.to_string(),
description: None,
style: None,
extra: None,
span: Span { start: 0, end: pos },
append_whitespace: false,
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 @@ -488,6 +488,7 @@ mod tests {
.map(|s| Suggestion {
value: s.into(),
description: None,
style: None,
extra: None,
span: Span::new(0, s.len()),
append_whitespace: false,
Expand All @@ -507,6 +508,7 @@ mod tests {
.map(|s| Suggestion {
value: s.into(),
description: None,
style: None,
extra: None,
span: Span::new(0, s.len()),
append_whitespace: false,
Expand Down

0 comments on commit c48ff32

Please sign in to comment.