diff --git a/src/tab.rs b/src/tab.rs index 1f4c618..b5ac5ff 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -128,11 +128,26 @@ impl EditorTab { let scroll = editor.with_buffer(|buffer| buffer.scroll()); //TODO: save/restore more? - match editor.load_text(path, self.attrs.clone()) { - Ok(()) => { + match std::fs::read_to_string(path) { + Ok(file_content) => { log::info!("reloaded {:?}", path); - // Clear changed state + // Store the entire operation as a single change for undo + editor.start_change(); + + // Grab everything in the buffer + let cursor_start: Cursor = cosmic_text::Cursor::new(0, 0); + let cursor_end = editor.with_buffer(|buffer| { + let last_line = buffer.lines.len().saturating_sub(1); + cosmic_text::Cursor::new(last_line, buffer.lines.get(last_line).map(|line| line.text().len()).unwrap_or(0)) + }); + + // Replace everything in the buffer with the content from disk + editor.delete_range(cursor_start, cursor_end); + editor.insert_at(cursor_start, &file_content, None); + editor.set_cursor(cursor_start); + + editor.finish_change(); editor.set_changed(false); } Err(err) => {