|
|
@ -63,6 +63,56 @@ impl ChangeSet {
|
|
|
|
len
|
|
|
|
len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Changeset builder operations: delete/insert/retain
|
|
|
|
|
|
|
|
fn delete(&mut self, n: usize) {
|
|
|
|
|
|
|
|
use Operation::*;
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(Delete(count)) = self.changes.last_mut() {
|
|
|
|
|
|
|
|
*count += n;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
self.changes.push(Delete(n));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn insert(&mut self, fragment: Tendril) {
|
|
|
|
|
|
|
|
use Operation::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if fragment.is_empty() {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let new_last = match self.changes.as_mut_slice() {
|
|
|
|
|
|
|
|
[.., Insert(prev)] => {
|
|
|
|
|
|
|
|
prev.push_tendril(&fragment);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[.., Insert(prev), Delete(_)] => {
|
|
|
|
|
|
|
|
prev.push_tendril(&fragment);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[.., last @ Delete(_)] => std::mem::replace(last, Insert(fragment)),
|
|
|
|
|
|
|
|
_ => Insert(fragment),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.changes.push(new_last);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn retain(&mut self, n: usize) {
|
|
|
|
|
|
|
|
use Operation::*;
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(Retain(count)) = self.changes.last_mut() {
|
|
|
|
|
|
|
|
*count += n;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
self.changes.push(Retain(n));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Combine two changesets together.
|
|
|
|
/// Combine two changesets together.
|
|
|
|
/// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
|
|
|
|
/// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
|
|
|
|
/// returned value will represent the change `docA` → `docC`.
|
|
|
|
/// returned value will represent the change `docA` → `docC`.
|
|
|
@ -77,7 +127,10 @@ impl ChangeSet {
|
|
|
|
let mut head_a = changes_a.next();
|
|
|
|
let mut head_a = changes_a.next();
|
|
|
|
let mut head_b = changes_b.next();
|
|
|
|
let mut head_b = changes_b.next();
|
|
|
|
|
|
|
|
|
|
|
|
let mut changes: Vec<Operation> = Vec::with_capacity(len); // TODO: max(a, b), shrink_to_fit() afterwards
|
|
|
|
let mut changes = Self {
|
|
|
|
|
|
|
|
len: self.len,
|
|
|
|
|
|
|
|
changes: Vec::with_capacity(len), // TODO: max(a, b), shrink_to_fit() afterwards
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::cmp::Ordering;
|
|
|
@ -88,37 +141,31 @@ impl ChangeSet {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// deletion in A
|
|
|
|
// deletion in A
|
|
|
|
(Some(change @ Delete(..)), b) => {
|
|
|
|
(Some(Delete(i)), b) => {
|
|
|
|
changes.push(change);
|
|
|
|
changes.delete(i);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = b;
|
|
|
|
head_b = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// insertion in B
|
|
|
|
// insertion in B
|
|
|
|
(a, Some(Insert(current))) => {
|
|
|
|
(a, Some(Insert(current))) => {
|
|
|
|
// merge onto previous insert if possible
|
|
|
|
changes.insert(current);
|
|
|
|
// TODO: do these as operations on a changeset
|
|
|
|
|
|
|
|
if let Some(Insert(prev)) = changes.last_mut() {
|
|
|
|
|
|
|
|
prev.push_tendril(¤t);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
changes.push(Insert(current));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
head_a = a;
|
|
|
|
head_a = a;
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(None, _) | (_, None) => return unreachable!(),
|
|
|
|
(None, _) | (_, None) => return unreachable!(),
|
|
|
|
(Some(Retain(i)), Some(Retain(j))) => match i.cmp(&j) {
|
|
|
|
(Some(Retain(i)), Some(Retain(j))) => match i.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
|
|
|
Ordering::Less => {
|
|
|
|
changes.push(Retain(i));
|
|
|
|
changes.retain(i);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Retain(j - i));
|
|
|
|
head_b = Some(Retain(j - i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
Ordering::Equal => {
|
|
|
|
changes.push(Retain(i));
|
|
|
|
changes.retain(i);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
Ordering::Greater => {
|
|
|
|
changes.push(Retain(j));
|
|
|
|
changes.retain(j);
|
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -147,12 +194,12 @@ impl ChangeSet {
|
|
|
|
let len = s.chars().count();
|
|
|
|
let len = s.chars().count();
|
|
|
|
match len.cmp(&j) {
|
|
|
|
match len.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
|
|
|
Ordering::Less => {
|
|
|
|
changes.push(Insert(s));
|
|
|
|
changes.insert(s);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Retain(j - len));
|
|
|
|
head_b = Some(Retain(j - len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
Ordering::Equal => {
|
|
|
|
changes.push(Insert(s));
|
|
|
|
changes.insert(s);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -160,7 +207,7 @@ impl ChangeSet {
|
|
|
|
// figure out the byte index of the truncated string end
|
|
|
|
// figure out the byte index of the truncated string end
|
|
|
|
let (pos, _) = s.char_indices().nth(j).unwrap();
|
|
|
|
let (pos, _) = s.char_indices().nth(j).unwrap();
|
|
|
|
let pos = pos as u32;
|
|
|
|
let pos = pos as u32;
|
|
|
|
changes.push(Insert(s.subtendril(0, pos)));
|
|
|
|
changes.insert(s.subtendril(0, pos));
|
|
|
|
head_a = Some(Insert(s.subtendril(pos, s.len() as u32 - pos)));
|
|
|
|
head_a = Some(Insert(s.subtendril(pos, s.len() as u32 - pos)));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -168,17 +215,17 @@ impl ChangeSet {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Some(Retain(i)), Some(Delete(j))) => match i.cmp(&j) {
|
|
|
|
(Some(Retain(i)), Some(Delete(j))) => match i.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
|
|
|
Ordering::Less => {
|
|
|
|
changes.push(Delete(i));
|
|
|
|
changes.delete(i);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Delete(j - i));
|
|
|
|
head_b = Some(Delete(j - i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
Ordering::Equal => {
|
|
|
|
changes.push(Delete(j));
|
|
|
|
changes.delete(j);
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
Ordering::Greater => {
|
|
|
|
changes.push(Delete(j));
|
|
|
|
changes.delete(j);
|
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -186,10 +233,7 @@ impl ChangeSet {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
changes
|
|
|
|
len: self.len,
|
|
|
|
|
|
|
|
changes,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Given another change set starting in the same document, maps this
|
|
|
|
/// Given another change set starting in the same document, maps this
|
|
|
@ -421,37 +465,29 @@ impl Transaction {
|
|
|
|
I: IntoIterator<Item = Change> + ExactSizeIterator,
|
|
|
|
I: IntoIterator<Item = Change> + ExactSizeIterator,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let len = state.doc.len_chars();
|
|
|
|
let len = state.doc.len_chars();
|
|
|
|
let mut acc = Vec::with_capacity(2 * changes.len() + 1);
|
|
|
|
let acc = Vec::with_capacity(2 * changes.len() + 1);
|
|
|
|
|
|
|
|
let mut changeset = ChangeSet { changes: acc, len };
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: verify ranges are ordered and not overlapping or change will panic.
|
|
|
|
// TODO: verify ranges are ordered and not overlapping or change will panic.
|
|
|
|
|
|
|
|
|
|
|
|
let mut last = 0;
|
|
|
|
let mut last = 0;
|
|
|
|
for (from, to, tendril) in changes {
|
|
|
|
for (from, to, tendril) in changes {
|
|
|
|
// Retain from last "to" to current "from"
|
|
|
|
// Retain from last "to" to current "from"
|
|
|
|
if from - last > 0 {
|
|
|
|
changeset.retain(from - last);
|
|
|
|
acc.push(Operation::Retain(from - last));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = to - from;
|
|
|
|
let span = to - from;
|
|
|
|
match tendril {
|
|
|
|
match tendril {
|
|
|
|
Some(text) => {
|
|
|
|
Some(text) => {
|
|
|
|
if span > 0 {
|
|
|
|
changeset.delete(span);
|
|
|
|
acc.push(Operation::Delete(span));
|
|
|
|
changeset.insert(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc.push(Operation::Insert(text));
|
|
|
|
None => changeset.delete(span),
|
|
|
|
}
|
|
|
|
|
|
|
|
None if span > 0 => acc.push(Operation::Delete(span)),
|
|
|
|
|
|
|
|
// empty delete is useless
|
|
|
|
|
|
|
|
None => (),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = to;
|
|
|
|
last = to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let span = len - last;
|
|
|
|
changeset.retain(len - last);
|
|
|
|
if span > 0 {
|
|
|
|
|
|
|
|
acc.push(Operation::Retain(span));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Self::from(ChangeSet { changes: acc, len })
|
|
|
|
Self::from(changeset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Generate a transaction with a change per selection range.
|
|
|
|
/// Generate a transaction with a change per selection range.
|
|
|
@ -591,7 +627,7 @@ mod test {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn insert_composition() {
|
|
|
|
fn optimized_composition() {
|
|
|
|
let mut state = State::new("".into());
|
|
|
|
let mut state = State::new("".into());
|
|
|
|
let t1 = Transaction::insert(&state, Tendril::from_char('h'));
|
|
|
|
let t1 = Transaction::insert(&state, Tendril::from_char('h'));
|
|
|
|
t1.apply(&mut state);
|
|
|
|
t1.apply(&mut state);
|
|
|
@ -620,5 +656,6 @@ mod test {
|
|
|
|
|
|
|
|
|
|
|
|
use Operation::*;
|
|
|
|
use Operation::*;
|
|
|
|
assert_eq!(changes.changes, &[Insert("hello".into())]);
|
|
|
|
assert_eq!(changes.changes, &[Insert("hello".into())]);
|
|
|
|
|
|
|
|
// instead of insert h, insert e, insert l, insert l, insert o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|