From 984378e12cf331e494caaced614d7617da301ab1 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 29 Dec 2021 23:47:03 -0800 Subject: [PATCH] Use anchors for line movement edits to support multi-buffers Because multi-buffers can contain the same content multiple times, we need to use anchors to track our desired insertion and removal locations when moving lines. This is because deleting a line in order to move it might end up deleting *multiple* lines. --- crates/editor/src/editor.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 028706b193..62b4b1c984 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1737,8 +1737,13 @@ impl Editor { .chain(['\n']) .collect::(); - edits.push((insertion_point..insertion_point, text)); - edits.push((range_to_move.clone(), String::new())); + edits.push(( + buffer.anchor_after(range_to_move.start) + ..buffer.anchor_before(range_to_move.end), + String::new(), + )); + let insertion_anchor = buffer.anchor_after(insertion_point); + edits.push((insertion_anchor.clone()..insertion_anchor, text)); let row_delta = range_to_move.start.row - insertion_point.row + 1; @@ -1773,7 +1778,7 @@ impl Editor { self.start_transaction(cx); self.unfold_ranges(unfold_ranges, cx); self.buffer.update(cx, |buffer, cx| { - for (range, text) in edits.into_iter().rev() { + for (range, text) in edits { buffer.edit([range], text, cx); } }); @@ -1828,8 +1833,13 @@ impl Editor { let mut text = String::from("\n"); text.extend(buffer.text_for_range(range_to_move.clone())); text.pop(); // Drop trailing newline - edits.push((range_to_move.clone(), String::new())); - edits.push((insertion_point..insertion_point, text)); + edits.push(( + buffer.anchor_after(range_to_move.start) + ..buffer.anchor_before(range_to_move.end), + String::new(), + )); + let insertion_anchor = buffer.anchor_after(insertion_point); + edits.push((insertion_anchor.clone()..insertion_anchor, text)); let row_delta = insertion_point.row - range_to_move.end.row + 1; @@ -1864,7 +1874,7 @@ impl Editor { self.start_transaction(cx); self.unfold_ranges(unfold_ranges, cx); self.buffer.update(cx, |buffer, cx| { - for (range, text) in edits.into_iter().rev() { + for (range, text) in edits { buffer.edit([range], text, cx); } });