Skip to content

Commit

Permalink
Provide cursor position for highlighting (#324)
Browse files Browse the repository at this point in the history
Changes the `Highlighter` trait to include the position of the cursor in
the buffer. This allows it to make cursor aware highlighting such as
highlighting matching braces or code blocks

Prerequisite to address nushell/nushell#4325
  • Loading branch information
sholderbach authored Feb 26, 2022
1 parent bb7179c commit 97230d7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ impl Reedline {
// Highlight matches
let res_string = if self.use_ansi_coloring {
let match_highlighter = SimpleMatchHighlighter::new(substring);
let styled = match_highlighter.highlight(&res_string);
let styled = match_highlighter.highlight(&res_string, 0);
styled.render_simple()
} else {
res_string
Expand Down Expand Up @@ -1057,7 +1057,7 @@ impl Reedline {

let (before_cursor, after_cursor) = self
.highlighter
.highlight(buffer_to_paint)
.highlight(buffer_to_paint, cursor_position_in_buffer)
.render_around_insertion_point(
cursor_position_in_buffer,
prompt.render_prompt_multiline_indicator().borrow(),
Expand Down
2 changes: 1 addition & 1 deletion src/highlighter/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ExampleHighlighter {
}

impl Highlighter for ExampleHighlighter {
fn highlight(&self, line: &str) -> StyledText {
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
let mut styled_text = StyledText::new();

if self
Expand Down
4 changes: 3 additions & 1 deletion src/highlighter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ pub use simple_match::SimpleMatchHighlighter;
/// return a `StyledText` object, which represents the contents of the original line as styled strings
pub trait Highlighter: Send {
/// The action that will handle the current buffer as a line and return the corresponding `StyledText` for the buffer
fn highlight(&self, line: &str) -> StyledText;
///
/// Cursor position as byte offsets in the string
fn highlight(&self, line: &str, cursor: usize) -> StyledText;
}
2 changes: 1 addition & 1 deletion src/highlighter/simple_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Default for SimpleMatchHighlighter {
}

impl Highlighter for SimpleMatchHighlighter {
fn highlight(&self, line: &str) -> StyledText {
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
let mut styled_text = StyledText::new();
if self.query.is_empty() {
styled_text.push((self.neutral_style, line.to_owned()));
Expand Down

0 comments on commit 97230d7

Please sign in to comment.