diff options
author | Blaž Hrastnik | 2021-02-18 03:17:33 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-02-18 03:17:33 +0000 |
commit | af55ebd002e7584e2f82a174c4b48552004afa43 (patch) | |
tree | e7b43538567352b356c72c31e0203c45488a9608 /helix-core | |
parent | d5f9622e2ee82887904ff5d5d433438075228cd1 (diff) |
transaction: Also modify map_pos to work with insert|delete order.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/transaction.rs | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index ce0174a6..e12aeebe 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -335,7 +335,7 @@ impl ChangeSet { Delete(i) | Retain(i) => *i, Insert(_) => 0, }; - let old_end = old_pos + len; + let mut old_end = old_pos + len; match change { Retain(_) => { @@ -345,37 +345,37 @@ impl ChangeSet { new_pos += len; } 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 if old_end > pos { - // at point or tracking before - if pos == old_pos || assoc == Assoc::Before { - return new_pos; - } else { - // place to end of delete - return new_pos + ins; - } + return new_pos; } - - new_pos += ins; } Insert(s) => { let ins = s.chars().count(); - // at insert point - if old_pos == pos { - // return position before inserted text - if assoc == Assoc::Before { - return new_pos; - } else { - // after text - return new_pos + ins; + + // 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 { + // at point or tracking before + if pos == old_pos || assoc == Assoc::Before { + return new_pos; + } else { + // place to end of insert + return new_pos + ins; + } + } + } else { + // at insert point + if old_pos == pos { + // return position before inserted text + if assoc == Assoc::Before { + return new_pos; + } else { + // after text + return new_pos + ins; + } } } @@ -602,10 +602,10 @@ mod test { // stays inbetween replacements let cs = ChangeSet { changes: vec![ - Delete(2), Insert("ab".into()), Delete(2), Insert("cd".into()), + Delete(2), ], len: 4, }; |