diff --git a/src/edit/editor.rs b/src/edit/editor.rs index f86fd4d..ae68e9e 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -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)); diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 6fc041e..d348a83 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -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(()) }