Allow buffer text to be reset

This commit is contained in:
Jeremy Soller 2022-10-19 13:15:07 -06:00
parent b32c64e892
commit 0cbc3c3cfa
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
4 changed files with 58 additions and 21 deletions

View file

@ -114,16 +114,11 @@ pub struct TextBuffer<'a> {
impl<'a> TextBuffer<'a> {
pub fn new(
font_matches: &'a FontMatches<'a>,
text: &str,
metrics: TextMetrics,
) -> Self {
let mut text_lines: Vec<String> = text.lines().map(String::from).collect();
if text_lines.is_empty() {
text_lines.push(String::new());
}
Self {
font_matches,
text_lines,
text_lines: Vec::new(),
shape_lines: Vec::new(),
layout_lines: Vec::new(),
metrics,
@ -294,6 +289,20 @@ impl<'a> TextBuffer<'a> {
&self.layout_lines
}
/// Set text of buffer
pub fn set_text(&mut self, text: &str) {
self.text_lines = text.lines().map(String::from).collect();
if self.text_lines.is_empty() {
self.text_lines.push(String::new());
}
self.shape_lines.clear();
self.layout_lines.clear();
self.scroll = 0;
self.cursor = TextCursor::default();
self.select_opt = None;
self.shape_until_scroll();
}
/// Get the lines of the original text
pub fn text_lines(&self) -> &[String] {
&self.text_lines