Editor::insert_at: ensure there are enough lines in the buffer

This commit is contained in:
Jeremy Soller 2024-01-10 09:24:36 -07:00
parent 40e503737b
commit 054b7da828

View file

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