From 22cb7b3338ba0f0c8f2eeb1dec762ec9b8173641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 13 Sep 2020 19:18:05 +0900 Subject: [PATCH] Change -> Operation, Change2 -> Change --- helix-core/src/selection.rs | 1 - helix-core/src/transaction.rs | 41 +++++++++++++++-------------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index f6ca424f5..2ae2d7c8d 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -105,7 +105,6 @@ pub struct Selection { } impl Selection { - // map // eq #[must_use] diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 84aaa64c3..7fbcbf6c6 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -1,8 +1,7 @@ use crate::{Rope, Selection, SelectionRange, State, Tendril}; -// TODO: make Change enum internal and export this as the public Change type /// (from, to, replacement) -type Change2 = (usize, usize, Option); +pub type Change = (usize, usize, Option); // TODO: divided into three different operations, I sort of like having just // Splice { extent, Option, distance } better. @@ -12,7 +11,7 @@ // unchanged?: Splice { extent: 0, text: None, distance: 2 } // harder to compose though. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Change { +enum Operation { /// Move cursor by n characters. Retain(usize), /// Delete n characters. @@ -21,12 +20,6 @@ pub enum Change { Insert(Tendril), } -// impl Change { -// pub fn new(from: usize, to: usize, insert: Option) { -// // old_extent, new_extent, insert -// } -// } - #[derive(Copy, Clone, PartialEq, Eq)] pub enum Assoc { Before, @@ -36,7 +29,7 @@ pub enum Assoc { // ChangeSpec = Change | ChangeSet | Vec #[derive(Debug)] pub struct ChangeSet { - changes: Vec, + changes: Vec, /// The required document length. Will refuse to apply changes unless it matches. len: usize, } @@ -46,7 +39,7 @@ impl ChangeSet { pub fn new(doc: &Rope) -> Self { let len = doc.len_chars(); Self { - changes: vec![Change::Retain(len)], + changes: vec![Operation::Retain(len)], len, } } @@ -70,11 +63,11 @@ pub fn compose(self, other: ChangeSet) -> Result { let mut head_a = changes_a.next(); let mut head_b = changes_b.next(); - let mut changes: Vec = Vec::with_capacity(len); // TODO: max(a, b), shrink_to_fit() afterwards + let mut changes: Vec = Vec::with_capacity(len); // TODO: max(a, b), shrink_to_fit() afterwards loop { use std::cmp::Ordering; - use Change::*; + use Operation::*; match (head_a, head_b) { // we are done (None, None) => { @@ -211,7 +204,7 @@ pub fn apply(&self, text: &mut Rope) -> bool { let mut pos = 0; for change in &self.changes { - use Change::*; + use Operation::*; match change { Retain(n) => { pos += n; @@ -233,7 +226,7 @@ pub fn apply(&self, text: &mut Rope) -> bool { #[inline] pub fn is_empty(&self) -> bool { let len = self.changes.len(); - len == 0 || (len == 1 && self.changes[0] == Change::Retain(self.len)) + len == 0 || (len == 1 && self.changes[0] == Operation::Retain(self.len)) } /// Map a position through the changes. @@ -243,7 +236,7 @@ pub fn is_empty(&self) -> bool { /// range, or at that point. `After` will move it forward, placing it at the end of such /// insertions. pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize { - use Change::*; + use Operation::*; let mut old_pos = 0; let mut new_pos = 0; @@ -348,7 +341,7 @@ pub fn apply(&self, state: &mut State) -> bool { /// Generate a transaction from a set of changes. // TODO: take an owned iter instead of Vec - pub fn change(state: &State, changes: Vec) -> Self { + pub fn change(state: &State, changes: Vec) -> Self { let len = state.doc.len_chars(); let mut acc = Vec::with_capacity(2 * changes.len() + 1); @@ -358,14 +351,14 @@ pub fn change(state: &State, changes: Vec) -> Self { for (from, to, tendril) in changes { // TODO: need to fill the in-between ranges too // Retain from last "to" to current "from" - acc.push(Change::Retain(from - last)); + acc.push(Operation::Retain(from - last)); match tendril { - Some(text) => acc.push(Change::Insert(text)), - None => acc.push(Change::Delete(to - from)), + Some(text) => acc.push(Operation::Insert(text)), + None => acc.push(Operation::Delete(to - from)), } last = to; } - acc.push(Change::Retain(len - last)); + acc.push(Operation::Retain(len - last)); Self::from(ChangeSet { changes: acc, len }) } @@ -373,7 +366,7 @@ pub fn change(state: &State, changes: Vec) -> Self { /// Generate a transaction with a change per selection range. pub fn change_by_selection(state: &State, f: F) -> Self where - F: Fn(&SelectionRange) -> Change2, + F: Fn(&SelectionRange) -> Change, { Self::change(state, state.selection.ranges.iter().map(f).collect()) } @@ -399,7 +392,7 @@ mod test { #[test] fn composition() { - use Change::*; + use Operation::*; let a = ChangeSet { changes: vec![ @@ -428,7 +421,7 @@ fn composition() { #[test] fn map_pos() { - use Change::*; + use Operation::*; // maps inserts let cs = ChangeSet {