Skip to content

Commit

Permalink
Implement "Goto last modification" command
Browse files Browse the repository at this point in the history
  • Loading branch information
ath3 committed Nov 11, 2021
1 parent 9d59142 commit 9fe9c0f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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, _, _)) = &current_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();
Expand Down
14 changes: 14 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(..);
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9fe9c0f

Please sign in to comment.