2023-04-17 14:56:00 +02:00
|
|
|
use operational_transform::OperationSeq;
|
|
|
|
use similar::{TextDiff, ChangeTag};
|
|
|
|
|
|
|
|
pub trait OperationFactory {
|
|
|
|
fn content(&self) -> String;
|
|
|
|
|
2023-04-20 04:36:04 +02:00
|
|
|
fn replace(&self, txt: &str) -> OperationSeq { self.delta(0, txt, 0) }
|
|
|
|
|
|
|
|
fn delta(&self, skip: usize, txt: &str, tail: usize) -> OperationSeq {
|
2023-04-17 14:56:00 +02:00
|
|
|
let mut out = OperationSeq::default();
|
2023-04-18 21:46:19 +02:00
|
|
|
let content = self.content();
|
2023-04-20 04:36:04 +02:00
|
|
|
let tail_index = content.len() - tail;
|
|
|
|
let content_slice = &content[skip..tail_index];
|
|
|
|
|
|
|
|
if content_slice == txt {
|
2023-04-17 14:56:00 +02:00
|
|
|
return out; // TODO this won't work, should we return a noop instead?
|
|
|
|
}
|
|
|
|
|
2023-04-20 04:36:04 +02:00
|
|
|
out.retain(skip as u64);
|
|
|
|
|
|
|
|
let diff = TextDiff::from_chars(content_slice, txt);
|
2023-04-17 14:56:00 +02:00
|
|
|
|
|
|
|
for change in diff.iter_all_changes() {
|
|
|
|
match change.tag() {
|
|
|
|
ChangeTag::Equal => out.retain(1),
|
|
|
|
ChangeTag::Delete => out.delete(1),
|
|
|
|
ChangeTag::Insert => out.insert(change.value()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 04:36:04 +02:00
|
|
|
out.retain(tail as u64);
|
|
|
|
|
2023-04-17 14:56:00 +02:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&self, txt: &str, pos: u64) -> OperationSeq {
|
|
|
|
let mut out = OperationSeq::default();
|
|
|
|
let total = self.content().len() as u64;
|
|
|
|
out.retain(pos);
|
|
|
|
out.insert(txt);
|
|
|
|
out.retain(total - pos);
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(&self, pos: u64, count: u64) -> OperationSeq {
|
|
|
|
let mut out = OperationSeq::default();
|
|
|
|
let len = self.content().len() as u64;
|
|
|
|
out.retain(pos - count);
|
|
|
|
out.delete(count);
|
|
|
|
out.retain(len - pos);
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cancel(&self, pos: u64, count: u64) -> OperationSeq {
|
|
|
|
let mut out = OperationSeq::default();
|
|
|
|
let len = self.content().len() as u64;
|
|
|
|
out.retain(pos);
|
|
|
|
out.delete(count);
|
|
|
|
out.retain(len - (pos+count));
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|