Don't panic on empty document

This commit is contained in:
Kevin Sjöberg 2021-06-08 21:26:28 +02:00 committed by Blaž Hrastnik
parent 35feb614b6
commit 7ef0e2cab6

View File

@ -149,7 +149,7 @@ pub fn load(path: PathBuf) -> Result<Self, Error> {
let file = File::open(&path).context(format!("unable to open {:?}", path))?; let file = File::open(&path).context(format!("unable to open {:?}", path))?;
let mut doc = Rope::from_reader(BufReader::new(file))?; let mut doc = Rope::from_reader(BufReader::new(file))?;
// add missing newline at the end of file // add missing newline at the end of file
if doc.byte(doc.len_bytes() - 1) != b'\n' { if doc.len_bytes() == 0 || doc.byte(doc.len_bytes() - 1) != b'\n' {
doc.insert_char(doc.len_chars(), '\n'); doc.insert_char(doc.len_chars(), '\n');
} }
doc doc