on master help sign in
Change -> Operation, Change2 -> Change
commit22cb7b3338ba0f0c8f2eeb1dec762ec9b8173641 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 13 Sep 2020, 19:18 (6 years ago) parentd86f0fe
2 files changed+17−25
helix-core/src/selection.rs−1 helix-core/src/transaction.rs+17−24
helix-core/src/selection.rs−1
@@ -105,7 +105,6 @@ pub struct Selection {
105105}
106106
107107impl Selection {
108− // map
109108 // eq
110109
111110 #[must_use]
helix-core/src/transaction.rs+17−24
@@ -1,8 +1,7 @@
11use crate::{Rope, Selection, SelectionRange, State, Tendril};
22
3−// TODO: make Change enum internal and export this as the public Change type
43/// (from, to, replacement)
5−type Change2 = (usize, usize, Option<Tendril>);
4+pub type Change = (usize, usize, Option<Tendril>);
65
76// TODO: divided into three different operations, I sort of like having just
87// Splice { extent, Option<text>, distance } better.
@@ -12,7 +11,7 @@ type Change2 = (usize, usize, Option<Tendril>);
1211// unchanged?: Splice { extent: 0, text: None, distance: 2 }
1312// harder to compose though.
1413#[derive(Debug, Clone, PartialEq, Eq)]
15−pub enum Change {
14+enum Operation {
1615 /// Move cursor by n characters.
1716 Retain(usize),
1817 /// Delete n characters.
@@ -21,12 +20,6 @@ pub enum Change {
2120 Insert(Tendril),
2221}
2322
24−// impl Change {
25−// pub fn new(from: usize, to: usize, insert: Option<Tendril>) {
26−// // old_extent, new_extent, insert
27−// }
28−// }
29−
3023#[derive(Copy, Clone, PartialEq, Eq)]
3124pub enum Assoc {
3225 Before,
@@ -36,7 +29,7 @@ pub enum Assoc {
3629// ChangeSpec = Change | ChangeSet | Vec<Change>
3730#[derive(Debug)]
3831pub struct ChangeSet {
39− changes: Vec<Change>,
32+ changes: Vec<Operation>,
4033 /// The required document length. Will refuse to apply changes unless it matches.
4134 len: usize,
4235}
@@ -46,7 +39,7 @@ impl ChangeSet {
4639 pub fn new(doc: &Rope) -> Self {
4740 let len = doc.len_chars();
4841 Self {
49− changes: vec![Change::Retain(len)],
42+ changes: vec![Operation::Retain(len)],
5043 len,
5144 }
5245 }
@@ -70,11 +63,11 @@ impl ChangeSet {
7063 let mut head_a = changes_a.next();
7164 let mut head_b = changes_b.next();
7265
73− let mut changes: Vec<Change> = Vec::with_capacity(len); // TODO: max(a, b), shrink_to_fit() afterwards
66+ let mut changes: Vec<Operation> = Vec::with_capacity(len); // TODO: max(a, b), shrink_to_fit() afterwards
7467
7568 loop {
7669 use std::cmp::Ordering;
77− use Change::*;
70+ use Operation::*;
7871 match (head_a, head_b) {
7972 // we are done
8073 (None, None) => {
@@ -211,7 +204,7 @@ impl ChangeSet {
211204 let mut pos = 0;
212205
213206 for change in &self.changes {
214− use Change::*;
207+ use Operation::*;
215208 match change {
216209 Retain(n) => {
217210 pos += n;
@@ -233,7 +226,7 @@ impl ChangeSet {
233226 #[inline]
234227 pub fn is_empty(&self) -> bool {
235228 let len = self.changes.len();
236− len == 0 || (len == 1 && self.changes[0] == Change::Retain(self.len))
229+ len == 0 || (len == 1 && self.changes[0] == Operation::Retain(self.len))
237230 }
238231
239232 /// Map a position through the changes.
@@ -243,7 +236,7 @@ impl ChangeSet {
243236 /// range, or at that point. `After` will move it forward, placing it at the end of such
244237 /// insertions.
245238 pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize {
246− use Change::*;
239+ use Operation::*;
247240 let mut old_pos = 0;
248241 let mut new_pos = 0;
249242
@@ -348,7 +341,7 @@ impl Transaction {
348341
349342 /// Generate a transaction from a set of changes.
350343 // TODO: take an owned iter instead of Vec
351− pub fn change(state: &State, changes: Vec<Change2>) -> Self {
344+ pub fn change(state: &State, changes: Vec<Change>) -> Self {
352345 let len = state.doc.len_chars();
353346 let mut acc = Vec::with_capacity(2 * changes.len() + 1);
354347
@@ -358,14 +351,14 @@ impl Transaction {
358351 for (from, to, tendril) in changes {
359352 // TODO: need to fill the in-between ranges too
360353 // Retain from last "to" to current "from"
361− acc.push(Change::Retain(from - last));
354+ acc.push(Operation::Retain(from - last));
362355 match tendril {
363− Some(text) => acc.push(Change::Insert(text)),
364− None => acc.push(Change::Delete(to - from)),
356+ Some(text) => acc.push(Operation::Insert(text)),
357+ None => acc.push(Operation::Delete(to - from)),
365358 }
366359 last = to;
367360 }
368− acc.push(Change::Retain(len - last));
361+ acc.push(Operation::Retain(len - last));
369362
370363 Self::from(ChangeSet { changes: acc, len })
371364 }
@@ -373,7 +366,7 @@ impl Transaction {
373366 /// Generate a transaction with a change per selection range.
374367 pub fn change_by_selection<F>(state: &State, f: F) -> Self
375368 where
376− F: Fn(&SelectionRange) -> Change2,
369+ F: Fn(&SelectionRange) -> Change,
377370 {
378371 Self::change(state, state.selection.ranges.iter().map(f).collect())
379372 }
@@ -399,7 +392,7 @@ mod test {
399392
400393 #[test]
401394 fn composition() {
402− use Change::*;
395+ use Operation::*;
403396
404397 let a = ChangeSet {
405398 changes: vec![
@@ -428,7 +421,7 @@ mod test {
428421
429422 #[test]
430423 fn map_pos() {
431− use Change::*;
424+ use Operation::*;
432425
433426 // maps inserts
434427 let cs = ChangeSet {