Merge crates
This commit is contained in:
parent
82e77eec89
commit
bcbac03228
60 changed files with 73 additions and 248 deletions
42
Cargo.lock
generated
42
Cargo.lock
generated
|
|
@ -2,12 +2,6 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "base64"
|
|
||||||
version = "0.22.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.16.0"
|
version = "3.16.0"
|
||||||
|
|
@ -206,11 +200,14 @@ dependencies = [
|
||||||
name = "reconcile"
|
name = "reconcile"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"console_error_panic_hook",
|
||||||
"insta",
|
"insta",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
"test-case",
|
"test-case",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"wasm-bindgen-test",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -290,19 +287,6 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sync_lib"
|
|
||||||
version = "0.4.0"
|
|
||||||
dependencies = [
|
|
||||||
"base64",
|
|
||||||
"console_error_panic_hook",
|
|
||||||
"insta",
|
|
||||||
"reconcile",
|
|
||||||
"thiserror",
|
|
||||||
"wasm-bindgen",
|
|
||||||
"wasm-bindgen-test",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "test-case"
|
name = "test-case"
|
||||||
version = "3.3.1"
|
version = "3.3.1"
|
||||||
|
|
@ -336,26 +320,6 @@ dependencies = [
|
||||||
"test-case-core",
|
"test-case-core",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "thiserror"
|
|
||||||
version = "2.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
|
||||||
dependencies = [
|
|
||||||
"thiserror-impl",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "thiserror-impl"
|
|
||||||
version = "2.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
|
|
||||||
43
Cargo.toml
43
Cargo.toml
|
|
@ -1,21 +1,38 @@
|
||||||
[workspace]
|
[package]
|
||||||
resolver = "2"
|
name = "reconcile"
|
||||||
members = [
|
rust-version = "1.85"
|
||||||
"reconcile",
|
|
||||||
"sync_lib"
|
|
||||||
]
|
|
||||||
|
|
||||||
[workspace.package]
|
|
||||||
rust-version = "1.83"
|
|
||||||
authors = ["Andras Schmelczer <andras@schmelczer.dev>"]
|
authors = ["Andras Schmelczer <andras@schmelczer.dev>"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/schmelczer/vault-link"
|
repository = "https://github.com/schmelczer/reconcile"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[lib]
|
||||||
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
|
crate-type = ["cdylib", "rlib"]
|
||||||
thiserror = { version = "2.0.12", default-features = false }
|
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0.219", optional = true, features = ["derive"] }
|
||||||
|
|
||||||
|
wasm-bindgen = { version = "0.2.99", optional = true }
|
||||||
|
|
||||||
|
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||||
|
# logging them with `console.error`. This is great for development, but requires
|
||||||
|
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
|
||||||
|
# code size when deploying.
|
||||||
|
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["serde", "wasm"]
|
||||||
|
serde = [ "dep:serde" ]
|
||||||
|
wasm = [ "dep:wasm-bindgen", "dep:console_error_panic_hook" ]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
insta = "1.42.2"
|
||||||
|
pretty_assertions = "1.4.1"
|
||||||
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
serde_yaml ="0.9.34"
|
||||||
|
test-case = "3.3.1"
|
||||||
|
wasm-bindgen-test = "0.3.49"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "reconcile"
|
|
||||||
version.workspace = true
|
|
||||||
edition.workspace = true
|
|
||||||
authors.workspace = true
|
|
||||||
license.workspace = true
|
|
||||||
repository.workspace = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = { version = "1.0.219", optional = true, features = ["derive"] }
|
|
||||||
|
|
||||||
[features]
|
|
||||||
serde = [ "dep:serde" ]
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
insta = "1.42.2"
|
|
||||||
pretty_assertions = "1.4.1"
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
serde_yaml ="0.9.34"
|
|
||||||
test-case = "3.3.1"
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
2
src/diffs.rs
Normal file
2
src/diffs.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod myers;
|
||||||
|
pub mod raw_operation;
|
||||||
357
src/diffs/myers.rs
Normal file
357
src/diffs/myers.rs
Normal file
|
|
@ -0,0 +1,357 @@
|
||||||
|
//! Taken from <https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/myers.rs>
|
||||||
|
//!
|
||||||
|
//! Myers' diff algorithm.
|
||||||
|
//!
|
||||||
|
//! * time: `O((N+M)D)`
|
||||||
|
//! * space `O(N+M)`
|
||||||
|
//!
|
||||||
|
//! See [the original article by Eugene W. Myers](http://www.xmailserver.org/diff2.pdf)
|
||||||
|
//! describing it.
|
||||||
|
//!
|
||||||
|
//! The implementation of this algorithm is based on the implementation by
|
||||||
|
//! Brandon Williams.
|
||||||
|
//!
|
||||||
|
//! # Heuristics
|
||||||
|
//!
|
||||||
|
//! At present this implementation of Myers' does not implement any more
|
||||||
|
//! advanced heuristics that would solve some pathological cases. For instance
|
||||||
|
//! passing two large and completely distinct sequences to the algorithm will
|
||||||
|
//! make it spin without making reasonable progress.
|
||||||
|
//! For potential improvements here see [similar#15](https://github.com/mitsuhiko/similar/issues/15).
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
ops::{Index, IndexMut, Range},
|
||||||
|
vec,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::raw_operation::RawOperation;
|
||||||
|
use crate::{
|
||||||
|
tokenizer::token::Token,
|
||||||
|
utils::{common_prefix_len::common_prefix_len, common_suffix_len::common_suffix_len},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Myers' diff algorithm.
|
||||||
|
///
|
||||||
|
/// Diff `old`, between indices `old_range` and `new` between indices
|
||||||
|
/// `new_range`.
|
||||||
|
///
|
||||||
|
/// The returned `RawOperations` all have a token count of 1.
|
||||||
|
pub fn diff<T>(old: &[Token<T>], new: &[Token<T>]) -> Vec<RawOperation<T>>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let max_d = (old.len() + new.len()).div_ceil(2) + 1;
|
||||||
|
let mut vb = V::new(max_d);
|
||||||
|
let mut vf = V::new(max_d);
|
||||||
|
let mut result: Vec<RawOperation<T>> = vec![];
|
||||||
|
|
||||||
|
conquer(
|
||||||
|
old,
|
||||||
|
0..old.len(),
|
||||||
|
new,
|
||||||
|
0..new.len(),
|
||||||
|
&mut vf,
|
||||||
|
&mut vb,
|
||||||
|
&mut result,
|
||||||
|
);
|
||||||
|
|
||||||
|
debug_assert!(
|
||||||
|
result.iter().all(|op| op.tokens().len() == 1),
|
||||||
|
"All operations should be of length 1"
|
||||||
|
);
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// A D-path is a path which starts at (0,0) that has exactly D non-diagonal
|
||||||
|
// edges. All D-paths consist of a (D - 1)-path followed by a non-diagonal edge
|
||||||
|
// and then a possibly empty sequence of diagonal edges called a snake.
|
||||||
|
|
||||||
|
/// `V` contains the endpoints of the furthest reaching `D-paths`. For each
|
||||||
|
/// recorded endpoint `(x,y)` in diagonal `k`, we only need to retain `x`
|
||||||
|
/// because `y` can be computed from `x - k`. In other words, `V` is an array of
|
||||||
|
/// integers where `V[k]` contains the row index of the endpoint of the furthest
|
||||||
|
/// reaching path in diagonal `k`.
|
||||||
|
///
|
||||||
|
/// We can't use a traditional Vec to represent `V` since we use `k` as an index
|
||||||
|
/// and it can take on negative values. So instead `V` is represented as a
|
||||||
|
/// light-weight wrapper around a Vec plus an `offset` which is the maximum
|
||||||
|
/// value `k` can take on in order to map negative `k`'s back to a value >= 0.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct V {
|
||||||
|
offset: isize,
|
||||||
|
v: Vec<usize>, // Look into initializing this to -1 and storing isize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl V {
|
||||||
|
fn new(max_d: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
offset: max_d as isize,
|
||||||
|
v: vec![0; 2 * max_d],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn len(&self) -> usize { self.v.len() }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Index<isize> for V {
|
||||||
|
type Output = usize;
|
||||||
|
|
||||||
|
fn index(&self, index: isize) -> &Self::Output { &self.v[(index + self.offset) as usize] }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IndexMut<isize> for V {
|
||||||
|
fn index_mut(&mut self, index: isize) -> &mut Self::Output {
|
||||||
|
&mut self.v[(index + self.offset) as usize]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn split_at(range: Range<usize>, at: usize) -> (Range<usize>, Range<usize>) {
|
||||||
|
(range.start..at, at..range.end)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `Snake` is a sequence of diagonal edges in the edit graph. Normally
|
||||||
|
/// a snake has a start end end point (and it is possible for a snake to have
|
||||||
|
/// a length of zero, meaning the start and end points are the same) however
|
||||||
|
/// we do not need the end point which is why it's not implemented here.
|
||||||
|
///
|
||||||
|
/// The divide part of a divide-and-conquer strategy. A D-path has D+1 snakes
|
||||||
|
/// some of which may be empty. The divide step requires finding the ceil(D/2) +
|
||||||
|
/// 1 or middle snake of an optimal D-path. The idea for doing so is to
|
||||||
|
/// simultaneously run the basic algorithm in both the forward and reverse
|
||||||
|
/// directions until furthest reaching forward and reverse paths starting at
|
||||||
|
/// opposing corners 'overlap'.
|
||||||
|
fn find_middle_snake<T>(
|
||||||
|
old: &[Token<T>],
|
||||||
|
old_range: Range<usize>,
|
||||||
|
new: &[Token<T>],
|
||||||
|
new_range: Range<usize>,
|
||||||
|
vf: &mut V,
|
||||||
|
vb: &mut V,
|
||||||
|
) -> Option<(usize, usize)>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let n = old_range.len();
|
||||||
|
let m = new_range.len();
|
||||||
|
|
||||||
|
// By Lemma 1 in the paper, the optimal edit script length is odd or even as
|
||||||
|
// `delta` is odd or even.
|
||||||
|
let delta = n as isize - m as isize;
|
||||||
|
let odd = delta & 1 == 1;
|
||||||
|
|
||||||
|
// The initial point at (0, -1)
|
||||||
|
vf[1] = 0;
|
||||||
|
// The initial point at (N, M+1)
|
||||||
|
vb[1] = 0;
|
||||||
|
|
||||||
|
let d_max = (n + m).div_ceil(2) + 1;
|
||||||
|
assert!(vf.len() >= d_max);
|
||||||
|
assert!(vb.len() >= d_max);
|
||||||
|
|
||||||
|
for d in 0..d_max as isize {
|
||||||
|
// Forward path
|
||||||
|
for k in (-d..=d).rev().step_by(2) {
|
||||||
|
let mut x = if k == -d || (k != d && vf[k - 1] < vf[k + 1]) {
|
||||||
|
vf[k + 1]
|
||||||
|
} else {
|
||||||
|
vf[k - 1] + 1
|
||||||
|
};
|
||||||
|
let y = (x as isize - k) as usize;
|
||||||
|
|
||||||
|
// The coordinate of the start of a snake
|
||||||
|
let (x0, y0) = (x, y);
|
||||||
|
// While these sequences are identical, keep moving through the
|
||||||
|
// graph with no cost
|
||||||
|
if x < old_range.len() && y < new_range.len() {
|
||||||
|
let advance = common_prefix_len(
|
||||||
|
old,
|
||||||
|
old_range.start + x..old_range.end,
|
||||||
|
new,
|
||||||
|
new_range.start + y..new_range.end,
|
||||||
|
);
|
||||||
|
x += advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the new best x value
|
||||||
|
vf[k] = x;
|
||||||
|
|
||||||
|
// Only check for connections from the forward search when N - M is
|
||||||
|
// odd and when there is a reciprocal k line coming from the other
|
||||||
|
// direction.
|
||||||
|
if odd && (k - delta).abs() <= (d - 1) {
|
||||||
|
// TODO optimize this so we don't have to compare against n
|
||||||
|
if vf[k] + vb[-(k - delta)] >= n {
|
||||||
|
// Return the snake
|
||||||
|
return Some((x0 + old_range.start, y0 + new_range.start));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backward path
|
||||||
|
for k in (-d..=d).rev().step_by(2) {
|
||||||
|
let mut x = if k == -d || (k != d && vb[k - 1] < vb[k + 1]) {
|
||||||
|
vb[k + 1]
|
||||||
|
} else {
|
||||||
|
vb[k - 1] + 1
|
||||||
|
};
|
||||||
|
let mut y = (x as isize - k) as usize;
|
||||||
|
|
||||||
|
// The coordinate of the start of a snake
|
||||||
|
if x < n && y < m {
|
||||||
|
let advance = common_suffix_len(
|
||||||
|
old,
|
||||||
|
old_range.start..old_range.start + n - x,
|
||||||
|
new,
|
||||||
|
new_range.start..new_range.start + m - y,
|
||||||
|
);
|
||||||
|
x += advance;
|
||||||
|
y += advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the new best x value
|
||||||
|
vb[k] = x;
|
||||||
|
|
||||||
|
if !odd && (k - delta).abs() <= d {
|
||||||
|
// TODO optimize this so we don't have to compare against n
|
||||||
|
if vb[k] + vf[-(k - delta)] >= n {
|
||||||
|
// Return the snake
|
||||||
|
return Some((n - x + old_range.start, m - y + new_range.start));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Maybe there's an opportunity to optimize and bail early?
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conquer<T>(
|
||||||
|
old: &[Token<T>],
|
||||||
|
mut old_range: Range<usize>,
|
||||||
|
new: &[Token<T>],
|
||||||
|
mut new_range: Range<usize>,
|
||||||
|
vf: &mut V,
|
||||||
|
vb: &mut V,
|
||||||
|
result: &mut Vec<RawOperation<T>>,
|
||||||
|
) where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
// Check for common prefix
|
||||||
|
let common_prefix_len = common_prefix_len(old, old_range.clone(), new, new_range.clone());
|
||||||
|
if common_prefix_len > 0 {
|
||||||
|
result.extend(
|
||||||
|
old[old_range.start..old_range.start + common_prefix_len]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Equal(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
old_range.start += common_prefix_len;
|
||||||
|
new_range.start += common_prefix_len;
|
||||||
|
|
||||||
|
// Check for common suffix
|
||||||
|
let common_suffix_len = common_suffix_len(old, old_range.clone(), new, new_range.clone());
|
||||||
|
let common_suffix = (
|
||||||
|
old_range.end - common_suffix_len,
|
||||||
|
new_range.end - common_suffix_len,
|
||||||
|
);
|
||||||
|
old_range.end -= common_suffix_len;
|
||||||
|
new_range.end -= common_suffix_len;
|
||||||
|
|
||||||
|
if old_range.is_empty() && new_range.is_empty() {
|
||||||
|
// do nothing
|
||||||
|
} else if new_range.is_empty() {
|
||||||
|
result.extend(
|
||||||
|
old[old_range.start..old_range.start + old_range.len()]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Delete(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
} else if old_range.is_empty() {
|
||||||
|
result.extend(
|
||||||
|
new[new_range.start..new_range.start + new_range.len()]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Insert(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
} else if let Some((x_start, y_start)) =
|
||||||
|
find_middle_snake(old, old_range.clone(), new, new_range.clone(), vf, vb)
|
||||||
|
{
|
||||||
|
let (old_a, old_b) = split_at(old_range, x_start);
|
||||||
|
let (new_a, new_b) = split_at(new_range, y_start);
|
||||||
|
conquer(old, old_a, new, new_a, vf, vb, result);
|
||||||
|
conquer(old, old_b, new, new_b, vf, vb, result);
|
||||||
|
} else {
|
||||||
|
result.extend(
|
||||||
|
old[old_range.start..old_range.end]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Delete(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
result.extend(
|
||||||
|
new[new_range.start..new_range.end]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Insert(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if common_suffix_len > 0 {
|
||||||
|
result.extend(
|
||||||
|
old[common_suffix.0..common_suffix.0 + common_suffix_len]
|
||||||
|
.iter()
|
||||||
|
.map(|token| RawOperation::Equal(vec![token.clone()])),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty_diff() {
|
||||||
|
let old: Vec<Token<String>> = vec![];
|
||||||
|
let new: Vec<Token<String>> = vec![];
|
||||||
|
let result = diff(&old, &new);
|
||||||
|
assert_eq!(result.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_identical_content() {
|
||||||
|
let content = vec!["a".into(), "b".into(), "c".into()];
|
||||||
|
let result = diff(&content, &content);
|
||||||
|
assert_debug_snapshot!(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_insert_only() {
|
||||||
|
let old: Vec<Token<String>> = vec![];
|
||||||
|
let new: Vec<Token<String>> = vec!["a".into(), "b".into()];
|
||||||
|
let result = diff(&old, &new);
|
||||||
|
assert_debug_snapshot!(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_only() {
|
||||||
|
let old = vec!["a".into(), "b".into()];
|
||||||
|
let new: Vec<Token<String>> = vec![];
|
||||||
|
let result = diff(&old, &new);
|
||||||
|
assert_debug_snapshot!(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_prefix_and_suffix() {
|
||||||
|
let old = vec!["a".into(), "b".into(), "c".into(), "d".into()];
|
||||||
|
let new = vec!["a".into(), "x".into(), "d".into()];
|
||||||
|
let result = diff(&old, &new);
|
||||||
|
assert_debug_snapshot!(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_complex_diff() {
|
||||||
|
let old = vec!["a".into(), "b".into(), "c".into(), "d".into()];
|
||||||
|
let new = vec!["a".into(), "x".into(), "c".into(), "y".into()];
|
||||||
|
let result = diff(&old, &new);
|
||||||
|
assert_debug_snapshot!(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/diffs/raw_operation.rs
Normal file
64
src/diffs/raw_operation.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
use crate::tokenizer::token::Token;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum RawOperation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
Insert(Vec<Token<T>>),
|
||||||
|
Delete(Vec<Token<T>>),
|
||||||
|
Equal(Vec<Token<T>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> RawOperation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
pub fn tokens(&self) -> &Vec<Token<T>> {
|
||||||
|
match self {
|
||||||
|
RawOperation::Insert(tokens)
|
||||||
|
| RawOperation::Delete(tokens)
|
||||||
|
| RawOperation::Equal(tokens) => tokens,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn original_text_length(&self) -> usize {
|
||||||
|
self.tokens().iter().map(Token::get_original_length).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_original_text(self) -> String { self.tokens().iter().map(Token::original).collect() }
|
||||||
|
|
||||||
|
pub fn is_left_joinable(&self) -> bool {
|
||||||
|
let first_token = self.tokens().first();
|
||||||
|
first_token.is_none_or(|token| token.is_left_joinable)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_right_joinable(&self) -> bool {
|
||||||
|
let last_token = self.tokens().last();
|
||||||
|
last_token.is_none_or(|token| token.is_right_joinable)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extends the operation with another operation. Only operations of the
|
||||||
|
/// same type as self can be used to extend self, otherwise the function
|
||||||
|
/// will panic.
|
||||||
|
pub fn extend(self, other: RawOperation<T>) -> RawOperation<T> {
|
||||||
|
debug_assert!(
|
||||||
|
std::mem::discriminant(&self) == std::mem::discriminant(&other),
|
||||||
|
"Cannot extend operations of different types. This should have been handled before \
|
||||||
|
calling this function."
|
||||||
|
);
|
||||||
|
|
||||||
|
match (self, other) {
|
||||||
|
(RawOperation::Insert(self_tokens), RawOperation::Insert(other_tokens)) => {
|
||||||
|
RawOperation::Insert(self_tokens.into_iter().chain(other_tokens).collect())
|
||||||
|
}
|
||||||
|
(RawOperation::Delete(tokens1), RawOperation::Delete(tokens2)) => {
|
||||||
|
RawOperation::Delete(tokens1.into_iter().chain(tokens2).collect())
|
||||||
|
}
|
||||||
|
(RawOperation::Equal(tokens1), RawOperation::Equal(tokens2)) => {
|
||||||
|
RawOperation::Equal(tokens1.into_iter().chain(tokens2).collect())
|
||||||
|
}
|
||||||
|
_ => unreachable!("Only operations of the same type can be extended"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/diffs/myers.rs
|
||||||
|
expression: result
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "a",
|
||||||
|
original: "a",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Insert(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "x",
|
||||||
|
original: "x",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "b",
|
||||||
|
original: "b",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "c",
|
||||||
|
original: "c",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Insert(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "y",
|
||||||
|
original: "y",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "d",
|
||||||
|
original: "d",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/diffs/myers.rs
|
||||||
|
expression: result
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "a",
|
||||||
|
original: "a",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "b",
|
||||||
|
original: "b",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/diffs/myers.rs
|
||||||
|
expression: result
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "a",
|
||||||
|
original: "a",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "b",
|
||||||
|
original: "b",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "c",
|
||||||
|
original: "c",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/diffs/myers.rs
|
||||||
|
expression: result
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Insert(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "a",
|
||||||
|
original: "a",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Insert(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "b",
|
||||||
|
original: "b",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/diffs/myers.rs
|
||||||
|
expression: result
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "a",
|
||||||
|
original: "a",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "b",
|
||||||
|
original: "b",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Delete(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "c",
|
||||||
|
original: "c",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Insert(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "x",
|
||||||
|
original: "x",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Equal(
|
||||||
|
[
|
||||||
|
Token {
|
||||||
|
normalised: "d",
|
||||||
|
original: "d",
|
||||||
|
is_left_joinable: true,
|
||||||
|
is_right_joinable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
13
src/lib.rs
Normal file
13
src/lib.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
mod diffs;
|
||||||
|
mod operation_transformation;
|
||||||
|
mod tokenizer;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
pub use operation_transformation::{
|
||||||
|
CursorPosition, EditedText, TextWithCursors, reconcile, reconcile_with_cursors,
|
||||||
|
reconcile_with_tokenizer,
|
||||||
|
};
|
||||||
|
pub use tokenizer::{Tokenizer, token::Token, word_tokenizer::word_tokenizer};
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
pub mod wasm;
|
||||||
167
src/operation_transformation.rs
Normal file
167
src/operation_transformation.rs
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
mod cursor;
|
||||||
|
mod edited_text;
|
||||||
|
mod merge_context;
|
||||||
|
mod operation;
|
||||||
|
mod ordered_operation;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
pub use cursor::{CursorPosition, TextWithCursors};
|
||||||
|
pub use edited_text::EditedText;
|
||||||
|
pub use operation::Operation;
|
||||||
|
|
||||||
|
use crate::Tokenizer;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn reconcile(original: &str, left: &str, right: &str) -> String {
|
||||||
|
reconcile_with_cursors(original, left.into(), right.into())
|
||||||
|
.text
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn reconcile_with_cursors<'a>(
|
||||||
|
original: &'a str,
|
||||||
|
left: TextWithCursors<'a>,
|
||||||
|
right: TextWithCursors<'a>,
|
||||||
|
) -> TextWithCursors<'static> {
|
||||||
|
let left_operations = EditedText::from_strings(original, left);
|
||||||
|
let right_operations = EditedText::from_strings(original, right);
|
||||||
|
|
||||||
|
let merged_operations = left_operations.merge(right_operations);
|
||||||
|
|
||||||
|
TextWithCursors::new_owned(merged_operations.apply(), merged_operations.cursors)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn reconcile_with_tokenizer<'a, F, T>(
|
||||||
|
original: &str,
|
||||||
|
left: TextWithCursors<'a>,
|
||||||
|
right: TextWithCursors<'a>,
|
||||||
|
tokenizer: &Tokenizer<T>,
|
||||||
|
) -> TextWithCursors<'static>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let left_operations = EditedText::from_strings_with_tokenizer(original, left, tokenizer);
|
||||||
|
let right_operations = EditedText::from_strings_with_tokenizer(original, right, tokenizer);
|
||||||
|
|
||||||
|
let merged_operations = left_operations.merge(right_operations);
|
||||||
|
|
||||||
|
TextWithCursors::new_owned(merged_operations.apply(), merged_operations.cursors)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use std::{fs, ops::Range, path::Path};
|
||||||
|
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use test_case::test_matrix;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::CursorPosition;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cursor_complex() {
|
||||||
|
let original = "this is some complex text to test cursor positions";
|
||||||
|
let left = TextWithCursors::new(
|
||||||
|
"this is really complex text for testing cursor positions",
|
||||||
|
vec![
|
||||||
|
CursorPosition {
|
||||||
|
id: 0,
|
||||||
|
char_index: 8,
|
||||||
|
}, // after "this is "
|
||||||
|
CursorPosition {
|
||||||
|
id: 1,
|
||||||
|
char_index: 22,
|
||||||
|
}, // after "this is really complex text"
|
||||||
|
],
|
||||||
|
);
|
||||||
|
let right = TextWithCursors::new(
|
||||||
|
"that was some complex sample to test cursor movements",
|
||||||
|
vec![
|
||||||
|
CursorPosition {
|
||||||
|
id: 2,
|
||||||
|
char_index: 5,
|
||||||
|
}, // after "that "
|
||||||
|
CursorPosition {
|
||||||
|
id: 3,
|
||||||
|
char_index: 29,
|
||||||
|
}, // after "some complex sample "
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let merged = reconcile_with_cursors(original, left, right);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
merged,
|
||||||
|
TextWithCursors::new(
|
||||||
|
"that was really complex sample for testing cursor movements",
|
||||||
|
vec![
|
||||||
|
CursorPosition {
|
||||||
|
id: 2,
|
||||||
|
char_index: 5
|
||||||
|
}, // unchanged
|
||||||
|
CursorPosition {
|
||||||
|
id: 0,
|
||||||
|
char_index: 9
|
||||||
|
}, // before "really"
|
||||||
|
CursorPosition {
|
||||||
|
id: 1,
|
||||||
|
char_index: 23
|
||||||
|
}, // inside of "s|ample" because "text" got replaced by "sample"
|
||||||
|
CursorPosition {
|
||||||
|
id: 3,
|
||||||
|
char_index: 43
|
||||||
|
}, // before "cursor movements"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[ignore = "expensive to run, only run in CI"]
|
||||||
|
#[test_matrix( [
|
||||||
|
"pride_and_prejudice.txt",
|
||||||
|
"room_with_a_view.txt",
|
||||||
|
"kun_lu.txt",
|
||||||
|
"blns.txt"
|
||||||
|
], [
|
||||||
|
"pride_and_prejudice.txt",
|
||||||
|
"room_with_a_view.txt",
|
||||||
|
"kun_lu.txt",
|
||||||
|
"blns.txt"
|
||||||
|
], [
|
||||||
|
"pride_and_prejudice.txt",
|
||||||
|
"room_with_a_view.txt",
|
||||||
|
"kun_lu.txt",
|
||||||
|
"blns.txt"
|
||||||
|
], [0..10000, 10000..20000], [0..10000, 10000..20000], [0..10000, 10000..20000])]
|
||||||
|
fn test_merge_files_without_panic(
|
||||||
|
file_name_1: &str,
|
||||||
|
file_name_2: &str,
|
||||||
|
file_name_3: &str,
|
||||||
|
range_1: Range<usize>,
|
||||||
|
range_2: Range<usize>,
|
||||||
|
range_3: Range<usize>,
|
||||||
|
) {
|
||||||
|
let files = [file_name_1, file_name_2, file_name_3];
|
||||||
|
let permutations = [range_1, range_2, range_3];
|
||||||
|
|
||||||
|
let root = Path::new("tests/resources/");
|
||||||
|
|
||||||
|
let contents = files
|
||||||
|
.iter()
|
||||||
|
.zip(permutations.iter())
|
||||||
|
.map(|(file, range)| {
|
||||||
|
let path = root.join(file);
|
||||||
|
fs::read_to_string(&path)
|
||||||
|
.unwrap()
|
||||||
|
.chars()
|
||||||
|
.skip(range.start)
|
||||||
|
.take(range.end)
|
||||||
|
.collect::<String>()
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let _ = reconcile(&contents[0], &contents[1], &contents[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/operation_transformation/cursor.rs
Normal file
57
src/operation_transformation/cursor.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
// CursorPosition represents the position of an identifiable cursor in a text
|
||||||
|
// document based on its (UTF-8) character index.
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
|
pub struct CursorPosition {
|
||||||
|
pub id: usize,
|
||||||
|
pub char_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CursorPosition {
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_index(&self, index: usize) -> Self {
|
||||||
|
CursorPosition {
|
||||||
|
id: self.id,
|
||||||
|
char_index: index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
|
pub struct TextWithCursors<'a> {
|
||||||
|
pub text: Cow<'a, str>,
|
||||||
|
pub cursors: Vec<CursorPosition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextWithCursors<'a> {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(text: &'a str, cursors: Vec<CursorPosition>) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn new_owned(text: String, cursors: Vec<CursorPosition>) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for TextWithCursors<'a> {
|
||||||
|
fn from(text: &'a str) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
277
src/operation_transformation/edited_text.rs
Normal file
277
src/operation_transformation/edited_text.rs
Normal file
|
|
@ -0,0 +1,277 @@
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::{CursorPosition, Operation, TextWithCursors, ordered_operation::OrderedOperation};
|
||||||
|
use crate::{
|
||||||
|
diffs::{myers::diff, raw_operation::RawOperation},
|
||||||
|
operation_transformation::{
|
||||||
|
merge_context::MergeContext,
|
||||||
|
utils::{cook_operations::cook_operations, elongate_operations::elongate_operations},
|
||||||
|
},
|
||||||
|
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
|
||||||
|
utils::{side::Side, string_builder::StringBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A text document and a sequence of operations that can be applied to the text
|
||||||
|
/// document. `EditedText` supports merging two sequences of operations using
|
||||||
|
/// the principles of Operational Transformation.
|
||||||
|
///
|
||||||
|
/// It's mainly created through the `from_strings` method, then merged with
|
||||||
|
/// another `EditedText` derived from the same original text and then applied to
|
||||||
|
/// the original text to get the reconciled text of concurrent edits.
|
||||||
|
///
|
||||||
|
/// In addition to text and operations, it also keeps track of cursor positions
|
||||||
|
/// in the original text. The cursor positions are updated when the operations
|
||||||
|
/// are applied, so that the cursor positions can be used to restore the
|
||||||
|
/// cursor positions in the updated text.
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
|
pub struct EditedText<'a, T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
text: &'a str,
|
||||||
|
operations: Vec<OrderedOperation<T>>,
|
||||||
|
pub(crate) cursors: Vec<CursorPosition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> EditedText<'a, String> {
|
||||||
|
/// Create an `EditedText` from the given original (old) and updated (new)
|
||||||
|
/// strings. The returned `EditedText` represents the changes from the
|
||||||
|
/// original to the updated text. When the return value is applied to
|
||||||
|
/// the original text, it will result in the updated text. The default
|
||||||
|
/// word tokenizer is used to tokenize the text which splits the text on
|
||||||
|
/// whitespaces.
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_strings(original: &'a str, updated: TextWithCursors<'a>) -> Self {
|
||||||
|
Self::from_strings_with_tokenizer(original, updated, &word_tokenizer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> EditedText<'a, T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
/// Create an `EditedText` from the given original (old) and updated (new)
|
||||||
|
/// strings. The returned `EditedText` represents the changes from the
|
||||||
|
/// original to the updated text. When the return value is applied to
|
||||||
|
/// the original text, it will result in the updated text. The tokenizer
|
||||||
|
/// function is used to tokenize the text.
|
||||||
|
pub fn from_strings_with_tokenizer(
|
||||||
|
original: &'a str,
|
||||||
|
updated: TextWithCursors<'a>,
|
||||||
|
tokenizer: &Tokenizer<T>,
|
||||||
|
) -> Self {
|
||||||
|
let original_tokens = (tokenizer)(original);
|
||||||
|
let updated_tokens = (tokenizer)(&updated.text);
|
||||||
|
|
||||||
|
let diff: Vec<RawOperation<T>> = diff(&original_tokens, &updated_tokens);
|
||||||
|
|
||||||
|
Self::new(
|
||||||
|
original,
|
||||||
|
cook_operations(elongate_operations(diff)).collect(),
|
||||||
|
updated.cursors,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new `EditedText` with the given operations.
|
||||||
|
/// The operations must be in the order in which they are meant to be
|
||||||
|
/// applied. The operations must not overlap.
|
||||||
|
fn new(
|
||||||
|
text: &'a str,
|
||||||
|
operations: Vec<OrderedOperation<T>>,
|
||||||
|
mut cursors: Vec<CursorPosition>,
|
||||||
|
) -> Self {
|
||||||
|
operations
|
||||||
|
.iter()
|
||||||
|
.zip(operations.iter().skip(1))
|
||||||
|
.for_each(|(previous, next)| {
|
||||||
|
debug_assert!(
|
||||||
|
previous.operation.start_index() <= next.operation.start_index(),
|
||||||
|
"{} must not come before {} yet it does",
|
||||||
|
previous.operation,
|
||||||
|
next.operation
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
cursors.sort_by_key(|cursor| cursor.char_index);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
text,
|
||||||
|
operations,
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn merge(self, other: Self) -> Self {
|
||||||
|
debug_assert_eq!(
|
||||||
|
self.text, other.text,
|
||||||
|
"`EditedText`-s must be derived from the same text to be mergable"
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut left_merge_context = MergeContext::default();
|
||||||
|
let mut right_merge_context = MergeContext::default();
|
||||||
|
|
||||||
|
let mut merged_cursors = Vec::with_capacity(self.cursors.len() + other.cursors.len());
|
||||||
|
let mut left_cursors = self.cursors.into_iter().peekable();
|
||||||
|
let mut right_cursors = other.cursors.into_iter().peekable();
|
||||||
|
|
||||||
|
let mut merged_operations: Vec<OrderedOperation<T>> =
|
||||||
|
Vec::with_capacity(self.operations.len() + other.operations.len());
|
||||||
|
|
||||||
|
let mut left_iter = self.operations.into_iter();
|
||||||
|
let mut right_iter = other.operations.into_iter();
|
||||||
|
|
||||||
|
let mut maybe_left_op = left_iter.next();
|
||||||
|
let mut maybe_right_op = right_iter.next();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let (side, OrderedOperation { operation, order }) =
|
||||||
|
match (maybe_left_op.clone(), maybe_right_op.clone()) {
|
||||||
|
(Some(left_op), Some(right_op)) => {
|
||||||
|
if left_op < right_op {
|
||||||
|
(Side::Left, left_op)
|
||||||
|
} else {
|
||||||
|
(Side::Right, right_op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(Some(left_op), None) => (Side::Left, left_op),
|
||||||
|
(None, Some(right_op)) => (Side::Right, right_op),
|
||||||
|
(None, None) => break,
|
||||||
|
};
|
||||||
|
|
||||||
|
if side == Side::Left {
|
||||||
|
maybe_left_op = left_iter.next();
|
||||||
|
} else {
|
||||||
|
maybe_right_op = right_iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
let original_start = operation.start_index() as i64;
|
||||||
|
let original_end = operation.end_index();
|
||||||
|
let original_length = operation.len() as i64;
|
||||||
|
|
||||||
|
let result = match side {
|
||||||
|
Side::Left => operation.merge_operations_with_context(
|
||||||
|
&mut right_merge_context,
|
||||||
|
&mut left_merge_context,
|
||||||
|
),
|
||||||
|
Side::Right => operation.merge_operations_with_context(
|
||||||
|
&mut left_merge_context,
|
||||||
|
&mut right_merge_context,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = result {
|
||||||
|
let shift =
|
||||||
|
op.start_index() as i64 - original_start + op.len() as i64 - original_length;
|
||||||
|
match side {
|
||||||
|
Side::Left => {
|
||||||
|
while let Some(cursor) =
|
||||||
|
left_cursors.next_if(|cursor| cursor.char_index <= original_end + 1)
|
||||||
|
{
|
||||||
|
merged_cursors.push(cursor.with_index(
|
||||||
|
(op.start_index() as i64).max(cursor.char_index as i64 + shift)
|
||||||
|
as usize,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Side::Right => {
|
||||||
|
while let Some(cursor) =
|
||||||
|
right_cursors.next_if(|cursor| cursor.char_index <= original_end + 1)
|
||||||
|
{
|
||||||
|
merged_cursors.push(cursor.with_index(
|
||||||
|
(op.start_index() as i64).max(cursor.char_index as i64 + shift)
|
||||||
|
as usize,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
merged_operations.extend(result.into_iter().map(|op| OrderedOperation {
|
||||||
|
order,
|
||||||
|
operation: op,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let last_index = merged_operations
|
||||||
|
.iter()
|
||||||
|
.filter(|operation| {
|
||||||
|
matches!(
|
||||||
|
operation.operation,
|
||||||
|
Operation::Insert { .. } | Operation::Equal { .. }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.next_back()
|
||||||
|
.map_or(0, |op| op.operation.end_index());
|
||||||
|
|
||||||
|
for cursor in left_cursors.chain(right_cursors) {
|
||||||
|
merged_cursors.push(cursor.with_index(last_index));
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::new(self.text, merged_operations, merged_cursors)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Apply the operations to the text and return the resulting text.
|
||||||
|
#[must_use]
|
||||||
|
pub fn apply(&self) -> String {
|
||||||
|
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
|
||||||
|
|
||||||
|
for OrderedOperation { operation, .. } in &self.operations {
|
||||||
|
builder = operation.apply(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_calculate_operations() {
|
||||||
|
let left = "hello world! How are you? Adam";
|
||||||
|
let right = "Hello, my friend! How are you doing? Albert";
|
||||||
|
|
||||||
|
let operations = EditedText::from_strings(left, right.into());
|
||||||
|
|
||||||
|
insta::assert_debug_snapshot!(operations);
|
||||||
|
|
||||||
|
let new_right = operations.apply();
|
||||||
|
assert_eq!(new_right.to_string(), right);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_calculate_operations_with_no_diff() {
|
||||||
|
let text = "hello world!";
|
||||||
|
|
||||||
|
let operations = EditedText::from_strings(text, text.into());
|
||||||
|
|
||||||
|
assert_debug_snapshot!(operations);
|
||||||
|
|
||||||
|
let new_right = operations.apply();
|
||||||
|
assert_eq!(new_right.to_string(), text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_calculate_operations_with_insert() {
|
||||||
|
let original = "hello world! ...";
|
||||||
|
let left = "Hello world! I'm Andras.";
|
||||||
|
let right = "Hello world! How are you?";
|
||||||
|
let expected = "Hello world! How are you? I'm Andras.";
|
||||||
|
|
||||||
|
let operations_1 = EditedText::from_strings(original, left.into());
|
||||||
|
let operations_2 = EditedText::from_strings(original, right.into());
|
||||||
|
|
||||||
|
let operations = operations_1.merge(operations_2);
|
||||||
|
assert_eq!(operations.apply(), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/operation_transformation/merge_context.rs
Normal file
73
src/operation_transformation/merge_context.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
use core::fmt::Debug;
|
||||||
|
|
||||||
|
use crate::operation_transformation::Operation;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct MergeContext<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
last_operation: Option<Operation<T>>,
|
||||||
|
pub shift: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for MergeContext<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn default() -> Self {
|
||||||
|
MergeContext {
|
||||||
|
last_operation: None,
|
||||||
|
shift: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> MergeContext<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
pub fn last_operation(&self) -> Option<&Operation<T>> { self.last_operation.as_ref() }
|
||||||
|
|
||||||
|
pub fn replace_last_operation(&mut self, operation: Option<Operation<T>>) {
|
||||||
|
self.last_operation = operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace the last delete operation (if there was one) with a new one
|
||||||
|
/// while applying it to the `shift` in case the last operation
|
||||||
|
/// was a delete.
|
||||||
|
pub fn consume_and_replace_last_operation(&mut self, operation: Option<Operation<T>>) {
|
||||||
|
if let Some(Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
..
|
||||||
|
}) = self.last_operation.take()
|
||||||
|
{
|
||||||
|
self.shift -= deleted_character_count as i64;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.last_operation = operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove the last operation (if there was one) in case it is behind the
|
||||||
|
/// threshold operation. This updates the `shift` in case the last operation
|
||||||
|
/// was a delete.
|
||||||
|
pub fn consume_last_operation_if_it_is_too_behind(&mut self, threshold_index: i64) {
|
||||||
|
if let Some(last_operation) = self.last_operation.as_ref() {
|
||||||
|
if let Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
..
|
||||||
|
} = last_operation
|
||||||
|
{
|
||||||
|
if threshold_index + self.shift > last_operation.end_index() as i64 {
|
||||||
|
self.shift -= *deleted_character_count as i64;
|
||||||
|
self.last_operation = None;
|
||||||
|
}
|
||||||
|
} else if let Operation::Insert { .. } = last_operation
|
||||||
|
&& threshold_index + self.shift - last_operation.len() as i64
|
||||||
|
> last_operation.end_index() as i64
|
||||||
|
{
|
||||||
|
self.last_operation = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
513
src/operation_transformation/operation.rs
Normal file
513
src/operation_transformation/operation.rs
Normal file
|
|
@ -0,0 +1,513 @@
|
||||||
|
use core::fmt::{Debug, Display};
|
||||||
|
use std::ops::Range;
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::merge_context::MergeContext;
|
||||||
|
use crate::{
|
||||||
|
Token,
|
||||||
|
utils::{
|
||||||
|
find_longest_prefix_contained_within::find_longest_prefix_contained_within,
|
||||||
|
string_builder::StringBuilder,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Represents a change that can be applied on a `StringBuilder`.
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub enum Operation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
Equal {
|
||||||
|
index: usize,
|
||||||
|
length: usize,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text: Option<String>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Insert {
|
||||||
|
index: usize,
|
||||||
|
text: Vec<Token<T>>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Delete {
|
||||||
|
index: usize,
|
||||||
|
deleted_character_count: usize,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text: Option<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Operation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
/// Creates an equal operation with the given index.
|
||||||
|
/// This operation is used to indicate that the text at the given index
|
||||||
|
/// is unchanged.
|
||||||
|
pub fn create_equal(index: usize, length: usize) -> Option<Self> {
|
||||||
|
if length == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Operation::Equal {
|
||||||
|
index,
|
||||||
|
length,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_equal_with_text(index: usize, text: String) -> Option<Self> {
|
||||||
|
if text.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Operation::Equal {
|
||||||
|
index,
|
||||||
|
length: text.chars().count(),
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text: Some(text),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an insert operation with the given index and text.
|
||||||
|
/// If the text is empty (meaning that the operation would be a no-op),
|
||||||
|
/// returns None.
|
||||||
|
pub fn create_insert(index: usize, text: Vec<Token<T>>) -> Option<Self> {
|
||||||
|
if text.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Operation::Insert { index, text })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a delete operation with the given index and number of
|
||||||
|
/// to-be-deleted characters. If the operation would delete 0 (meaning
|
||||||
|
/// that the operation would be a no-op), returns None.
|
||||||
|
pub fn create_delete(index: usize, deleted_character_count: usize) -> Option<Self> {
|
||||||
|
if deleted_character_count == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Operation::Delete {
|
||||||
|
index,
|
||||||
|
deleted_character_count,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_delete_with_text(index: usize, text: String) -> Option<Self> {
|
||||||
|
if text.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Operation::Delete {
|
||||||
|
index,
|
||||||
|
deleted_character_count: text.chars().count(),
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text: Some(text),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies the operation to the given `StringBuilder`, returning the
|
||||||
|
/// modified `StringBuilder`.
|
||||||
|
///
|
||||||
|
/// When compiled in debug mode, panics if a delete operation is attempted
|
||||||
|
/// on a range of text that does not match the text to be deleted.
|
||||||
|
pub fn apply<'a>(&self, mut builder: StringBuilder<'a>) -> StringBuilder<'a> {
|
||||||
|
match self {
|
||||||
|
Operation::Equal {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
debug_assert!(
|
||||||
|
text.as_ref()
|
||||||
|
.is_none_or(|text| builder.get_slice(self.range()) == *text),
|
||||||
|
"Text which is supposed to be equal does not match the text in the range"
|
||||||
|
);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
Operation::Insert { text, .. } => builder.insert(
|
||||||
|
self.start_index(),
|
||||||
|
&text.iter().map(Token::original).collect::<String>(),
|
||||||
|
),
|
||||||
|
Operation::Delete {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
debug_assert!(
|
||||||
|
deleted_text
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|text| builder.get_slice(self.range()) == *text),
|
||||||
|
"Text to delete does not match the text in the range"
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.delete(self.range());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the index of the first character that the operation affects.
|
||||||
|
pub fn start_index(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Operation::Equal { index, .. }
|
||||||
|
| Operation::Insert { index, .. }
|
||||||
|
| Operation::Delete { index, .. } => *index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the index of the last character that the operation affects.
|
||||||
|
pub fn end_index(&self) -> usize {
|
||||||
|
debug_assert!(
|
||||||
|
self.len() > 0,
|
||||||
|
" len() must be greater than 0 because operations must be non-empty"
|
||||||
|
);
|
||||||
|
self.start_index() + self.len() - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the range of indices of characters that the operation affects.
|
||||||
|
#[allow(clippy::range_plus_one)]
|
||||||
|
pub fn range(&self) -> Range<usize> { self.start_index()..self.end_index() + 1 }
|
||||||
|
|
||||||
|
/// Returns the number of affected characters. It is always greater than 0
|
||||||
|
/// because empty operations cannot be created.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Operation::Equal { length, .. } => *length,
|
||||||
|
Operation::Insert { text, .. } => text.iter().map(Token::get_original_length).sum(),
|
||||||
|
Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
..
|
||||||
|
} => *deleted_character_count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new operation with the same type and text but with the given
|
||||||
|
/// index.
|
||||||
|
pub fn with_index(self, index: usize) -> Self {
|
||||||
|
match self {
|
||||||
|
Operation::Equal {
|
||||||
|
length,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text,
|
||||||
|
..
|
||||||
|
} => Operation::Equal {
|
||||||
|
index,
|
||||||
|
length,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text,
|
||||||
|
},
|
||||||
|
Operation::Insert { text, .. } => Operation::Insert { index, text },
|
||||||
|
Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text,
|
||||||
|
..
|
||||||
|
} => Operation::Delete {
|
||||||
|
index,
|
||||||
|
deleted_character_count,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new operation with the same type and text but with the index
|
||||||
|
/// shifted by the given offset. The offset can be negative but the
|
||||||
|
/// resulting index must be non-negative.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// In debug mode, panics if the resulting index is negative.
|
||||||
|
pub fn with_shifted_index(self, offset: i64) -> Self {
|
||||||
|
let index = self.start_index() as i64 + offset;
|
||||||
|
debug_assert!(index >= 0, "Shifted index must be non-negative");
|
||||||
|
|
||||||
|
self.with_index(index as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Merges the operation with the given context, producing a new operation
|
||||||
|
/// and updating the context. This implements a comples FSM that handles
|
||||||
|
/// the merging of operations in a way that is consistent with the text.
|
||||||
|
/// The contexts are updated in-place.
|
||||||
|
#[allow(clippy::too_many_lines)]
|
||||||
|
pub fn merge_operations_with_context(
|
||||||
|
self,
|
||||||
|
affecting_context: &mut MergeContext<T>,
|
||||||
|
produced_context: &mut MergeContext<T>,
|
||||||
|
) -> Option<Operation<T>> {
|
||||||
|
affecting_context.consume_last_operation_if_it_is_too_behind(self.start_index() as i64);
|
||||||
|
let operation = self.with_shifted_index(affecting_context.shift);
|
||||||
|
|
||||||
|
match (operation, affecting_context.last_operation()) {
|
||||||
|
(operation @ Operation::Insert { .. }, None | Some(Operation::Equal { .. })) => {
|
||||||
|
produced_context.shift += operation.len() as i64;
|
||||||
|
produced_context.consume_and_replace_last_operation(Some(operation.clone()));
|
||||||
|
Some(operation)
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
Operation::Insert { text, index },
|
||||||
|
Some(Operation::Insert {
|
||||||
|
text: previous_inserted_text,
|
||||||
|
..
|
||||||
|
}),
|
||||||
|
) => {
|
||||||
|
// In case the current insert's prefix appears in the previously inserted text,
|
||||||
|
// we can trim the current insert to only include the non-overlapping part.
|
||||||
|
// This way, we don't end up duplicating text.
|
||||||
|
let offset_in_tokens =
|
||||||
|
find_longest_prefix_contained_within(previous_inserted_text, &text);
|
||||||
|
let offset_in_length = text
|
||||||
|
.iter()
|
||||||
|
.take(offset_in_tokens)
|
||||||
|
.map(Token::get_original_length)
|
||||||
|
.sum::<usize>();
|
||||||
|
let trimmed_operation =
|
||||||
|
Operation::create_insert(index, text[offset_in_tokens..].to_vec());
|
||||||
|
|
||||||
|
affecting_context.shift -= offset_in_length as i64;
|
||||||
|
produced_context.shift += trimmed_operation
|
||||||
|
.as_ref()
|
||||||
|
.map(Operation::len)
|
||||||
|
.unwrap_or_default() as i64;
|
||||||
|
produced_context.consume_and_replace_last_operation(trimmed_operation.clone());
|
||||||
|
|
||||||
|
trimmed_operation
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
operation @ Operation::Delete { .. },
|
||||||
|
None | Some(Operation::Insert { .. } | Operation::Equal { .. }),
|
||||||
|
) => {
|
||||||
|
produced_context.consume_and_replace_last_operation(Some(operation.clone()));
|
||||||
|
Some(operation)
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
operation @ Operation::Insert { .. },
|
||||||
|
Some(last_delete @ Operation::Delete { .. }),
|
||||||
|
) => {
|
||||||
|
produced_context.shift += operation.len() as i64;
|
||||||
|
|
||||||
|
debug_assert!(
|
||||||
|
last_delete.range().contains(&operation.start_index()),
|
||||||
|
"There is a last delete ({last_delete}) but the operation ({operation}) is \
|
||||||
|
not contained in it"
|
||||||
|
);
|
||||||
|
|
||||||
|
let difference = operation.start_index() as i64 - last_delete.start_index() as i64;
|
||||||
|
|
||||||
|
let moved_operation = operation.with_index(last_delete.start_index());
|
||||||
|
|
||||||
|
affecting_context.replace_last_operation(Operation::create_delete(
|
||||||
|
moved_operation.end_index() + 1,
|
||||||
|
(last_delete.len() as i64 - difference) as usize,
|
||||||
|
));
|
||||||
|
affecting_context.shift -= difference;
|
||||||
|
|
||||||
|
produced_context.consume_and_replace_last_operation(Some(moved_operation.clone()));
|
||||||
|
|
||||||
|
Some(moved_operation)
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
operation @ Operation::Delete { .. },
|
||||||
|
Some(last_delete @ Operation::Delete { .. }),
|
||||||
|
) => {
|
||||||
|
debug_assert!(
|
||||||
|
last_delete.range().contains(&operation.start_index()),
|
||||||
|
"There is a last delete ({last_delete}) but the operation ({operation}) is \
|
||||||
|
not contained in it"
|
||||||
|
);
|
||||||
|
|
||||||
|
let difference = operation.start_index() as i64 - last_delete.start_index() as i64;
|
||||||
|
|
||||||
|
let updated_delete = Operation::create_delete(
|
||||||
|
last_delete.start_index(),
|
||||||
|
0.max(operation.end_index() as i64 - last_delete.end_index() as i64) as usize,
|
||||||
|
);
|
||||||
|
|
||||||
|
affecting_context.replace_last_operation(Operation::create_delete(
|
||||||
|
last_delete.start_index(),
|
||||||
|
0.max(last_delete.end_index() as i64 - operation.end_index() as i64) as usize,
|
||||||
|
));
|
||||||
|
affecting_context.shift -= difference;
|
||||||
|
|
||||||
|
produced_context.consume_and_replace_last_operation(updated_delete.clone());
|
||||||
|
|
||||||
|
updated_delete
|
||||||
|
}
|
||||||
|
(
|
||||||
|
ref operation @ Operation::Equal {
|
||||||
|
length,
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
ref text,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
Some(last_delete @ Operation::Delete { .. }),
|
||||||
|
) => {
|
||||||
|
debug_assert!(
|
||||||
|
last_delete.range().contains(&operation.start_index()),
|
||||||
|
"There is a last delete ({last_delete}) but the operation ({operation}) is \
|
||||||
|
not contained in it"
|
||||||
|
);
|
||||||
|
|
||||||
|
let overlap = (length as i64)
|
||||||
|
.min(last_delete.end_index() as i64 - operation.start_index() as i64 + 1);
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
let result = text.as_ref().map_or_else(
|
||||||
|
|| {
|
||||||
|
Operation::create_equal(
|
||||||
|
operation.end_index().min(last_delete.end_index()),
|
||||||
|
(length as i64 - overlap) as usize,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|text| {
|
||||||
|
Operation::create_equal_with_text(
|
||||||
|
operation.end_index().min(last_delete.end_index()),
|
||||||
|
text.chars().skip(overlap as usize).collect::<String>(),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
let result = Operation::create_equal(
|
||||||
|
operation.end_index().min(last_delete.end_index()),
|
||||||
|
(length as i64 - overlap) as usize,
|
||||||
|
);
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
(operation @ Operation::Equal { .. }, _) => Some(operation),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Display for Operation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Operation::Equal {
|
||||||
|
index,
|
||||||
|
length,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
text,
|
||||||
|
} => {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"<equal {} from index {}>",
|
||||||
|
text.as_ref()
|
||||||
|
.map(|text| format!("'{text}'"))
|
||||||
|
.unwrap_or(format!("{length} characters")),
|
||||||
|
index
|
||||||
|
)?;
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
write!(f, "<equal {length} from index {index}>")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Operation::Insert { index, text } => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"<insert '{}' from index {}>",
|
||||||
|
text.iter().map(Token::original).collect::<String>(),
|
||||||
|
index
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Operation::Delete {
|
||||||
|
index,
|
||||||
|
deleted_character_count,
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
deleted_text,
|
||||||
|
} => {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"<delete {} from index {}>",
|
||||||
|
deleted_text
|
||||||
|
.as_ref()
|
||||||
|
.map(|text| format!("'{text}'"))
|
||||||
|
.unwrap_or(format!("{deleted_character_count} characters")),
|
||||||
|
index
|
||||||
|
)?;
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"<delete {deleted_character_count} characters from index {index}>",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Debug for Operation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{self}") }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "Shifted index must be non-negative")]
|
||||||
|
fn test_shifting_error() {
|
||||||
|
insta::assert_debug_snapshot!(
|
||||||
|
Operation::create_insert(1, vec!["hi".into()])
|
||||||
|
.unwrap()
|
||||||
|
.with_shifted_index(-2)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_apply_delete_with_create() {
|
||||||
|
let builder = StringBuilder::new("hello world");
|
||||||
|
let operation = Operation::<()>::create_delete_with_text(5, " world".to_owned()).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(operation.apply(builder).build(), "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_apply_insert() {
|
||||||
|
let builder = StringBuilder::new("hello");
|
||||||
|
let operation = Operation::create_insert(5, vec![" my friend".into()]).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(operation.apply(builder).build(), "hello my friend");
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/operation_transformation/ordered_operation.rs
Normal file
48
src/operation_transformation/ordered_operation.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::operation_transformation::Operation;
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct OrderedOperation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
pub order: usize,
|
||||||
|
pub operation: Operation<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> OrderedOperation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
pub fn get_sort_key(&self) -> (usize, usize, String) {
|
||||||
|
(
|
||||||
|
self.order,
|
||||||
|
self.operation.start_index(),
|
||||||
|
// Make sure that the ordering is deterministic regardless of which text
|
||||||
|
// is left or right.
|
||||||
|
match &self.operation {
|
||||||
|
Operation::Equal { index, .. } => index.to_string(),
|
||||||
|
Operation::Insert { text, .. } => text
|
||||||
|
.iter()
|
||||||
|
.map(crate::tokenizer::token::Token::original)
|
||||||
|
.collect::<String>(),
|
||||||
|
Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
..
|
||||||
|
} => deleted_character_count.to_string(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> PartialOrd for OrderedOperation<T>
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
|
self.get_sort_key().partial_cmp(&other.get_sort_key())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/operation_transformation/edited_text.rs
|
||||||
|
expression: operations
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
EditedText {
|
||||||
|
text: "hello world! How are you? Adam",
|
||||||
|
operations: [
|
||||||
|
OrderedOperation {
|
||||||
|
order: 0,
|
||||||
|
operation: <insert 'Hello, my friend!' from index 0>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 0,
|
||||||
|
operation: <delete 'hello world!' from index 17>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 12,
|
||||||
|
operation: <equal ' ' from index 17>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 13,
|
||||||
|
operation: <equal 'How' from index 18>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 16,
|
||||||
|
operation: <equal ' ' from index 21>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 17,
|
||||||
|
operation: <equal 'are' from index 22>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 20,
|
||||||
|
operation: <insert ' you doing? Albert' from index 25>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 20,
|
||||||
|
operation: <delete ' you? Adam' from index 43>,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
cursors: [],
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
---
|
||||||
|
source: reconcile/src/operation_transformation/edited_text.rs
|
||||||
|
expression: operations
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
EditedText {
|
||||||
|
text: "hello world!",
|
||||||
|
operations: [
|
||||||
|
OrderedOperation {
|
||||||
|
order: 0,
|
||||||
|
operation: <equal 'hello' from index 0>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 5,
|
||||||
|
operation: <equal ' ' from index 5>,
|
||||||
|
},
|
||||||
|
OrderedOperation {
|
||||||
|
order: 6,
|
||||||
|
operation: <equal 'world!' from index 6>,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
cursors: [],
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue