on master help sign in
fix: 2 panics while setting style + off by 1

The panics would occur because set_style would draw outside of the the surface. Both occured using `find_prev` or `till_prev` In my case the first panic! would appear in a terminal with around 80 columns in helix/README.md going to the end of the file with `geglf(` the second with `geglfX` The off by one fix ensures that `find_nth_prev` starts at the first character to the left

commite2d780f993201a79261bc6bd869c8b4f1dc410ce authorahkrr <alexhk@protonmail.com> dateSun 6 Jun 2021, 21:32 (5 years ago) parent843c2cd
2 files changed+16−3
helix-core/src/search.rs−1 helix-term/src/ui/editor.rs+16−2
helix-core/src/search.rs−1
@@ -41,7 +41,6 @@ pub fn find_nth_prev(
4141 inclusive: bool,
4242) -> Option<usize> {
4343 // start searching right before pos
44− pos = pos.saturating_sub(1);
4544 let mut chars = text.chars_at(pos);
4645
4746 for _ in 0..n {
helix-term/src/ui/editor.rs+16−2
@@ -261,7 +261,16 @@ impl EditorView {
261261 Rect::new(
262262 viewport.x + start.col as u16,
263263 viewport.y + start.row as u16,
264− ((end.col - start.col) as u16 + 1).min(viewport.width),
264+ // .min is important, because set_style does a
265+ // for i in area.left()..area.right() and
266+ // area.right = x + width !!! which shouldn't be > then surface.area.right()
267+ // This is checked by a debug_assert! in Buffer::index_of
268+ ((end.col - start.col) as u16 + 1).min(
269+ surface
270+ .area
271+ .width
272+ .saturating_sub(viewport.x + start.col as u16),
273+ ),
265274 1,
266275 ),
267276 selection_style,
@@ -290,7 +299,12 @@ impl EditorView {
290299 );
291300 }
292301 surface.set_style(
293− Rect::new(viewport.x, viewport.y + end.row as u16, end.col as u16, 1),
302+ Rect::new(
303+ viewport.x,
304+ viewport.y + end.row as u16,
305+ (end.col as u16).min(viewport.width),
306+ 1,
307+ ),
294308 selection_style,
295309 );
296310 }