mirror of
https://github.com/helix-editor/helix.git
synced 2024-09-11 18:58:00 +03:00
Implement "Goto last modification" command (#1067)
This commit is contained in:
parent
0949a0de7f
commit
35c974c9c4
@ -165,6 +165,7 @@ #### Goto mode
|
||||
| `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
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ChangeSet, Rope, State, Transaction};
|
||||
use crate::{Assoc, ChangeSet, Rope, State, Transaction};
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use std::num::NonZeroUsize;
|
||||
@ -133,6 +133,34 @@ pub fn redo(&mut self) -> Option<&Transaction> {
|
||||
Some(&self.revisions[last_child.get()].transaction)
|
||||
}
|
||||
|
||||
// Get the position of last change
|
||||
pub fn last_edit_pos(&self) -> Option<usize> {
|
||||
if self.current == 0 {
|
||||
return None;
|
||||
}
|
||||
let current_revision = &self.revisions[self.current];
|
||||
let primary_selection = current_revision
|
||||
.inversion
|
||||
.selection()
|
||||
.expect("inversion always contains a selection")
|
||||
.primary();
|
||||
let (_from, to, _fragment) = current_revision
|
||||
.transaction
|
||||
.changes_iter()
|
||||
// find a change that matches the primary selection
|
||||
.find(|(from, to, _fragment)| {
|
||||
crate::Range::new(*from, *to).overlaps(&primary_selection)
|
||||
})
|
||||
// or use the first change
|
||||
.or_else(|| current_revision.transaction.changes_iter().next())
|
||||
.unwrap();
|
||||
let pos = current_revision
|
||||
.transaction
|
||||
.changes()
|
||||
.map_pos(to, Assoc::After);
|
||||
Some(pos)
|
||||
}
|
||||
|
||||
fn lowest_common_ancestor(&self, mut a: usize, mut b: usize) -> usize {
|
||||
use std::collections::HashSet;
|
||||
let mut a_path_set = HashSet::new();
|
||||
|
@ -257,6 +257,7 @@ pub fn doc(&self) -> &'static str {
|
||||
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",
|
||||
@ -3195,6 +3196,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(..);
|
||||
|
@ -525,6 +525,7 @@ fn default() -> Keymaps {
|
||||
"a" => goto_last_accessed_file,
|
||||
"n" => goto_next_buffer,
|
||||
"p" => goto_previous_buffer,
|
||||
"." => goto_last_modification,
|
||||
},
|
||||
":" => command_mode,
|
||||
|
||||
|
@ -743,7 +743,7 @@ fn command_mode(&mut self, mode: Mode, cxt: &mut commands::Context, event: KeyEv
|
||||
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
|
||||
|
@ -98,7 +98,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>,
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user