From c5d171a641b6161ee425dc0240e7794246e89cc8 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 20 Apr 2023 04:36:04 +0200 Subject: [PATCH] feat: add delta (replace with offset) in factory --- src/lib/operation/factory.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/operation/factory.rs b/src/lib/operation/factory.rs index b8fa014..49ed278 100644 --- a/src/lib/operation/factory.rs +++ b/src/lib/operation/factory.rs @@ -4,14 +4,21 @@ use similar::{TextDiff, ChangeTag}; pub trait OperationFactory { fn content(&self) -> String; - fn replace(&self, txt: &str) -> OperationSeq { + fn replace(&self, txt: &str) -> OperationSeq { self.delta(0, txt, 0) } + + fn delta(&self, skip: usize, txt: &str, tail: usize) -> OperationSeq { let mut out = OperationSeq::default(); let content = self.content(); - if content == txt { + let tail_index = content.len() - tail; + let content_slice = &content[skip..tail_index]; + + if content_slice == txt { return out; // TODO this won't work, should we return a noop instead? } - let diff = TextDiff::from_chars(content.as_str(), txt); + out.retain(skip as u64); + + let diff = TextDiff::from_chars(content_slice, txt); for change in diff.iter_all_changes() { match change.tag() { @@ -21,6 +28,8 @@ pub trait OperationFactory { } } + out.retain(tail as u64); + out }