Skip to content

Commit

Permalink
Clamp highlighting range to be within document
Browse files Browse the repository at this point in the history
This fixes a panic possible when two vsplits of the same document
exist and enough lines are deleted from the document so that one of
the windows focuses past the end of the document.
  • Loading branch information
the-mikedavis committed Nov 3, 2022
1 parent 1bed2f3 commit 41de475
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,16 @@ impl EditorView {
_theme: &Theme,
) -> Box<dyn Iterator<Item = HighlightEvent> + 'doc> {
let text = doc.text().slice(..);
let last_line = std::cmp::min(
// Saturating subs to make it inclusive zero indexing.
(offset.row + height as usize).saturating_sub(1),
doc.text().len_lines().saturating_sub(1),
);

let range = {
// calculate viewport byte ranges
let start = text.line_to_byte(offset.row);
let end = text.line_to_byte(last_line + 1);
// Calculate viewport byte ranges:
// Saturating subs to make it inclusive zero indexing.
let last_line = doc.text().len_lines().saturating_sub(1);
let last_visible_line = (offset.row + height as usize)
.saturating_sub(1)
.min(last_line);
let start = text.line_to_byte(offset.row.min(last_line));
let end = text.line_to_byte(last_visible_line.min(last_line) + 1);

start..end
};
Expand Down

0 comments on commit 41de475

Please sign in to comment.