Skip to content

Commit

Permalink
suggestion: added style
Browse files Browse the repository at this point in the history
supports reverse highlighting using nu.config

```nushell
style: {
    text: white
    selected_text: {
        attr: r
    }
    description_text: white_dimmed
}
```
  • Loading branch information
rsteube committed Dec 30, 2023
1 parent f396223 commit c2bf6c5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 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 replacement
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
2 changes: 1 addition & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ impl Reedline {
self.get_history_session_id(),
)))
.unwrap_or_else(|_| Vec::new())
.get(0)
.first()

Check warning on line 1541 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1541

Added line #L1541 was not covered by tests
.and_then(|history| history.command_line.split_whitespace().next_back())
.map(|token| (parsed.remainder.len(), indicator.len(), token.to_string())),
});
Expand Down
21 changes: 17 additions & 4 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,16 @@ 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$}{}{}{}",
"{}{}{:max$}{}{}{}{}{}",
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse()
.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
self.color.selected_text_style.prefix(),
&suggestion.value,
self.color.description_style.reverse().prefix(),
self.color.selected_text_style.prefix(),

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

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L355-L356

Added lines #L355 - L356 were not covered by tests
description
.chars()
.take(right_text_size)
Expand All @@ -358,7 +365,12 @@ impl ColumnarMenu {
)
} else {
format!(
"{}{}{}{:>empty$}{}",
"{}{}{}{}{:>empty$}{}",
suggestion
.style
.unwrap_or(self.color.text_style)
.reverse()
.prefix(),

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

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L368-L373

Added lines #L368 - L373 were not covered by tests
self.color.selected_text_style.prefix(),
&suggestion.value,
RESET,
Expand All @@ -372,7 +384,7 @@ 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 387 in src/menu/columnar_menu.rs

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L387

Added line #L387 was not covered by tests
&suggestion.value,
RESET,
self.color.description_style.prefix(),
Expand All @@ -388,7 +400,7 @@ impl ColumnarMenu {
} else {
format!(
"{}{}{}{}{:>empty$}{}{}",
self.color.text_style.prefix(),
suggestion.style.unwrap_or(self.color.text_style).prefix(),

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

View check run for this annotation

Codecov / codecov/patch

src/menu/columnar_menu.rs#L403

Added line #L403 was not covered by tests
&suggestion.value,
RESET,
self.color.description_style.prefix(),
Expand Down Expand Up @@ -806,6 +818,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 c2bf6c5

Please sign in to comment.