on master help sign in
Simplify changeset tracking.
commit490e23b645311fef86d8e8045f97f514927d2867 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 7 Oct 2020, 22:22 (6 years ago) parent518426b
3 files changed+40−19
helix-core/src/state.rs+5−2 helix-core/src/transaction.rs+19−5 helix-view/src/commands.rs+16−12
helix-core/src/state.rs+5−2
@@ -25,7 +25,8 @@ pub struct State {
2525
2626 //
2727 pub syntax: Option<Syntax>,
28− pub changes: Option<ChangeSet>,
28+ /// Pending changes since last history commit.
29+ pub changes: ChangeSet,
2930 pub old_state: Option<(Rope, Selection)>,
3031}
3132
@@ -45,6 +46,8 @@ pub enum Granularity {
4546impl State {
4647 #[must_use]
4748 pub fn new(doc: Rope) -> Self {
49+ let changes = ChangeSet::new(&doc);
50+
4851 Self {
4952 path: None,
5053 doc,
@@ -52,7 +55,7 @@ impl State {
5255 mode: Mode::Normal,
5356 restore_cursor: false,
5457 syntax: None,
55− changes: None,
58+ changes,
5659 old_state: None,
5760 }
5861 }
helix-core/src/transaction.rs+19−5
@@ -342,6 +342,22 @@ pub struct Transaction {
342342 // scroll_into_view
343343}
344344
345+/// Like std::mem::replace() except it allows the replacement value to be mapped from the
346+/// original value.
347+pub fn take_with<T, F>(mut_ref: &mut T, closure: F)
348+where
349+ F: FnOnce(T) -> T,
350+{
351+ use std::{panic, ptr};
352+
353+ unsafe {
354+ let old_t = ptr::read(mut_ref);
355+ let new_t = panic::catch_unwind(panic::AssertUnwindSafe(|| closure(old_t)))
356+ .unwrap_or_else(|_| ::std::process::abort());
357+ ptr::write(mut_ref, new_t);
358+ }
359+}
360+
345361impl Transaction {
346362 /// Create a new, empty transaction.
347363 pub fn new(state: &mut State) -> Self {
@@ -364,11 +380,9 @@ impl Transaction {
364380 }
365381
366382 // Compose this transaction with the previous one
367− let old_changes = state.changes.take();
368− state.changes = Some(old_changes.map_or_else(
369− || self.changes.clone(),
370− |changes| changes.compose(self.changes.clone()).unwrap(),
371− ));
383+ take_with(&mut state.changes, |changes| {
384+ changes.compose(self.changes.clone()).unwrap()
385+ });
372386
373387 if let Some(syntax) = &mut state.syntax {
374388 // TODO: no unwrap
helix-view/src/commands.rs+16−12
@@ -368,22 +368,26 @@ pub fn open_below(view: &mut View, _count: usize) {
368368// O inserts a new line before each line with a selection
369369
370370fn append_changes_to_history(view: &mut View) {
371− if let Some(changes) = view.state.changes.take() {
372− // Instead of doing this messy merge we could always commit, and based on transaction
373− // annotations either add a new layer or compose into the previous one.
374− let transaction = Transaction::from(changes).with_selection(view.state.selection().clone());
375− let (doc, selection) = view.state.old_state.take().unwrap();
376− let mut old_state = State::new(doc);
377− old_state.selection = selection;
378−
379− // TODO: take transaction by value?
380− view.history.commit_revision(&transaction, &old_state);
371+ if view.state.changes.is_empty() {
372+ return;
381373 }
382374
383− // TODO: need to start the state with these vals
375+ let new_changeset = ChangeSet::new(view.state.doc());
376+ let changes = std::mem::replace(&mut view.state.changes, new_changeset);
377+ // Instead of doing this messy merge we could always commit, and based on transaction
378+ // annotations either add a new layer or compose into the previous one.
379+ let transaction = Transaction::from(changes).with_selection(view.state.selection().clone());
380+
381+ // HAXX: we need to reconstruct the state as it was before the changes..
382+ let (doc, selection) = view.state.old_state.take().unwrap();
383+ let mut old_state = State::new(doc);
384+ old_state.selection = selection;
384385
386+ // TODO: take transaction by value?
387+ view.history.commit_revision(&transaction, &old_state);
388+
389+ // TODO: need to start the state with these vals
385390 // HAXX
386− view.state.changes = Some(ChangeSet::new(view.state.doc()));
387391 view.state.old_state = Some((view.state.doc().clone(), view.state.selection.clone()));
388392}
389393