Enforce trailing newline on save

This commit is contained in:
Joshua Barretto 2025-12-23 20:02:26 +00:00
parent 7a0838144c
commit 26af37dfd6
2 changed files with 12 additions and 0 deletions

View file

@ -224,6 +224,10 @@ impl Buffer {
}
pub fn save(&mut self) -> Result<(), Error> {
// Ensure trailing newline exists
if self.text.chars.last().map_or(false, |c| *c != '\n') {
self.insert(self.text.chars.len(), ['\n']);
}
let path = self.path.as_ref().expect("buffer must have path to save");
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;

View file

@ -149,6 +149,14 @@ impl Element for Doc {
)
.into(),
)
} else if buffer.path.is_none() {
Some(
Action::Show(
None,
"Error: buffer does not have a path (TODO: implement save_as)".to_string(),
)
.into(),
)
} else {
buffer.save().err().map(|err| {
Action::Show(Some("Could not save file".to_string()), err.to_string()).into()