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.
This commit is contained in:
parent
786e274dd1
commit
231ef51365
|
@ -148,13 +148,17 @@ impl<T: Copy + Clone> Grid<T> {
|
||||||
|
|
||||||
pub fn buffer_to_visible(&self, point: Point<usize>) -> Point {
|
pub fn buffer_to_visible(&self, point: Point<usize>) -> 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
|
col: point.col
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn buffer_line_to_visible(&self, line: usize) -> Line {
|
pub fn buffer_line_to_visible(&self, line: usize) -> Option<Line> {
|
||||||
self.offset_to_line(line - self.display_offset)
|
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 {
|
pub fn visible_line_to_buffer(&self, line: Line) -> usize {
|
||||||
|
@ -267,10 +271,12 @@ impl<T: Copy + Clone> Grid<T> {
|
||||||
*(self.num_lines() - line - 1)
|
*(self.num_lines() - line - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn offset_to_line(&self, offset: usize) -> Line {
|
pub fn offset_to_line(&self, offset: usize) -> Option<Line> {
|
||||||
assert!(offset < *self.num_lines());
|
if offset < *self.num_lines() {
|
||||||
|
Some(self.lines - offset - 1)
|
||||||
self.lines - offset - 1
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -122,29 +122,49 @@ impl<'a> RenderableCellsIter<'a> {
|
||||||
let cursor_offset = grid.line_to_offset(cursor.line);
|
let cursor_offset = grid.line_to_offset(cursor.line);
|
||||||
let inner = grid.display_iter();
|
let inner = grid.display_iter();
|
||||||
|
|
||||||
let selection = selection.map(|loc| {
|
let mut selection_range = None;
|
||||||
// start and end *lines* are swapped as we switch from buffer to
|
selection.map(|loc| {
|
||||||
// Line coordinates.
|
// Get on-screen lines of the selection's locations
|
||||||
let mut end = Point {
|
let start_line = grid.buffer_line_to_visible(loc.start.line);
|
||||||
line: grid.buffer_line_to_visible(loc.start.line),
|
let end_line = grid.buffer_line_to_visible(loc.end.line);
|
||||||
col: loc.start.col
|
|
||||||
};
|
// Get start/end locations based on what part of selection is on screen
|
||||||
let mut start = Point {
|
let locations = match (start_line, end_line) {
|
||||||
line: grid.buffer_line_to_visible(loc.end.line),
|
(Some(start_line), Some(end_line)) => {
|
||||||
col: loc.end.col
|
Some((start_line, loc.start.col, end_line, loc.end.col))
|
||||||
|
},
|
||||||
|
(Some(start_line), None) => {
|
||||||
|
Some((start_line, loc.start.col, Line(0), grid.num_cols()))
|
||||||
|
},
|
||||||
|
(None, Some(end_line)) => {
|
||||||
|
Some((grid.num_lines(), Column(0), end_line, loc.end.col))
|
||||||
|
},
|
||||||
|
(None, None) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if start > end {
|
if let Some((start_line, start_col, end_line, end_col)) = locations {
|
||||||
::std::mem::swap(&mut start, &mut end);
|
// start and end *lines* are swapped as we switch from buffer to
|
||||||
|
// Line coordinates.
|
||||||
|
let mut end = Point {
|
||||||
|
line: start_line,
|
||||||
|
col: start_col,
|
||||||
|
};
|
||||||
|
let mut start = Point {
|
||||||
|
line: end_line,
|
||||||
|
col: end_col,
|
||||||
|
};
|
||||||
|
|
||||||
|
if start > end {
|
||||||
|
::std::mem::swap(&mut start, &mut end);
|
||||||
|
}
|
||||||
|
|
||||||
|
let cols = grid.num_cols();
|
||||||
|
let start = Linear(start.line.0 * cols.0 + start.col.0);
|
||||||
|
let end = Linear(end.line.0 * cols.0 + end.col.0);
|
||||||
|
|
||||||
|
// Update the selection
|
||||||
|
selection_range = Some(RangeInclusive::new(start, end));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("start={:?}, end={:?}", start, end);
|
|
||||||
|
|
||||||
let cols = grid.num_cols();
|
|
||||||
let start = Linear(start.line.0 * cols.0 + start.col.0);
|
|
||||||
let end = Linear(end.line.0 * cols.0 + end.col.0);
|
|
||||||
|
|
||||||
RangeInclusive::new(start, end)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RenderableCellsIter {
|
RenderableCellsIter {
|
||||||
|
@ -153,7 +173,7 @@ impl<'a> RenderableCellsIter<'a> {
|
||||||
grid: grid,
|
grid: grid,
|
||||||
inner: inner,
|
inner: inner,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
selection: selection,
|
selection: selection_range,
|
||||||
config: config,
|
config: config,
|
||||||
colors: colors,
|
colors: colors,
|
||||||
cursor_cells: ArrayDeque::new(),
|
cursor_cells: ArrayDeque::new(),
|
||||||
|
|
Loading…
Reference in New Issue