Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

vim: Fix paste crash with out-of-order marks (#64876)

Agus Zubiaga2280e5d95e77
diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs
index 61cd45b0f1a..f3851b15101 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -30,6 +30,125 @@ fn test_edit() {
assert_eq!(buffer.text(), "ghiamnoef");
}
+#[test]
+fn test_summaries_for_unordered_anchors() {
+ let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), "ab\ncdef");
+ let old_right = buffer.anchor_before(6);
+ let old_left = buffer.anchor_before(2);
+ buffer.edit([(3..3, "é\n")]);
+
+ let snapshot = buffer.snapshot();
+ let anchors = [
+ snapshot.anchor_before(6),
+ snapshot.anchor_before(5),
+ old_right,
+ snapshot.anchor_after(snapshot.len()),
+ old_left,
+ snapshot.anchor_before(0),
+ snapshot.anchor_before(5),
+ ];
+ assert_eq!(
+ anchors
+ .iter()
+ .map(|anchor| snapshot.offset_for_anchor(anchor))
+ .collect::<Vec<_>>(),
+ [6, 5, 9, 10, 2, 0, 5]
+ );
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<Point, _>(anchors)
+ .collect::<Vec<_>>(),
+ anchors
+ .iter()
+ .map(|anchor| snapshot.summary_for_anchor::<Point>(anchor))
+ .collect::<Vec<_>>()
+ );
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<usize, _>(anchors)
+ .collect::<Vec<_>>(),
+ [6, 5, 9, 10, 2, 0, 5]
+ );
+
+ let ordered = [old_left, snapshot.anchor_before(5), old_right];
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<Point, _>(ordered)
+ .collect::<Vec<_>>(),
+ snapshot
+ .summaries_for_anchors::<Point, _>(ordered)
+ .collect::<Vec<_>>()
+ );
+
+ let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), "abcdefgh");
+ let deleted_anchor = buffer.anchor_before(5);
+ buffer.edit([(3..6, "")]);
+ let snapshot = buffer.snapshot();
+ let anchors = [
+ snapshot.anchor_before(4),
+ deleted_anchor,
+ snapshot.anchor_before(1),
+ ];
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<Point, _>(anchors)
+ .collect::<Vec<_>>(),
+ anchors
+ .iter()
+ .map(|anchor| snapshot.summary_for_anchor::<Point>(anchor))
+ .collect::<Vec<_>>()
+ );
+}
+
+#[test]
+fn test_summaries_for_unordered_anchors_across_chunks() {
+ let mut buffer = Buffer::new(
+ ReplicaId::LOCAL,
+ BufferId::new(1).unwrap(),
+ "abcdefghij\n".repeat(100),
+ );
+ let deleted_anchor = buffer.anchor_before(6);
+ let old_anchor = buffer.anchor_after(310);
+ buffer.edit([(4..8, ""), (120..120, "é")]);
+
+ let snapshot = buffer.snapshot();
+ let mut anchors = (0..100)
+ .flat_map(|row| {
+ [
+ snapshot.anchor_before(Point::new(row, 1)),
+ snapshot.anchor_after(Point::new(row, 1)),
+ ]
+ })
+ .collect::<Vec<_>>();
+ anchors.extend([
+ deleted_anchor,
+ old_anchor,
+ snapshot.anchor_before(0),
+ snapshot.anchor_after(snapshot.len()),
+ deleted_anchor,
+ ]);
+ anchors.shuffle(&mut StdRng::seed_from_u64(42));
+
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<Point, _>(anchors.iter().copied())
+ .collect::<Vec<_>>(),
+ anchors
+ .iter()
+ .map(|anchor| snapshot.summary_for_anchor::<Point>(anchor))
+ .collect::<Vec<_>>()
+ );
+ assert_eq!(
+ snapshot
+ .summaries_for_anchors_unordered::<usize, _>(anchors.iter().copied())
+ .collect::<Vec<_>>(),
+ anchors
+ .iter()
+ .map(|anchor| snapshot.offset_for_anchor(anchor))
+ .collect::<Vec<_>>()
+ );
+}
+
#[test]
fn test_point_for_row_and_column_from_external_source() {
let buffer = Buffer::new(
diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs
index 3e16ddff16a..b66e1fab76f 100644
--- a/crates/text/src/text.rs
+++ b/crates/text/src/text.rs
@@ -2443,6 +2443,31 @@ impl BufferSnapshot {
&'a self,
anchors: A,
) -> impl 'a + Iterator<Item = (D, T)>
+ where
+ D: 'a + TextDimension,
+ A: 'a + IntoIterator<Item = (Anchor, T)>,
+ {
+ self.summaries_for_anchors_with_payload_impl::<D, A, T, false>(anchors)
+ }
+
+ pub fn summaries_for_anchors_unordered<'a, D, A>(
+ &'a self,
+ anchors: A,
+ ) -> impl 'a + Iterator<Item = D>
+ where
+ D: 'a + TextDimension,
+ A: 'a + IntoIterator<Item = Anchor>,
+ {
+ self.summaries_for_anchors_with_payload_impl::<D, _, (), true>(
+ anchors.into_iter().map(|anchor| (anchor, ())),
+ )
+ .map(|(summary, ())| summary)
+ }
+
+ fn summaries_for_anchors_with_payload_impl<'a, D, A, T, const ALLOW_BACKWARDS: bool>(
+ &'a self,
+ anchors: A,
+ ) -> impl 'a + Iterator<Item = (D, T)>
where
D: 'a + TextDimension,
A: 'a + IntoIterator<Item = (Anchor, T)>,
@@ -2487,13 +2512,23 @@ impl BufferSnapshot {
anchor.bias == Bias::Right,
);
- fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left);
+ let fragment_id = Some(&insertion.fragment_id);
+ // The cursor's start locator belongs to the preceding fragment.
+ if ALLOW_BACKWARDS && fragment_id <= fragment_cursor.start().0 {
+ fragment_cursor.seek(&fragment_id, Bias::Left);
+ } else {
+ fragment_cursor.seek_forward(&fragment_id, Bias::Left);
+ }
let fragment = fragment_cursor.item().unwrap();
let mut fragment_offset = fragment_cursor.start().1;
if fragment.visible {
fragment_offset += (anchor.offset - insertion.split_offset) as usize;
}
+ if ALLOW_BACKWARDS && fragment_offset < text_cursor.offset() {
+ text_cursor = self.visible_text.cursor(0);
+ position = D::zero(());
+ }
position.add_assign(&text_cursor.summary(fragment_offset));
(position, payload)
})
diff --git a/crates/vim/src/normal/paste.rs b/crates/vim/src/normal/paste.rs
index b9b68018382..78346451c23 100644
--- a/crates/vim/src/normal/paste.rs
+++ b/crates/vim/src/normal/paste.rs
@@ -1235,6 +1235,32 @@ mod test {
);
}
+ #[gpui::test]
+ async fn test_paste_multiple_clipboard_selections_at_end_of_file(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ let mut cx = VimTestContext::new(cx, true).await;
+ cx.set_state("012345678901234567890ˇ", Mode::Normal);
+
+ let clipboard_selections = [6, 3]
+ .into_iter()
+ .map(|len| editor::ClipboardSelection {
+ len,
+ is_entire_line: false,
+ first_line_indent: 0,
+ file_path: None,
+ line_range: None,
+ })
+ .collect::<Vec<_>>();
+ cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
+ "abcdef\nxyz".to_string(),
+ clipboard_selections,
+ ));
+
+ cx.simulate_keystrokes("p");
+ assert_eq!(cx.buffer_text(), "012345678901234567890abcdefxyz");
+ }
+
#[gpui::test]
async fn test_paste_marks(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
diff --git a/crates/vim/src/state.rs b/crates/vim/src/state.rs
index c76579311ec..45a08567adc 100644
--- a/crates/vim/src/state.rs
+++ b/crates/vim/src/state.rs
@@ -449,23 +449,24 @@ impl MarksState {
buffer: &Entity<Buffer>,
cx: &mut Context<Self>,
) {
- let new_points: HashMap<String, Vec<Point>> =
- if let Some(anchors) = self.buffer_marks.get(&buffer.read(cx).remote_id()) {
- anchors
- .iter()
- .map(|(name, anchors)| {
- (
- name.clone(),
- buffer
- .read(cx)
- .summaries_for_anchors::<Point, _>(anchors.iter().copied())
- .collect(),
- )
- })
- .collect()
- } else {
- HashMap::default()
- };
+ let new_points: HashMap<String, Vec<Point>> = if let Some(anchors) =
+ self.buffer_marks.get(&buffer.read(cx).remote_id())
+ {
+ anchors
+ .iter()
+ .map(|(name, anchors)| {
+ let snapshot = buffer.read(cx);
+ (
+ name.clone(),
+ snapshot
+ .summaries_for_anchors_unordered::<Point, _>(anchors.iter().copied())
+ .collect(),
+ )
+ })
+ .collect()
+ } else {
+ HashMap::default()
+ };
let old_points = self.serialized_marks.get(&path);
if old_points == Some(&new_points) {
return;