Add line ending abstraction (#250)

* Add line ending abstraction

* Make Buffer::set_text use LineIter

* Add ctrl+s for saving to editor
This commit is contained in:
Jeremy Soller 2024-04-30 12:12:25 -06:00 committed by GitHub
parent ff5501d9a3
commit 0cfd9b64ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 194 additions and 15 deletions

View file

@ -7,8 +7,8 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::{
Affinity, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color, Cursor,
FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, Motion, Scroll, ShapeBuffer, ShapeLine,
Shaping, Wrap,
FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion, Scroll,
ShapeBuffer, ShapeLine, Shaping, Wrap,
};
/// A line of visible text for rendering
@ -607,7 +607,17 @@ impl Buffer {
attrs: Attrs,
shaping: Shaping,
) {
self.set_rich_text(font_system, [(text, attrs)], attrs, shaping);
self.lines.clear();
for (range, ending) in LineIter::new(text) {
self.lines.push(BufferLine::new(
&text[range],
ending,
AttrsList::new(attrs),
shaping,
));
}
self.scroll = Scroll::default();
self.shape_until_scroll(font_system, false);
}
/// Set text of buffer, using an iterator of styled spans (pairs of text and attributes)
@ -661,12 +671,15 @@ impl Buffer {
start..end
});
let mut maybe_line = lines_iter.next();
//TODO: set this based on information from spans
let line_ending = LineEnding::default();
loop {
let (Some(line_range), Some((attrs, span_range))) = (&maybe_line, &maybe_span) else {
// this is reached only if this text is empty
self.lines.push(BufferLine::new(
String::new(),
line_ending,
AttrsList::new(default_attrs),
shaping,
));
@ -701,11 +714,13 @@ impl Buffer {
let prev_attrs_list =
core::mem::replace(&mut attrs_list, AttrsList::new(default_attrs));
let prev_line_string = core::mem::take(&mut line_string);
let buffer_line = BufferLine::new(prev_line_string, prev_attrs_list, shaping);
let buffer_line =
BufferLine::new(prev_line_string, line_ending, prev_attrs_list, shaping);
self.lines.push(buffer_line);
} else {
// finalize the final line
let buffer_line = BufferLine::new(line_string, attrs_list, shaping);
let buffer_line =
BufferLine::new(line_string, line_ending, attrs_list, shaping);
self.lines.push(buffer_line);
break;
}