Improve handling of non-existant files in load_text

This commit is contained in:
Jeremy Soller 2025-10-14 09:55:48 -06:00
parent 646e04389e
commit d409ddd1d3
No known key found for this signature in database
GPG key ID: 670FDFB5428E05CA
2 changed files with 10 additions and 4 deletions

View file

@ -419,7 +419,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
});
// Append the inserted text, line by line
let mut lines: Vec<_> = LineIter::new(&data).collect();
let mut lines: Vec<_> = LineIter::new(data).collect();
// Ensure there is always an ending line with no line ending
if lines.last().map(|line| line.1).unwrap_or(LineEnding::None) != LineEnding::None {
lines.push((Default::default(), LineEnding::None));

View file

@ -123,12 +123,12 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
));
}
let text = fs::read_to_string(path)?;
// Clear buffer first (allows sane handling of non-existant files)
self.editor.with_buffer_mut(|buffer| {
buffer.set_text(font_system, &text, &attrs, Shaping::Advanced, None);
buffer.set_text(font_system, "", &attrs, Shaping::Advanced, None);
});
//TODO: re-use text
// Update syntax based on file name
self.syntax = match self.syntax_system.syntax_set.find_syntax_for_file(path) {
Ok(Some(some)) => some,
Ok(None) => {
@ -144,6 +144,12 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
// Clear syntax cache
self.syntax_cache.clear();
// Set text
let text = fs::read_to_string(path)?;
self.editor.with_buffer_mut(|buffer| {
buffer.set_text(font_system, &text, &attrs, Shaping::Advanced, None);
});
Ok(())
}