mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
feat: add delta (replace with offset) in factory
This commit is contained in:
parent
97be3cf4ce
commit
c5d171a641
1 changed files with 12 additions and 3 deletions
|
@ -4,14 +4,21 @@ use similar::{TextDiff, ChangeTag};
|
||||||
pub trait OperationFactory {
|
pub trait OperationFactory {
|
||||||
fn content(&self) -> String;
|
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 mut out = OperationSeq::default();
|
||||||
let content = self.content();
|
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?
|
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() {
|
for change in diff.iter_all_changes() {
|
||||||
match change.tag() {
|
match change.tag() {
|
||||||
|
@ -21,6 +28,8 @@ pub trait OperationFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.retain(tail as u64);
|
||||||
|
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue