From fce67e6f3f8495f7ba029c55f2e319626cee56ca Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 10 Mar 2018 14:53:54 +0100 Subject: Fix crash when selection leaves viewport There was an issue where alacritty tries to convert the lines in a selection to the on-screen lines even when the selection is not on the screen. This results in a crash. To prevent this from happening the selection now is not shown if it is off the screen. There currently still is a bug that when the selection is at the top of the screen but still half visible, it will not show the top line as selected but start in the second line. This bug should be resolved with https://github.com/jwilm/alacritty/pull/1171. This fixes #1148. --- src/grid/mod.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/grid') diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 7f648f46..a52c27c5 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -148,13 +148,17 @@ impl Grid { pub fn buffer_to_visible(&self, point: Point) -> Point { Point { - line: self.buffer_line_to_visible(point.line), + line: self.buffer_line_to_visible(point.line).expect("Line not visible"), col: point.col } } - pub fn buffer_line_to_visible(&self, line: usize) -> Line { - self.offset_to_line(line - self.display_offset) + pub fn buffer_line_to_visible(&self, line: usize) -> Option { + if line >= self.display_offset { + self.offset_to_line(line - self.display_offset) + } else { + None + } } pub fn visible_line_to_buffer(&self, line: Line) -> usize { @@ -267,10 +271,12 @@ impl Grid { *(self.num_lines() - line - 1) } - pub fn offset_to_line(&self, offset: usize) -> Line { - assert!(offset < *self.num_lines()); - - self.lines - offset - 1 + pub fn offset_to_line(&self, offset: usize) -> Option { + if offset < *self.num_lines() { + Some(self.lines - offset - 1) + } else { + None + } } #[inline] -- cgit v1.2.3-54-g00ecf