Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom highlight support [WIP] #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ enum Boundary {
Select(Style),
#[cfg(feature = "search")]
Search(Style),
Custom(Style, u8), // style, priority
End,
}

impl Boundary {
fn cmp(&self, other: &Boundary) -> Ordering {
fn rank(b: &Boundary) -> u8 {
match b {
Boundary::Cursor(_) => 3,
Boundary::Cursor(_) => 30,
#[cfg(feature = "search")]
Boundary::Search(_) => 2,
Boundary::Select(_) => 1,
Boundary::Search(_) => 20,
Boundary::Select(_) => 10,
Boundary::Custom(_, p) => *p,
Boundary::End => 0,
}
}
Expand All @@ -38,6 +40,7 @@ impl Boundary {
Boundary::Select(s) => Some(*s),
#[cfg(feature = "search")]
Boundary::Search(s) => Some(*s),
Boundary::Custom(s, _) => Some(*s),
Boundary::End => None,
}
}
Expand Down Expand Up @@ -156,13 +159,15 @@ impl<'a> LineHighlighter<'a> {
}
}

pub fn selection(
// Shared code for selection and custom highlights
fn multiline_highlight(
&mut self,
current_row: usize,
start_row: usize,
start_off: usize,
end_row: usize,
end_off: usize,
boundary: Boundary,
) {
let (start, end) = if current_row == start_row {
if start_row == end_row {
Expand All @@ -180,12 +185,49 @@ impl<'a> LineHighlighter<'a> {
return;
};
if start != end {
self.boundaries
.push((Boundary::Select(self.select_style), start));
self.boundaries.push((boundary, start));
self.boundaries.push((Boundary::End, end));
}
}

pub fn selection(
&mut self,
current_row: usize,
start_row: usize,
start_off: usize,
end_row: usize,
end_off: usize,
) {
self.multiline_highlight(
current_row,
start_row,
start_off,
end_row,
end_off,
Boundary::Select(self.select_style),
);
}

pub fn custom(
&mut self,
current_row: usize,
start_row: usize,
start_off: usize,
end_row: usize,
end_off: usize,
style: Style,
priority: u8,
) {
self.multiline_highlight(
current_row,
start_row,
start_off,
end_row,
end_off,
Boundary::Custom(style, priority),
);
}

pub fn into_spans(self) -> Line<'a> {
let Self {
line,
Expand Down
43 changes: 43 additions & 0 deletions src/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ impl fmt::Display for YankText {
}
}

#[derive(Clone, Debug)]
struct CustomHighlight {
range: ((usize, usize), (usize, usize)),
style: Style,
priority: u8,
}

/// A type to manage state of textarea. These are some important methods:
///
/// - [`TextArea::default`] creates an empty textarea.
Expand Down Expand Up @@ -125,6 +132,7 @@ pub struct TextArea<'a> {
mask: Option<char>,
selection_start: Option<(usize, usize)>,
select_style: Style,
custom_highlights: Vec<CustomHighlight>,
}

/// Convert any iterator whose elements can be converted into [`String`] into [`TextArea`]. Each [`String`] element is
Expand Down Expand Up @@ -230,6 +238,7 @@ impl<'a> TextArea<'a> {
mask: None,
selection_start: None,
select_style: Style::default().bg(Color::LightBlue),
custom_highlights: Default::default(),
}
}

Expand Down Expand Up @@ -1355,6 +1364,23 @@ impl<'a> TextArea<'a> {
self.selection_start = None;
}

pub fn custom_highlight(
&mut self,
range: ((usize, usize), (usize, usize)),
style: Style,
priority: u8,
) {
self.custom_highlights.push(CustomHighlight {
range,
style,
priority,
});
}

pub fn clear_custom_highlight(&mut self) {
self.custom_highlights.clear();
}

/// Select the entire text. Cursor moves to the end of the text buffer. When text selection is already ongoing,
/// it is canceled.
/// ```
Expand Down Expand Up @@ -1612,6 +1638,23 @@ impl<'a> TextArea<'a> {
hl.selection(row, start.row, start.offset, end.row, end.offset);
}

for CustomHighlight {
range: ((start_row, start_offset), (end_row, end_offset)),
style,
priority,
} in &self.custom_highlights
{
hl.custom(
row,
*start_row,
*start_offset,
*end_row,
*end_offset,
*style,
*priority,
);
}

hl.into_spans()
}

Expand Down