transaction: Also modify map_pos to work with insert|delete order.

pull/8/head
Blaž Hrastnik 4 years ago
parent d5f9622e2e
commit af55ebd002

@ -335,7 +335,7 @@ impl ChangeSet {
Delete(i) | Retain(i) => *i, Delete(i) | Retain(i) => *i,
Insert(_) => 0, Insert(_) => 0,
}; };
let old_end = old_pos + len; let mut old_end = old_pos + len;
match change { match change {
Retain(_) => { Retain(_) => {
@ -345,29 +345,28 @@ impl ChangeSet {
new_pos += len; new_pos += len;
} }
Delete(_) => { Delete(_) => {
// a subsequent ins means a replace, consume it
let ins = if let Some(Insert(s)) = iter.peek() {
iter.next();
s.chars().count()
} else {
0
};
// in range // in range
if old_end > pos {
return new_pos;
}
}
Insert(s) => {
let ins = s.chars().count();
// a subsequent delete means a replace, consume it
if let Some(Delete(len)) = iter.peek() {
old_end = old_pos + len;
// in range of replaced text
if old_end > pos { if old_end > pos {
// at point or tracking before // at point or tracking before
if pos == old_pos || assoc == Assoc::Before { if pos == old_pos || assoc == Assoc::Before {
return new_pos; return new_pos;
} else { } else {
// place to end of delete // place to end of insert
return new_pos + ins; return new_pos + ins;
} }
} }
} else {
new_pos += ins;
}
Insert(s) => {
let ins = s.chars().count();
// at insert point // at insert point
if old_pos == pos { if old_pos == pos {
// return position before inserted text // return position before inserted text
@ -378,6 +377,7 @@ impl ChangeSet {
return new_pos + ins; return new_pos + ins;
} }
} }
}
new_pos += ins; new_pos += ins;
} }
@ -602,10 +602,10 @@ mod test {
// stays inbetween replacements // stays inbetween replacements
let cs = ChangeSet { let cs = ChangeSet {
changes: vec![ changes: vec![
Delete(2),
Insert("ab".into()), Insert("ab".into()),
Delete(2), Delete(2),
Insert("cd".into()), Insert("cd".into()),
Delete(2),
], ],
len: 4, len: 4,
}; };

Loading…
Cancel
Save