From 9fe9c0f99c109a39588d9fe20a3bd80fae05acf1 Mon Sep 17 00:00:00 2001 From: ath3 Date: Thu, 11 Nov 2021 17:14:26 +0100 Subject: [PATCH] Implement "Goto last modification" command --- book/src/keymap.md | 1 + helix-core/src/history.rs | 26 ++++++++++++++++++++++++++ helix-term/src/commands.rs | 14 ++++++++++++++ helix-term/src/keymap.rs | 1 + helix-term/src/ui/editor.rs | 2 +- helix-view/src/document.rs | 5 +++++ 6 files changed, 48 insertions(+), 1 deletion(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 901c84711c72d..1da59cccf6e60 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -162,6 +162,7 @@ active language server for the file to work. | `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` | | `n` | Go to next buffer | `goto_next_buffer` | | `p` | Go to previous buffer | `goto_previous_buffer` | +| `.` | Go to last modification in current file | `goto_last_modification` | #### Match mode diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index b53c01fe759df..8359baa9048b6 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -133,6 +133,32 @@ impl History { Some(&self.revisions[last_child.get()].transaction) } + // Get the position of last change + pub fn last_edit_pos(&self) -> Option { + let current_revision = &self.revisions[self.current]; + let mut primary_selection = 0; + if let Some(tsel) = current_revision.transaction.selection() { + if let Some(isel) = current_revision.inversion.selection() { + if isel.len() <= tsel.len() { + primary_selection = isel.primary_index(); + } + } + } + if let Some((pos, _, _)) = ¤t_revision + .transaction + .changes_iter() + .nth(primary_selection) + { + let mpos = current_revision + .transaction + .changes() + .map_pos(*pos, crate::Assoc::After); + Some(mpos) + } 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 738621b0466f8..6d985e752c8a2 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -259,6 +259,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", @@ -3196,6 +3197,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().unwrap().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 b2b865e434c57..547f3817412cd 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -509,6 +509,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-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 90f09e9c9db15..5f53ca58e5f4a 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -741,7 +741,7 @@ impl EditorView { std::num::NonZeroUsize::new(cxt.editor.count.map_or(i, |c| c.get() * 10 + i)); } // special handling for repeat operator - key!('.') => { + key!('.') if self.keymaps.pending().is_empty() => { // first execute whatever put us into insert mode self.last_insert.0.execute(cxt); // then replay the inputs diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 80f6a740f81aa..8bd5a69566629 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -782,6 +782,11 @@ impl Document { success } + // Get reference to history + pub fn history(&self) -> Option<&History> { + unsafe { self.history.as_ptr().as_ref() } + } + /// Commit pending changes to history pub fn append_changes_to_history(&mut self, view_id: ViewId) { if self.changes.is_empty() {