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 10, 2021
1 parent e0540fb commit 9dd941a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ impl History {
Some(&self.revisions[last_child.get()].transaction)
}

pub fn last_edit_pos(&mut self) -> Option<usize> {
let current_revision = &self.revisions[self.current];
if let Some(op) = &current_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();
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 @@ -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",
Expand Down Expand Up @@ -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(..);
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 @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<History>,
pub history: Cell<History>,

pub savepoint: Option<Transaction>,

Expand Down

0 comments on commit 9dd941a

Please sign in to comment.