aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/history.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-11-21 11:06:45 +0000
committerBlaž Hrastnik2021-11-21 11:06:45 +0000
commitd1854d8e6af07cd78ab6c24c859a4471afb3514e (patch)
tree301e4212e7fc88dd5f626f884bd78b700cf3e4a6 /helix-core/src/history.rs
parent8b85903116fdfdc177bf2ca171831674144de70a (diff)
parentb95c9470de9f9199f109fdbfb6ec9a951fbe8866 (diff)
Merge remote-tracking branch 'origin/master' into debug
Diffstat (limited to 'helix-core/src/history.rs')
-rw-r--r--helix-core/src/history.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs
index b53c01fe..4b1c8d3b 100644
--- a/helix-core/src/history.rs
+++ b/helix-core/src/history.rs
@@ -1,4 +1,4 @@
-use crate::{ChangeSet, Rope, State, Transaction};
+use crate::{Assoc, ChangeSet, Range, Rope, State, Transaction};
use once_cell::sync::Lazy;
use regex::Regex;
use std::num::NonZeroUsize;
@@ -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> {
+ 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)| 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();
@@ -256,7 +282,7 @@ impl History {
}
/// Whether to undo by a number of edits or a duration of time.
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone, Copy)]
pub enum UndoKind {
Steps(usize),
TimePeriod(std::time::Duration),