Merge pull request #381 from ace6807/fix/undo-after-revert-all
fix(322): fix undo/redo after reverting all changes
This commit is contained in:
commit
7d279a1d8b
1 changed files with 18 additions and 3 deletions
21
src/tab.rs
21
src/tab.rs
|
|
@ -128,11 +128,26 @@ impl EditorTab {
|
||||||
let scroll = editor.with_buffer(|buffer| buffer.scroll());
|
let scroll = editor.with_buffer(|buffer| buffer.scroll());
|
||||||
//TODO: save/restore more?
|
//TODO: save/restore more?
|
||||||
|
|
||||||
match editor.load_text(path, self.attrs.clone()) {
|
match std::fs::read_to_string(path) {
|
||||||
Ok(()) => {
|
Ok(file_content) => {
|
||||||
log::info!("reloaded {:?}", path);
|
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);
|
editor.set_changed(false);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue