on master help sign in
Change -> Operation, Change2 -> Change
22cb7b3Blaž Hrastnik
helix-core/src/selection.rs−1
@@ -105,7 +105,6 @@ pub struct Selection {
105}
105}
106
106
107impl Selection {
107impl Selection {
108− // map
109 // eq
108 // eq
110
109
111 #[must_use]
110 #[must_use]
helix-core/src/transaction.rs+17−24
@@ -1,8 +1,7 @@
1use crate::{Rope, Selection, SelectionRange, State, Tendril};
1use crate::{Rope, Selection, SelectionRange, State, Tendril};
2
2
3−// TODO: make Change enum internal and export this as the public Change type
4/// (from, to, replacement)
3/// (from, to, replacement)
5−type Change2 = (usize, usize, Option<Tendril>);
4+pub type Change = (usize, usize, Option<Tendril>);
6
5
7// TODO: divided into three different operations, I sort of like having just
6// TODO: divided into three different operations, I sort of like having just
8// Splice { extent, Option<text>, distance } better.
7// Splice { extent, Option<text>, distance } better.
@@ -12,7 +11,7 @@ type Change2 = (usize, usize, Option<Tendril>);
12// unchanged?: Splice { extent: 0, text: None, distance: 2 }
11// unchanged?: Splice { extent: 0, text: None, distance: 2 }
13// harder to compose though.
12// harder to compose though.
14#[derive(Debug, Clone, PartialEq, Eq)]
13#[derive(Debug, Clone, PartialEq, Eq)]
15−pub enum Change {
14+enum Operation {
16 /// Move cursor by n characters.
15 /// Move cursor by n characters.
17 Retain(usize),
16 Retain(usize),
18 /// Delete n characters.
17 /// Delete n characters.
@@ -21,12 +20,6 @@ pub enum Change {
21 Insert(Tendril),
20 Insert(Tendril),
22}
21}
23
22
24−// impl Change {
25−// pub fn new(from: usize, to: usize, insert: Option<Tendril>) {
26−// // old_extent, new_extent, insert
27−// }
28−// }
29−
30#[derive(Copy, Clone, PartialEq, Eq)]
23#[derive(Copy, Clone, PartialEq, Eq)]
31pub enum Assoc {
24pub enum Assoc {
32 Before,
25 Before,
@@ -36,7 +29,7 @@ pub enum Assoc {
36// ChangeSpec = Change | ChangeSet | Vec<Change>
29// ChangeSpec = Change | ChangeSet | Vec<Change>
37#[derive(Debug)]
30#[derive(Debug)]
38pub struct ChangeSet {
31pub struct ChangeSet {
39− changes: Vec<Change>,
32+ changes: Vec<Operation>,
40 /// The required document length. Will refuse to apply changes unless it matches.
33 /// The required document length. Will refuse to apply changes unless it matches.
41 len: usize,
34 len: usize,
42}
35}
@@ -46,7 +39,7 @@ impl ChangeSet {
46 pub fn new(doc: &Rope) -> Self {
39 pub fn new(doc: &Rope) -> Self {
47 let len = doc.len_chars();
40 let len = doc.len_chars();
48 Self {
41 Self {
49− changes: vec![Change::Retain(len)],
42+ changes: vec![Operation::Retain(len)],
50 len,
43 len,
51 }
44 }
52 }
45 }
@@ -70,11 +63,11 @@ impl ChangeSet {
70 let mut head_a = changes_a.next();
63 let mut head_a = changes_a.next();
71 let mut head_b = changes_b.next();
64 let mut head_b = changes_b.next();
72
65
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
74
67
75 loop {
68 loop {
76 use std::cmp::Ordering;
69 use std::cmp::Ordering;
77− use Change::*;
70+ use Operation::*;
78 match (head_a, head_b) {
71 match (head_a, head_b) {
79 // we are done
72 // we are done
80 (None, None) => {
73 (None, None) => {
@@ -211,7 +204,7 @@ impl ChangeSet {
211 let mut pos = 0;
204 let mut pos = 0;
212
205
213 for change in &self.changes {
206 for change in &self.changes {
214− use Change::*;
207+ use Operation::*;
215 match change {
208 match change {
216 Retain(n) => {
209 Retain(n) => {
217 pos += n;
210 pos += n;
@@ -233,7 +226,7 @@ impl ChangeSet {
233 #[inline]
226 #[inline]
234 pub fn is_empty(&self) -> bool {
227 pub fn is_empty(&self) -> bool {
235 let len = self.changes.len();
228 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))
237 }
230 }
238
231
239 /// Map a position through the changes.
232 /// Map a position through the changes.
@@ -243,7 +236,7 @@ impl ChangeSet {
243 /// range, or at that point. `After` will move it forward, placing it at the end of such
236 /// range, or at that point. `After` will move it forward, placing it at the end of such
244 /// insertions.
237 /// insertions.
245 pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize {
238 pub fn map_pos(&self, pos: usize, assoc: Assoc) -> usize {
246− use Change::*;
239+ use Operation::*;
247 let mut old_pos = 0;
240 let mut old_pos = 0;
248 let mut new_pos = 0;
241 let mut new_pos = 0;
249
242
@@ -348,7 +341,7 @@ impl Transaction {
348
341
349 /// Generate a transaction from a set of changes.
342 /// Generate a transaction from a set of changes.
350 // TODO: take an owned iter instead of Vec
343 // 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 {
352 let len = state.doc.len_chars();
345 let len = state.doc.len_chars();
353 let mut acc = Vec::with_capacity(2 * changes.len() + 1);
346 let mut acc = Vec::with_capacity(2 * changes.len() + 1);
354
347
@@ -358,14 +351,14 @@ impl Transaction {
358 for (from, to, tendril) in changes {
351 for (from, to, tendril) in changes {
359 // TODO: need to fill the in-between ranges too
352 // TODO: need to fill the in-between ranges too
360 // Retain from last "to" to current "from"
353 // Retain from last "to" to current "from"
361− acc.push(Change::Retain(from - last));
354+ acc.push(Operation::Retain(from - last));
362 match tendril {
355 match tendril {
363− Some(text) => acc.push(Change::Insert(text)),
356+ Some(text) => acc.push(Operation::Insert(text)),
364− None => acc.push(Change::Delete(to - from)),
357+ None => acc.push(Operation::Delete(to - from)),
365 }
358 }
366 last = to;
359 last = to;
367 }
360 }
368− acc.push(Change::Retain(len - last));
361+ acc.push(Operation::Retain(len - last));
369
362
370 Self::from(ChangeSet { changes: acc, len })
363 Self::from(ChangeSet { changes: acc, len })
371 }
364 }
@@ -373,7 +366,7 @@ impl Transaction {
373 /// Generate a transaction with a change per selection range.
366 /// Generate a transaction with a change per selection range.
374 pub fn change_by_selection<F>(state: &State, f: F) -> Self
367 pub fn change_by_selection<F>(state: &State, f: F) -> Self
375 where
368 where
376− F: Fn(&SelectionRange) -> Change2,
369+ F: Fn(&SelectionRange) -> Change,
377 {
370 {
378 Self::change(state, state.selection.ranges.iter().map(f).collect())
371 Self::change(state, state.selection.ranges.iter().map(f).collect())
379 }
372 }
@@ -399,7 +392,7 @@ mod test {
399
392
400 #[test]
393 #[test]
401 fn composition() {
394 fn composition() {
402− use Change::*;
395+ use Operation::*;
403
396
404 let a = ChangeSet {
397 let a = ChangeSet {
405 changes: vec![
398 changes: vec![
@@ -428,7 +421,7 @@ mod test {
428
421
429 #[test]
422 #[test]
430 fn map_pos() {
423 fn map_pos() {
431− use Change::*;
424+ use Operation::*;
432
425
433 // maps inserts
426 // maps inserts
434 let cs = ChangeSet {
427 let cs = ChangeSet {