2020-09-19 18:16:00 +04:00
|
|
|
use crate::{Range, Rope, Selection, State, Tendril};
|
2020-10-02 13:16:26 +04:00
|
|
|
use std::borrow::Cow;
|
2020-09-28 20:00:35 +04:00
|
|
|
use std::convert::TryFrom;
|
2020-05-26 13:11:11 +04:00
|
|
|
|
2020-09-13 10:48:15 +04:00
|
|
|
/// (from, to, replacement)
|
2020-09-13 14:18:05 +04:00
|
|
|
pub type Change = (usize, usize, Option<Tendril>);
|
2020-09-13 10:48:15 +04:00
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
// TODO: pub(crate)
|
2020-05-26 13:11:11 +04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-10-23 13:48:03 +04:00
|
|
|
pub enum Operation {
|
2020-05-26 13:11:11 +04:00
|
|
|
/// Move cursor by n characters.
|
|
|
|
Retain(usize),
|
|
|
|
/// Delete n characters.
|
|
|
|
Delete(usize),
|
|
|
|
/// Insert text at position.
|
|
|
|
Insert(Tendril),
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:46:00 +04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Assoc {
|
|
|
|
Before,
|
|
|
|
After,
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:21 +04:00
|
|
|
// ChangeSpec = Change | ChangeSet | Vec<Change>
|
2020-10-02 13:16:26 +04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-05-25 08:02:21 +04:00
|
|
|
pub struct ChangeSet {
|
2020-09-17 09:57:49 +04:00
|
|
|
pub(crate) changes: Vec<Operation>,
|
2020-05-26 13:11:11 +04:00
|
|
|
/// The required document length. Will refuse to apply changes unless it matches.
|
|
|
|
len: usize,
|
2020-05-25 08:02:21 +04:00
|
|
|
}
|
2020-05-26 13:11:11 +04:00
|
|
|
|
|
|
|
impl ChangeSet {
|
2020-05-28 09:45:44 +04:00
|
|
|
#[must_use]
|
2020-09-07 06:28:52 +04:00
|
|
|
pub fn new(doc: &Rope) -> Self {
|
|
|
|
let len = doc.len_chars();
|
2020-05-26 13:11:11 +04:00
|
|
|
Self {
|
2020-09-13 14:18:05 +04:00
|
|
|
changes: vec![Operation::Retain(len)],
|
2020-05-26 13:11:11 +04:00
|
|
|
len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: from iter
|
2020-10-23 13:48:03 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
#[doc(hidden)] // used by lsp to convert to LSP changes
|
|
|
|
pub fn changes(&self) -> &[Operation] {
|
|
|
|
&self.changes
|
|
|
|
}
|
2020-05-26 13:11:11 +04:00
|
|
|
|
2020-10-14 06:55:24 +04:00
|
|
|
#[must_use]
|
|
|
|
fn len_after(&self) -> usize {
|
|
|
|
use Operation::*;
|
|
|
|
|
|
|
|
let mut len = 0;
|
|
|
|
for change in &self.changes {
|
|
|
|
match change {
|
|
|
|
Retain(i) => len += i,
|
|
|
|
Insert(s) => len += s.chars().count(),
|
|
|
|
Delete(_) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
|
|
|
|
2021-02-16 06:03:36 +04:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:11:11 +04:00
|
|
|
/// Combine two changesets together.
|
|
|
|
/// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
|
|
|
|
/// returned value will represent the change `docA` → `docC`.
|
2021-01-08 11:31:19 +04:00
|
|
|
pub fn compose(self, other: ChangeSet) -> Self {
|
2020-10-14 06:55:24 +04:00
|
|
|
debug_assert!(self.len_after() == other.len);
|
2020-05-26 13:11:11 +04:00
|
|
|
|
|
|
|
let len = self.changes.len();
|
|
|
|
|
|
|
|
let mut changes_a = self.changes.into_iter();
|
|
|
|
let mut changes_b = other.changes.into_iter();
|
|
|
|
|
|
|
|
let mut head_a = changes_a.next();
|
|
|
|
let mut head_b = changes_b.next();
|
|
|
|
|
2021-02-16 06:03:36 +04:00
|
|
|
let mut changes = Self {
|
|
|
|
len: self.len,
|
|
|
|
changes: Vec::with_capacity(len), // TODO: max(a, b), shrink_to_fit() afterwards
|
|
|
|
};
|
2020-05-26 13:11:11 +04:00
|
|
|
|
|
|
|
loop {
|
|
|
|
use std::cmp::Ordering;
|
2020-09-13 14:18:05 +04:00
|
|
|
use Operation::*;
|
2020-05-26 13:11:11 +04:00
|
|
|
match (head_a, head_b) {
|
|
|
|
// we are done
|
|
|
|
(None, None) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// deletion in A
|
2021-02-16 06:03:36 +04:00
|
|
|
(Some(Delete(i)), b) => {
|
|
|
|
changes.delete(i);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = b;
|
|
|
|
}
|
|
|
|
// insertion in B
|
2021-02-15 19:15:49 +04:00
|
|
|
(a, Some(Insert(current))) => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.insert(current);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = a;
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
2021-01-08 11:31:19 +04:00
|
|
|
(None, _) | (_, None) => return unreachable!(),
|
2020-05-26 13:11:11 +04:00
|
|
|
(Some(Retain(i)), Some(Retain(j))) => match i.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.retain(i);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Retain(j - i));
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.retain(i);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.retain(j);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(Some(Insert(mut s)), Some(Delete(j))) => {
|
|
|
|
let len = s.chars().count();
|
|
|
|
match len.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Delete(j - len));
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
// figure out the byte index of the truncated string end
|
|
|
|
let (pos, _) = s.char_indices().nth(len - j).unwrap();
|
2020-10-14 06:46:13 +04:00
|
|
|
s.pop_front(s.len() as u32 - pos as u32);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = Some(Insert(s));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Some(Insert(mut s)), Some(Retain(j))) => {
|
|
|
|
let len = s.chars().count();
|
|
|
|
match len.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.insert(s);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Retain(j - len));
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.insert(s);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
// figure out the byte index of the truncated string end
|
|
|
|
let (pos, _) = s.char_indices().nth(j).unwrap();
|
2020-10-14 04:38:52 +04:00
|
|
|
let pos = pos as u32;
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.insert(s.subtendril(0, pos));
|
2020-10-14 04:38:52 +04:00
|
|
|
head_a = Some(Insert(s.subtendril(pos, s.len() as u32 - pos)));
|
2020-05-26 13:11:11 +04:00
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Some(Retain(i)), Some(Delete(j))) => match i.cmp(&j) {
|
|
|
|
Ordering::Less => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.delete(i);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = Some(Delete(j - i));
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.delete(j);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = changes_a.next();
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changes.delete(j);
|
2020-05-26 13:11:11 +04:00
|
|
|
head_a = Some(Retain(i - j));
|
|
|
|
head_b = changes_b.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-16 06:03:36 +04:00
|
|
|
changes
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given another change set starting in the same document, maps this
|
|
|
|
/// change set over the other, producing a new change set that can be
|
|
|
|
/// applied to the document produced by applying `other`. When
|
|
|
|
/// `before` is `true`, order changes as if `this` comes before
|
|
|
|
/// `other`, otherwise (the default) treat `other` as coming first.
|
|
|
|
///
|
|
|
|
/// Given two changes `A` and `B`, `A.compose(B.map(A))` and
|
|
|
|
/// `B.compose(A.map(B, true))` will produce the same document. This
|
|
|
|
/// provides a basic form of [operational
|
|
|
|
/// transformation](https://en.wikipedia.org/wiki/Operational_transformation),
|
|
|
|
/// and can be used for collaborative editing.
|
2020-06-02 05:57:01 +04:00
|
|
|
pub fn map(self, _other: Self) -> Self {
|
2020-05-26 13:11:11 +04:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new changeset that reverts this one. Useful for `undo` implementation.
|
2020-10-02 13:16:26 +04:00
|
|
|
/// The document parameter expects the original document before this change was applied.
|
|
|
|
pub fn invert(&self, original_doc: &Rope) -> Self {
|
2020-10-14 06:55:24 +04:00
|
|
|
assert!(original_doc.len_chars() == self.len);
|
2020-10-02 13:16:26 +04:00
|
|
|
|
|
|
|
let mut changes = Vec::with_capacity(self.changes.len());
|
|
|
|
let mut pos = 0;
|
|
|
|
let mut len = 0;
|
|
|
|
|
|
|
|
for change in &self.changes {
|
|
|
|
use Operation::*;
|
|
|
|
match change {
|
|
|
|
Retain(n) => {
|
|
|
|
changes.push(Retain(*n));
|
|
|
|
pos += n;
|
|
|
|
len += n;
|
|
|
|
}
|
|
|
|
Delete(n) => {
|
|
|
|
let text = Cow::from(original_doc.slice(pos..pos + *n));
|
|
|
|
changes.push(Insert(Tendril::from_slice(&text)));
|
|
|
|
pos += n;
|
|
|
|
}
|
|
|
|
Insert(s) => {
|
2020-10-04 12:16:37 +04:00
|
|
|
let chars = s.chars().count();
|
|
|
|
changes.push(Delete(chars));
|
|
|
|
len += chars;
|
2020-10-02 13:16:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self { changes, len }
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
/// Returns true if applied successfully.
|
|
|
|
pub fn apply(&self, text: &mut Rope) -> bool {
|
|
|
|
if text.len_chars() != self.len {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-26 13:11:11 +04:00
|
|
|
|
|
|
|
let mut pos = 0;
|
|
|
|
|
2020-05-28 09:45:44 +04:00
|
|
|
for change in &self.changes {
|
2020-09-13 14:18:05 +04:00
|
|
|
use Operation::*;
|
2020-05-26 13:11:11 +04:00
|
|
|
match change {
|
|
|
|
Retain(n) => {
|
|
|
|
pos += n;
|
|
|
|
}
|
|
|
|
Delete(n) => {
|
|
|
|
text.remove(pos..pos + *n);
|
|
|
|
// pos += n;
|
|
|
|
}
|
|
|
|
Insert(s) => {
|
|
|
|
text.insert(pos, s);
|
2020-10-04 12:16:37 +04:00
|
|
|
pos += s.chars().count();
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 12:08:28 +04:00
|
|
|
true
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:59:50 +04:00
|
|
|
/// `true` when the set is empty.
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2021-02-17 09:41:43 +04:00
|
|
|
matches!(self.changes.as_slice(), [] | [Operation::Retain(_)])
|
2020-05-28 09:59:50 +04:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:46:00 +04:00
|
|
|
/// Map a position through the changes.
|
|
|
|
///
|
|
|
|
/// `assoc` indicates which size to associate the position with. `Before` will keep the
|
|
|
|
/// position close to the character before, and will place it before insertions over that
|
|
|
|
/// 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 {
|
2020-09-13 14:18:05 +04:00
|
|
|
use Operation::*;
|
2020-05-28 09:46:00 +04:00
|
|
|
let mut old_pos = 0;
|
|
|
|
let mut new_pos = 0;
|
|
|
|
|
|
|
|
let mut iter = self.changes.iter().peekable();
|
|
|
|
|
|
|
|
while let Some(change) = iter.next() {
|
|
|
|
let len = match change {
|
|
|
|
Delete(i) | Retain(i) => *i,
|
|
|
|
Insert(_) => 0,
|
|
|
|
};
|
2021-02-18 07:17:33 +04:00
|
|
|
let mut old_end = old_pos + len;
|
2020-05-28 09:46:00 +04:00
|
|
|
|
|
|
|
match change {
|
2020-09-19 06:55:15 +04:00
|
|
|
Retain(_) => {
|
2020-05-28 09:46:00 +04:00
|
|
|
if old_end > pos {
|
|
|
|
return new_pos + (pos - old_pos);
|
|
|
|
}
|
|
|
|
new_pos += len;
|
|
|
|
}
|
2020-09-19 06:55:15 +04:00
|
|
|
Delete(_) => {
|
2020-05-28 09:46:00 +04:00
|
|
|
// in range
|
|
|
|
if old_end > pos {
|
2021-02-18 07:17:33 +04:00
|
|
|
return new_pos;
|
2020-05-28 09:46:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Insert(s) => {
|
|
|
|
let ins = s.chars().count();
|
2021-02-18 07:17:33 +04:00
|
|
|
|
|
|
|
// a subsequent delete means a replace, consume it
|
|
|
|
if let Some(Delete(len)) = iter.peek() {
|
|
|
|
old_end = old_pos + len;
|
|
|
|
// in range of replaced text
|
|
|
|
if old_end > pos {
|
|
|
|
// at point or tracking before
|
|
|
|
if pos == old_pos || assoc == Assoc::Before {
|
|
|
|
return new_pos;
|
|
|
|
} else {
|
|
|
|
// place to end of insert
|
|
|
|
return new_pos + ins;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// at insert point
|
|
|
|
if old_pos == pos {
|
|
|
|
// return position before inserted text
|
|
|
|
if assoc == Assoc::Before {
|
|
|
|
return new_pos;
|
|
|
|
} else {
|
|
|
|
// after text
|
|
|
|
return new_pos + ins;
|
|
|
|
}
|
2020-05-28 09:46:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_pos += ins;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old_pos = old_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos > old_pos {
|
|
|
|
panic!(
|
|
|
|
"Position {} is out of range for changeset len {}!",
|
|
|
|
pos, old_pos
|
|
|
|
)
|
|
|
|
}
|
|
|
|
new_pos
|
|
|
|
}
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transaction represents a single undoable unit of changes. Several changes can be grouped into
|
|
|
|
/// a single transaction.
|
2020-10-04 12:15:43 +04:00
|
|
|
#[derive(Debug, Clone)]
|
2020-05-26 13:11:11 +04:00
|
|
|
pub struct Transaction {
|
|
|
|
/// Changes made to the buffer.
|
2020-09-17 09:57:49 +04:00
|
|
|
pub(crate) changes: ChangeSet,
|
2020-05-26 13:11:11 +04:00
|
|
|
/// When set, explicitly updates the selection.
|
|
|
|
selection: Option<Selection>,
|
|
|
|
// effects, annotations
|
|
|
|
// scroll_into_view
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
impl Transaction {
|
2020-09-13 15:04:16 +04:00
|
|
|
/// Create a new, empty transaction.
|
|
|
|
pub fn new(state: &mut State) -> Self {
|
|
|
|
Self {
|
|
|
|
changes: ChangeSet::new(&state.doc),
|
|
|
|
selection: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:35:07 +04:00
|
|
|
pub fn changes(&self) -> &ChangeSet {
|
|
|
|
&self.changes
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
/// Returns true if applied successfully.
|
|
|
|
pub fn apply(&self, state: &mut State) -> bool {
|
2020-09-28 20:01:27 +04:00
|
|
|
if !self.changes.is_empty() {
|
|
|
|
// apply changes to the document
|
|
|
|
if !self.changes.apply(&mut state.doc) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-07 12:08:28 +04:00
|
|
|
}
|
|
|
|
|
2020-10-22 09:35:07 +04:00
|
|
|
// TODO: also avoid mapping the selection if not necessary
|
|
|
|
|
2020-09-07 12:08:28 +04:00
|
|
|
// update the selection: either take the selection specified in the transaction, or map the
|
|
|
|
// current selection through changes.
|
|
|
|
state.selection = self
|
|
|
|
.selection
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| state.selection.clone().map(&self.changes));
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:16:26 +04:00
|
|
|
/// Generate a transaction that reverts this one.
|
|
|
|
pub fn invert(&self, original: &State) -> Self {
|
2021-03-14 05:04:03 +04:00
|
|
|
let changes = self.changes.invert(&original.doc);
|
2020-10-02 13:16:26 +04:00
|
|
|
// Store the current cursor position
|
|
|
|
let selection = original.selection.clone();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
changes,
|
|
|
|
selection: Some(selection),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:51:42 +04:00
|
|
|
pub fn with_selection(mut self, selection: Selection) -> Self {
|
|
|
|
self.selection = Some(selection);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-13 10:48:15 +04:00
|
|
|
/// Generate a transaction from a set of changes.
|
2021-03-18 08:28:27 +04:00
|
|
|
pub fn change<I>(doc: &Rope, changes: I) -> Self
|
2020-09-13 18:12:14 +04:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Change> + ExactSizeIterator,
|
|
|
|
{
|
2021-03-18 08:28:27 +04:00
|
|
|
let len = doc.len_chars();
|
2021-02-16 06:03:36 +04:00
|
|
|
let acc = Vec::with_capacity(2 * changes.len() + 1);
|
|
|
|
let mut changeset = ChangeSet { changes: acc, len };
|
2020-09-13 06:32:37 +04:00
|
|
|
|
2020-10-14 04:38:52 +04:00
|
|
|
// TODO: verify ranges are ordered and not overlapping or change will panic.
|
2020-09-07 12:08:28 +04:00
|
|
|
|
2020-09-13 06:32:37 +04:00
|
|
|
let mut last = 0;
|
|
|
|
for (from, to, tendril) in changes {
|
|
|
|
// Retain from last "to" to current "from"
|
2021-02-16 06:03:36 +04:00
|
|
|
changeset.retain(from - last);
|
2020-09-17 09:57:49 +04:00
|
|
|
let span = to - from;
|
2020-09-13 06:32:37 +04:00
|
|
|
match tendril {
|
2020-09-17 09:57:49 +04:00
|
|
|
Some(text) => {
|
2021-02-16 06:03:36 +04:00
|
|
|
changeset.insert(text);
|
2021-02-16 06:09:05 +04:00
|
|
|
changeset.delete(span);
|
2020-09-17 09:57:49 +04:00
|
|
|
}
|
2021-02-16 06:03:36 +04:00
|
|
|
None => changeset.delete(span),
|
2020-09-13 06:32:37 +04:00
|
|
|
}
|
|
|
|
last = to;
|
2020-09-07 12:08:28 +04:00
|
|
|
}
|
2020-10-14 04:38:52 +04:00
|
|
|
|
2021-02-16 06:03:36 +04:00
|
|
|
changeset.retain(len - last);
|
2020-09-07 12:08:28 +04:00
|
|
|
|
2021-02-16 06:03:36 +04:00
|
|
|
Self::from(changeset)
|
2020-09-07 12:08:28 +04:00
|
|
|
}
|
|
|
|
|
2020-09-13 10:48:15 +04:00
|
|
|
/// Generate a transaction with a change per selection range.
|
|
|
|
pub fn change_by_selection<F>(state: &State, f: F) -> Self
|
|
|
|
where
|
2020-10-06 11:00:23 +04:00
|
|
|
F: FnMut(&Range) -> Change,
|
2020-09-13 10:48:15 +04:00
|
|
|
{
|
2021-03-18 08:28:27 +04:00
|
|
|
Self::change(&state.doc, state.selection.ranges().iter().map(f))
|
2020-09-13 10:48:15 +04:00
|
|
|
}
|
|
|
|
|
2020-09-13 06:32:37 +04:00
|
|
|
/// Insert text at each selection head.
|
|
|
|
pub fn insert(state: &State, text: Tendril) -> Self {
|
|
|
|
Self::change_by_selection(state, |range| (range.head, range.head, Some(text.clone())))
|
2020-09-07 12:08:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ChangeSet> for Transaction {
|
|
|
|
fn from(changes: ChangeSet) -> Self {
|
|
|
|
Self {
|
|
|
|
changes,
|
|
|
|
selection: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 12:47:35 +04:00
|
|
|
|
2020-05-26 13:11:11 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn composition() {
|
2020-09-13 14:18:05 +04:00
|
|
|
use Operation::*;
|
2020-05-26 13:11:11 +04:00
|
|
|
|
|
|
|
let a = ChangeSet {
|
|
|
|
changes: vec![
|
|
|
|
Retain(5),
|
2020-10-14 06:46:13 +04:00
|
|
|
Insert(" test!".into()),
|
2020-05-26 13:11:11 +04:00
|
|
|
Retain(1),
|
|
|
|
Delete(2),
|
2020-10-06 09:44:33 +04:00
|
|
|
Insert("abc".into()),
|
2020-05-26 13:11:11 +04:00
|
|
|
],
|
2020-10-06 09:44:33 +04:00
|
|
|
len: 8,
|
2020-05-26 13:11:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
let b = ChangeSet {
|
2020-10-14 06:46:13 +04:00
|
|
|
changes: vec![Delete(10), Insert("world".into()), Retain(5)],
|
|
|
|
len: 15,
|
2020-05-26 13:11:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut text = Rope::from("hello xz");
|
|
|
|
|
|
|
|
// should probably return cloned text
|
2021-01-08 11:31:19 +04:00
|
|
|
let composed = a.compose(b);
|
2020-10-06 09:44:33 +04:00
|
|
|
assert_eq!(composed.len, 8);
|
|
|
|
assert!(composed.apply(&mut text));
|
|
|
|
assert_eq!(text, "world! abc");
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:16:26 +04:00
|
|
|
#[test]
|
|
|
|
fn invert() {
|
|
|
|
use Operation::*;
|
|
|
|
|
|
|
|
let changes = ChangeSet {
|
|
|
|
changes: vec![Retain(4), Delete(5), Insert("test".into()), Retain(3)],
|
|
|
|
len: 12,
|
|
|
|
};
|
|
|
|
|
2020-10-04 12:16:37 +04:00
|
|
|
let doc = Rope::from("世界3 hello xz");
|
2020-10-02 13:16:26 +04:00
|
|
|
let revert = changes.invert(&doc);
|
|
|
|
|
|
|
|
let mut doc2 = doc.clone();
|
|
|
|
changes.apply(&mut doc2);
|
|
|
|
|
|
|
|
// a revert is different
|
|
|
|
assert_ne!(changes, revert);
|
|
|
|
assert_ne!(doc, doc2);
|
|
|
|
|
|
|
|
// but inverting a revert will give us the original
|
|
|
|
assert_eq!(changes, revert.invert(&doc2));
|
|
|
|
|
|
|
|
// applying a revert gives us back the original
|
|
|
|
revert.apply(&mut doc2);
|
|
|
|
assert_eq!(doc, doc2);
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:11:11 +04:00
|
|
|
#[test]
|
2020-05-28 09:46:00 +04:00
|
|
|
fn map_pos() {
|
2020-09-13 14:18:05 +04:00
|
|
|
use Operation::*;
|
2020-05-28 09:46:00 +04:00
|
|
|
|
|
|
|
// maps inserts
|
|
|
|
let cs = ChangeSet {
|
|
|
|
changes: vec![Retain(4), Insert("!!".into()), Retain(4)],
|
|
|
|
len: 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(cs.map_pos(0, Assoc::Before), 0); // before insert region
|
|
|
|
assert_eq!(cs.map_pos(4, Assoc::Before), 4); // at insert, track before
|
|
|
|
assert_eq!(cs.map_pos(4, Assoc::After), 6); // at insert, track after
|
|
|
|
assert_eq!(cs.map_pos(5, Assoc::Before), 7); // after insert region
|
|
|
|
|
|
|
|
// maps deletes
|
|
|
|
let cs = ChangeSet {
|
|
|
|
changes: vec![Retain(4), Delete(4), Retain(4)],
|
|
|
|
len: 12,
|
|
|
|
};
|
|
|
|
assert_eq!(cs.map_pos(0, Assoc::Before), 0); // at start
|
|
|
|
assert_eq!(cs.map_pos(4, Assoc::Before), 4); // before a delete
|
|
|
|
assert_eq!(cs.map_pos(5, Assoc::Before), 4); // inside a delete
|
|
|
|
assert_eq!(cs.map_pos(5, Assoc::After), 4); // inside a delete
|
|
|
|
|
|
|
|
// TODO: delete tracking
|
|
|
|
|
|
|
|
// stays inbetween replacements
|
|
|
|
let cs = ChangeSet {
|
|
|
|
changes: vec![
|
|
|
|
Insert("ab".into()),
|
|
|
|
Delete(2),
|
|
|
|
Insert("cd".into()),
|
2021-02-18 07:17:33 +04:00
|
|
|
Delete(2),
|
2020-05-28 09:46:00 +04:00
|
|
|
],
|
|
|
|
len: 4,
|
|
|
|
};
|
|
|
|
assert_eq!(cs.map_pos(2, Assoc::Before), 2);
|
|
|
|
assert_eq!(cs.map_pos(2, Assoc::After), 2);
|
|
|
|
}
|
2020-09-17 09:57:49 +04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transaction_change() {
|
|
|
|
let mut state = State::new("hello world!\ntest 123".into());
|
|
|
|
let transaction = Transaction::change(
|
2021-03-18 08:28:27 +04:00
|
|
|
&state.doc,
|
2020-10-08 09:21:03 +04:00
|
|
|
// (1, 1, None) is a useless 0-width delete
|
2020-10-14 04:38:52 +04:00
|
|
|
vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
|
2020-09-17 09:57:49 +04:00
|
|
|
);
|
|
|
|
transaction.apply(&mut state);
|
|
|
|
assert_eq!(state.doc, Rope::from_str("hello void! 123"));
|
|
|
|
}
|
2021-02-15 19:15:49 +04:00
|
|
|
|
|
|
|
#[test]
|
2021-02-16 06:03:36 +04:00
|
|
|
fn optimized_composition() {
|
2021-02-15 19:15:49 +04:00
|
|
|
let mut state = State::new("".into());
|
|
|
|
let t1 = Transaction::insert(&state, Tendril::from_char('h'));
|
|
|
|
t1.apply(&mut state);
|
|
|
|
let t2 = Transaction::insert(&state, Tendril::from_char('e'));
|
|
|
|
t2.apply(&mut state);
|
|
|
|
let t3 = Transaction::insert(&state, Tendril::from_char('l'));
|
|
|
|
t3.apply(&mut state);
|
|
|
|
let t4 = Transaction::insert(&state, Tendril::from_char('l'));
|
|
|
|
t4.apply(&mut state);
|
|
|
|
let t5 = Transaction::insert(&state, Tendril::from_char('o'));
|
|
|
|
t5.apply(&mut state);
|
|
|
|
|
|
|
|
assert_eq!(state.doc, Rope::from_str("hello"));
|
|
|
|
|
|
|
|
// changesets as follows:
|
|
|
|
// h
|
|
|
|
// retain 1, e
|
|
|
|
// retain 2, l
|
|
|
|
|
|
|
|
let mut changes = t1
|
|
|
|
.changes
|
|
|
|
.compose(t2.changes)
|
|
|
|
.compose(t3.changes)
|
|
|
|
.compose(t4.changes)
|
|
|
|
.compose(t5.changes);
|
|
|
|
|
|
|
|
use Operation::*;
|
|
|
|
assert_eq!(changes.changes, &[Insert("hello".into())]);
|
2021-02-16 06:03:36 +04:00
|
|
|
// instead of insert h, insert e, insert l, insert l, insert o
|
2021-02-15 19:15:49 +04:00
|
|
|
}
|
2020-05-26 13:11:11 +04:00
|
|
|
}
|