pub struct EditedText<'a, T>{ /* private fields */ }Expand description
A text document with a sequence of operations derived from diffing it
against an updated version. Supports merging two EditedText instances
(from the same original) via Operational Transformation.
Created via from_strings, from_strings_with_tokenizer, or from_diff,
then merged with another EditedText and applied to get the reconciled
text.
Also tracks cursor positions from the updated text, repositioning them when operations are applied.
Implementations§
Source§impl<'a> EditedText<'a, String>
impl<'a> EditedText<'a, String>
Sourcepub fn from_strings(original: &'a str, updated: &TextWithCursors) -> Self
pub fn from_strings(original: &'a str, updated: &TextWithCursors) -> Self
Create an EditedText from the given original and updated strings.
Uses the default word tokenizer (splits on word boundaries).
Source§impl<'a, T> EditedText<'a, T>
impl<'a, T> EditedText<'a, T>
Sourcepub fn from_strings_with_tokenizer(
original: &'a str,
updated: &TextWithCursors,
tokenizer: &Tokenizer<T>,
) -> Self
pub fn from_strings_with_tokenizer( original: &'a str, updated: &TextWithCursors, tokenizer: &Tokenizer<T>, ) -> Self
Create an EditedText from the given original and updated strings
using the provided tokenizer
Sourcepub fn merge(self, other: Self) -> Self
pub fn merge(self, other: Self) -> Self
Merge two EditedText instances. The two instances must be derived
from the same original text. The operations are merged using the
principles of Operational Transformation. The cursors are updated
accordingly to reflect the changes made by the merged operations.
§Panics
Panics if there’s an integer overflow (in isize) when calculating new cursor positions.
Sourcepub fn apply(&self) -> TextWithCursors
pub fn apply(&self) -> TextWithCursors
Apply the operations to the text and return the resulting text
Sourcepub fn apply_with_history(&self) -> Vec<SpanWithHistory>
pub fn apply_with_history(&self) -> Vec<SpanWithHistory>
Apply the operations to the text and return the resulting text in chunks together with the provenance describing where each chunk came from.
Returns all spans including deletions (not present in the merged text).
use reconcile_text::{History, SpanWithHistory, BuiltinTokenizer, reconcile};
let parent = "Merging text is hard!";
let left = "Merging text is easy!"; // Changed "hard" to "easy"
let right = "With reconcile, merging documents is hard!"; // Added prefix and changed word
let result = reconcile(
parent,
&left.into(),
&right.into(),
&*BuiltinTokenizer::Word,
);
assert_eq!(
result.apply_with_history(),
vec![
SpanWithHistory::new("Merging text".to_string(), History::RemovedFromRight,),
SpanWithHistory::new(
"With reconcile, merging documents".to_string(),
History::AddedFromRight,
),
SpanWithHistory::new(" ".to_string(), History::Unchanged,),
SpanWithHistory::new("is".to_string(), History::Unchanged,),
SpanWithHistory::new(" hard!".to_string(), History::RemovedFromLeft,),
SpanWithHistory::new(" easy!".to_string(), History::AddedFromLeft,),
]
);Sourcepub fn apply_with_all(&self) -> (TextWithCursors, Vec<SpanWithHistory>)
pub fn apply_with_all(&self) -> (TextWithCursors, Vec<SpanWithHistory>)
Apply the operations and return both the merged text with cursors and the provenance history in a single pass
Sourcepub fn to_diff(&self) -> Result<Vec<NumberOrText>, DiffError>
pub fn to_diff(&self) -> Result<Vec<NumberOrText>, DiffError>
Convert the EditedText into a terse representation ready for
serialization. The result omits cursor positions and the original text.
This is useful for sending text diffs over the network if there’s a
clear consensus on the original text.
Inserts are strings, deletes are negative integers (character count), and retained spans are positive integers (character count).
§Errors
Returns DiffError::IntegerOverflow if a character count exceeds
i64::MAX.
Sourcepub fn from_diff(
original_text: &'a str,
diff: Vec<NumberOrText>,
tokenizer: &Tokenizer<T>,
) -> Result<EditedText<'a, T>, DiffError>
pub fn from_diff( original_text: &'a str, diff: Vec<NumberOrText>, tokenizer: &Tokenizer<T>, ) -> Result<EditedText<'a, T>, DiffError>
Trait Implementations§
Source§impl<'a, T> Clone for EditedText<'a, T>
impl<'a, T> Clone for EditedText<'a, T>
Source§fn clone(&self) -> EditedText<'a, T>
fn clone(&self) -> EditedText<'a, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more