on master help sign in
transaction: Also modify map_pos to work with insert|delete order.
commitaf55ebd002e7584e2f82a174c4b48552004afa43 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 18 Feb 2021, 12:17 (6 years ago) parentd5f9622
1 file changed+27−27
helix-core/src/transaction.rs+27−27
helix-core/src/transaction.rs+27−27
@@ -335,7 +335,7 @@ impl ChangeSet {
335335 Delete(i) | Retain(i) => *i,
336336 Insert(_) => 0,
337337 };
338− let old_end = old_pos + len;
338+ let mut old_end = old_pos + len;
339339
340340 match change {
341341 Retain(_) => {
@@ -345,37 +345,37 @@ impl ChangeSet {
345345 new_pos += len;
346346 }
347347 Delete(_) => {
348− // a subsequent ins means a replace, consume it
349− let ins = if let Some(Insert(s)) = iter.peek() {
350− iter.next();
351− s.chars().count()
352− } else {
353− 0
354− };
355−
356348 // in range
357349 if old_end > pos {
358− // at point or tracking before
359− if pos == old_pos || assoc == Assoc::Before {
360− return new_pos;
361− } else {
362− // place to end of delete
363− return new_pos + ins;
364− }
350+ return new_pos;
365351 }
366−
367− new_pos += ins;
368352 }
369353 Insert(s) => {
370354 let ins = s.chars().count();
371− // at insert point
372− if old_pos == pos {
373− // return position before inserted text
374− if assoc == Assoc::Before {
375− return new_pos;
376− } else {
377− // after text
378− return new_pos + ins;
355+
356+ // a subsequent delete means a replace, consume it
357+ if let Some(Delete(len)) = iter.peek() {
358+ old_end = old_pos + len;
359+ // in range of replaced text
360+ if old_end > pos {
361+ // at point or tracking before
362+ if pos == old_pos || assoc == Assoc::Before {
363+ return new_pos;
364+ } else {
365+ // place to end of insert
366+ return new_pos + ins;
367+ }
368+ }
369+ } else {
370+ // at insert point
371+ if old_pos == pos {
372+ // return position before inserted text
373+ if assoc == Assoc::Before {
374+ return new_pos;
375+ } else {
376+ // after text
377+ return new_pos + ins;
378+ }
379379 }
380380 }
381381
@@ -602,10 +602,10 @@ mod test {
602602 // stays inbetween replacements
603603 let cs = ChangeSet {
604604 changes: vec![
605− Delete(2),
606605 Insert("ab".into()),
607606 Delete(2),
608607 Insert("cd".into()),
608+ Delete(2),
609609 ],
610610 len: 4,
611611 };