@@ -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 }