From 579d66c3d9c8dd0e60bc0e7c5c2c4719c20cdd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Sat, 30 Mar 2024 03:14:45 +0000 Subject: [PATCH] add working undo after paste --- src/app.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/app.rs b/src/app.rs index 103c802..284430f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -24,6 +24,7 @@ enum EditAction { InsertAndDelete { inserted_text: String, deleted_text: String, + selection_after_delete: CCursorRange, selection_before: CCursorRange, }, } @@ -457,8 +458,19 @@ impl eframe::App for TemplateApp { } Event::Paste(text_to_insert) => { if !text_to_insert.is_empty() { + let deleted_text = + self.selection.slice_str(&self.text).to_owned(); + let mut ccursor = self.text.delete_selected(&self.selection); + // Push edit action onto the edit history stack. + self.history.push(EditAction::InsertAndDelete { + inserted_text: text_to_insert.to_owned(), + deleted_text, + selection_after_delete: CCursorRange::one(ccursor), + selection_before: self.selection.as_ccursor_range(), + }); + self.text.insert_text_at( &mut ccursor, text_to_insert, @@ -475,17 +487,19 @@ impl eframe::App for TemplateApp { && text_to_insert != "\n" && text_to_insert != "\r" { + let deleted_text = + self.selection.slice_str(&self.text).to_owned(); + + let mut ccursor = self.text.delete_selected(&self.selection); + // Push edit action onto the edit history stack. self.history.push(EditAction::InsertAndDelete { inserted_text: text_to_insert.to_owned(), - deleted_text: self - .selection - .slice_str(&self.text) - .to_owned(), + deleted_text, + selection_after_delete: CCursorRange::one(ccursor), selection_before: self.selection.as_ccursor_range(), }); - let mut ccursor = self.text.delete_selected(&self.selection); self.text.insert_text_at( &mut ccursor, text_to_insert, @@ -502,13 +516,17 @@ impl eframe::App for TemplateApp { pressed: true, .. } => { + let deleted_text = self.selection.slice_str(&self.text).to_owned(); + + let mut ccursor = self.text.delete_selected(&self.selection); + self.history.push(EditAction::InsertAndDelete { inserted_text: "\t".to_owned(), - deleted_text: self.selection.slice_str(&self.text).to_owned(), + deleted_text, + selection_after_delete: CCursorRange::one(ccursor), selection_before: self.selection.as_ccursor_range(), }); - let mut ccursor = self.text.delete_selected(&self.selection); self.text.insert_text_at(&mut ccursor, "\t", usize::MAX); Some(CCursorRange::one(ccursor)) @@ -518,13 +536,17 @@ impl eframe::App for TemplateApp { pressed: true, .. } => { + let deleted_text = self.selection.slice_str(&self.text).to_owned(); + + let mut ccursor = self.text.delete_selected(&self.selection); + self.history.push(EditAction::InsertAndDelete { inserted_text: "\n".to_owned(), - deleted_text: self.selection.slice_str(&self.text).to_owned(), + deleted_text, + selection_after_delete: CCursorRange::one(ccursor), selection_before: self.selection.as_ccursor_range(), }); - let mut ccursor = self.text.delete_selected(&self.selection); self.text.insert_text_at(&mut ccursor, "\n", usize::MAX); Some(CCursorRange::one(ccursor)) @@ -709,20 +731,21 @@ impl eframe::App for TemplateApp { Some(EditAction::InsertAndDelete { inserted_text, deleted_text, + selection_after_delete, selection_before, }) => { // Delete the inserted text. - self.text.delete_selected(&CursorRange::two( - galley.from_ccursor(selection_before.primary), + let mut ccursor = self.text.delete_selected(&CursorRange::two( + galley.from_ccursor(selection_after_delete.primary), galley.from_ccursor( - selection_before.primary + inserted_text.len(), + selection_after_delete.primary + inserted_text.len(), ), )); // Insert deleted text back. if !deleted_text.is_empty() { self.text.insert_text_at( - &mut selection_before.primary.clone(), + &mut ccursor, &deleted_text, usize::MAX, );