diff --git a/src/state.rs b/src/state.rs index cc1a0cd..189da4c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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)?; diff --git a/src/ui/doc.rs b/src/ui/doc.rs index 8e947fe..b6e30e5 100644 --- a/src/ui/doc.rs +++ b/src/ui/doc.rs @@ -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()