From 054b7da8280457550fa7cd8015df4ba35ecc2885 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 10 Jan 2024 09:24:36 -0700 Subject: [PATCH] Editor::insert_at: ensure there are enough lines in the buffer --- src/edit/editor.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index be475f6..d1f8a88 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -8,8 +8,8 @@ use unicode_segmentation::UnicodeSegmentation; #[cfg(feature = "swash")] use crate::Color; use crate::{ - Action, AttrsList, BorrowedWithFontSystem, BufferLine, BufferRef, Change, ChangeItem, Cursor, - Edit, FontSystem, Selection, Shaping, + Action, Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, BufferRef, Change, ChangeItem, + Cursor, Edit, FontSystem, Selection, Shaping, }; /// A wrapper of [`Buffer`] for easy editing @@ -357,6 +357,24 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> { // Save cursor for change tracking let start = cursor; + // Ensure there are enough lines in the buffer to handle this cursor + while cursor.line >= buffer.lines.len() { + let line = BufferLine::new( + String::new(), + AttrsList::new(attrs_list.as_ref().map_or_else( + || { + buffer + .lines + .last() + .map_or(Attrs::new(), |line| line.attrs_list().defaults()) + }, + |x| x.defaults(), + )), + Shaping::Advanced, + ); + buffer.lines.push(line); + } + let line: &mut BufferLine = &mut buffer.lines[cursor.line]; let insert_line = cursor.line + 1;