Skip to content

Commit

Permalink
Refactor highlight patching in table row in picker
Browse files Browse the repository at this point in the history
  • Loading branch information
sudormrfbin committed Jul 12, 2022
1 parent 4ef0863 commit 7d53b58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
31 changes: 9 additions & 22 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,18 +630,7 @@ impl<T: Item + 'static> Component for Picker<T> {
.map(|mut row| {
const TEMP_CELL_SEP: &str = " ";

let cells: Vec<_> = row
.cells
.iter()
.filter_map(|cell| {
cell.content
.lines
.iter()
.nth(0)
.map(|spans| String::from(spans))
})
.collect();
let line = cells.join(TEMP_CELL_SEP);
let line = row.cell_text().join(TEMP_CELL_SEP);

// Items are filtered by using the text returned by menu::Item::filter_text
// but we do highlighting here using the text in Row and therefore there
Expand All @@ -665,7 +654,7 @@ impl<T: Item + 'static> Component for Picker<T> {
// The starting byte index of the current (iterating) cell
let mut cell_start_byte_offset = 0;
for cell in row.cells.iter_mut() {
let spans = match cell.content.lines.iter().nth(0) {
let spans = match cell.content.lines.get(0) {
Some(s) => s,
None => continue,
};
Expand All @@ -682,17 +671,15 @@ impl<T: Item + 'static> Component for Picker<T> {
})
.map(|((grapheme_byte_offset, grapheme), style)| {
cell_len += grapheme.len();
let start = cell_start_byte_offset;

let grapheme_byte_range =
grapheme_byte_offset..grapheme_byte_offset + grapheme.len();
let start = cell_start_byte_offset;
if highlight_byte_ranges
.iter()
.find(|rng| {
rng.start >= start + grapheme_byte_range.start
&& rng.end <= start + grapheme_byte_range.end
})
.is_some()
{

if highlight_byte_ranges.iter().any(|hl_rng| {
hl_rng.start >= start + grapheme_byte_range.start
&& hl_rng.end <= start + grapheme_byte_range.end
}) {
(grapheme, style.patch(highlight_style))
} else {
(grapheme, style)
Expand Down
13 changes: 13 additions & 0 deletions helix-tui/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ impl<'a> From<Vec<Spans<'a>>> for Text<'a> {
}
}

impl<'a> From<Text<'a>> for String {
fn from(text: Text<'a>) -> String {
let lines: Vec<String> = text.lines.iter().map(String::from).collect();
lines.join("\n")
}
}

impl<'a> From<&Text<'a>> for String {
fn from(text: &Text<'a>) -> String {
let lines: Vec<String> = text.lines.iter().map(String::from).collect();
lines.join("\n")
}
}
impl<'a> IntoIterator for Text<'a> {
type Item = Spans<'a>;
type IntoIter = std::vec::IntoIter<Self::Item>;
Expand Down
8 changes: 8 additions & 0 deletions helix-tui/src/widgets/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ impl<'a> Row<'a> {
fn total_height(&self) -> u16 {
self.height.saturating_add(self.bottom_margin)
}

/// Returns the contents of cells as plain text, without styles and colors.
pub fn cell_text(&self) -> Vec<String> {
self.cells
.iter()
.map(|cell| String::from(&cell.content))
.collect()
}
}

/// A widget to display data in formatted columns.
Expand Down

0 comments on commit 7d53b58

Please sign in to comment.