diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index b53c01fe759df..4fc9bd3e58165 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -133,6 +133,18 @@ impl History { Some(&self.revisions[last_child.get()].transaction) } + pub fn last_edit_pos(&mut self) -> Option { + let current_revision = &self.revisions[self.current]; + if let Some(op) = ¤t_revision.transaction.changes().changes.first() { + if let crate::Operation::Retain(pos) = op { + return Some(*pos); + } + Some(0) + } else { + None + } + } + fn lowest_common_ancestor(&self, mut a: usize, mut b: usize) -> usize { use std::collections::HashSet; let mut a_path_set = HashSet::new(); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 489308d878dfb..ced4ec613dd3d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -257,6 +257,7 @@ impl Command { goto_window_middle, "Goto window middle", goto_window_bottom, "Goto window bottom", goto_last_accessed_file, "Goto last accessed file", + goto_last_modification, "Goto last modification", goto_line, "Goto line", goto_last_line, "Goto last line", goto_first_diag, "Goto first diagnostic", @@ -3187,6 +3188,19 @@ fn goto_last_accessed_file(cx: &mut Context) { } } +fn goto_last_modification(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let pos = doc.history.get_mut().last_edit_pos(); + let text = doc.text().slice(..); + if let Some(pos) = pos { + let selection = doc + .selection(view.id) + .clone() + .transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select)); + doc.set_selection(view.id, selection); + } +} + fn select_mode(cx: &mut Context) { let (view, doc) = current!(cx.editor); let text = doc.text().slice(..); diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 7bed3ddb0b82b..ffe30ba423e59 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -488,6 +488,7 @@ impl Default for Keymaps { "a" => goto_last_accessed_file, "n" => goto_next_buffer, "p" => goto_previous_buffer, + ";" => goto_last_modification, }, ":" => command_mode, diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 351ad05a262cc..d56d5123f4a4e 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -96,7 +96,7 @@ pub struct Document { // It can be used as a cell where we will take it out to get some parts of the history and put // it back as it separated from the edits. We could split out the parts manually but that will // be more troublesome. - history: Cell, + pub history: Cell, pub savepoint: Option,