Implement Copy for multibuffer anchors

Max Brunsfeld created

Change summary

crates/editor/src/editor.rs              | 35 +++++++++++--------------
crates/editor/src/editor_tests.rs        |  6 ++--
crates/editor/src/element.rs             |  9 ++----
crates/editor/src/hover_popover.rs       |  2 
crates/editor/src/multi_buffer.rs        |  2 
crates/editor/src/multi_buffer/anchor.rs |  2 
crates/vim/src/visual.rs                 |  4 +-
7 files changed, 26 insertions(+), 34 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -1162,7 +1162,7 @@ impl Editor {
         });
         clone.selections.set_state(&self.selections);
         clone.scroll_position = self.scroll_position;
-        clone.scroll_top_anchor = self.scroll_top_anchor.clone();
+        clone.scroll_top_anchor = self.scroll_top_anchor;
         clone.searchable = self.searchable;
         clone
     }
@@ -1305,7 +1305,7 @@ impl Editor {
             display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)),
             ongoing_scroll: self.ongoing_scroll,
             scroll_position: self.scroll_position,
-            scroll_top_anchor: self.scroll_top_anchor.clone(),
+            scroll_top_anchor: self.scroll_top_anchor,
             placeholder_text: self.placeholder_text.clone(),
             is_focused: self
                 .handle
@@ -1791,17 +1791,15 @@ impl Editor {
             .pending_anchor()
             .expect("extend_selection not called with pending selection");
         if position >= tail {
-            pending_selection.start = tail_anchor.clone();
+            pending_selection.start = tail_anchor;
         } else {
-            pending_selection.end = tail_anchor.clone();
+            pending_selection.end = tail_anchor;
             pending_selection.reversed = true;
         }
 
         let mut pending_mode = self.selections.pending_mode().unwrap();
         match &mut pending_mode {
-            SelectMode::Word(range) | SelectMode::Line(range) => {
-                *range = tail_anchor.clone()..tail_anchor
-            }
+            SelectMode::Word(range) | SelectMode::Line(range) => *range = tail_anchor..tail_anchor,
             _ => {}
         }
 
@@ -2145,10 +2143,9 @@ impl Editor {
                                     ));
                             if following_text_allows_autoclose && preceding_text_matches_prefix {
                                 let anchor = snapshot.anchor_before(selection.end);
-                                new_selections
-                                    .push((selection.map(|_| anchor.clone()), text.len()));
+                                new_selections.push((selection.map(|_| anchor), text.len()));
                                 new_autoclose_regions.push((
-                                    anchor.clone(),
+                                    anchor,
                                     text.len(),
                                     selection.id,
                                     bracket_pair.clone(),
@@ -2169,10 +2166,8 @@ impl Editor {
                                 && text.as_ref() == region.pair.end.as_str();
                             if should_skip {
                                 let anchor = snapshot.anchor_after(selection.end);
-                                new_selections.push((
-                                    selection.map(|_| anchor.clone()),
-                                    region.pair.end.len(),
-                                ));
+                                new_selections
+                                    .push((selection.map(|_| anchor), region.pair.end.len()));
                                 continue;
                             }
                         }
@@ -2204,7 +2199,7 @@ impl Editor {
             // text with the given input and move the selection to the end of the
             // newly inserted text.
             let anchor = snapshot.anchor_after(selection.end);
-            new_selections.push((selection.map(|_| anchor.clone()), 0));
+            new_selections.push((selection.map(|_| anchor), 0));
             edits.push((selection.start..selection.end, text.clone()));
         }
 
@@ -2306,7 +2301,7 @@ impl Editor {
                         }
 
                         let anchor = buffer.anchor_after(end);
-                        let new_selection = selection.map(|_| anchor.clone());
+                        let new_selection = selection.map(|_| anchor);
                         (
                             (start..end, new_text),
                             (insert_extra_newline, new_selection),
@@ -2386,7 +2381,7 @@ impl Editor {
                         .iter()
                         .map(|s| {
                             let anchor = snapshot.anchor_after(s.end);
-                            s.map(|_| anchor.clone())
+                            s.map(|_| anchor)
                         })
                         .collect::<Vec<_>>()
                 };
@@ -3650,7 +3645,7 @@ impl Editor {
                         String::new(),
                     ));
                     let insertion_anchor = buffer.anchor_after(insertion_point);
-                    edits.push((insertion_anchor.clone()..insertion_anchor, text));
+                    edits.push((insertion_anchor..insertion_anchor, text));
 
                     let row_delta = range_to_move.start.row - insertion_point.row + 1;
 
@@ -3755,7 +3750,7 @@ impl Editor {
                         String::new(),
                     ));
                     let insertion_anchor = buffer.anchor_after(insertion_point);
-                    edits.push((insertion_anchor.clone()..insertion_anchor, text));
+                    edits.push((insertion_anchor..insertion_anchor, text));
 
                     let row_delta = insertion_point.row - range_to_move.end.row + 1;
 
@@ -4625,7 +4620,7 @@ impl Editor {
                     cursor_anchor: position,
                     cursor_position: point,
                     scroll_position: self.scroll_position,
-                    scroll_top_anchor: self.scroll_top_anchor.clone(),
+                    scroll_top_anchor: self.scroll_top_anchor,
                     scroll_top_row,
                 }),
                 cx,

crates/editor/src/editor_tests.rs 🔗

@@ -542,7 +542,7 @@ fn test_navigation_history(cx: &mut gpui::MutableAppContext) {
         // Set scroll position to check later
         editor.set_scroll_position(Vector2F::new(5.5, 5.5), cx);
         let original_scroll_position = editor.scroll_position;
-        let original_scroll_top_anchor = editor.scroll_top_anchor.clone();
+        let original_scroll_top_anchor = editor.scroll_top_anchor;
 
         // Jump to the end of the document and adjust scroll
         editor.move_to_end(&MoveToEnd, cx);
@@ -556,12 +556,12 @@ fn test_navigation_history(cx: &mut gpui::MutableAppContext) {
         assert_eq!(editor.scroll_top_anchor, original_scroll_top_anchor);
 
         // Ensure we don't panic when navigation data contains invalid anchors *and* points.
-        let mut invalid_anchor = editor.scroll_top_anchor.clone();
+        let mut invalid_anchor = editor.scroll_top_anchor;
         invalid_anchor.text_anchor.buffer_id = Some(999);
         let invalid_point = Point::new(9999, 0);
         editor.navigate(
             Box::new(NavigationData {
-                cursor_anchor: invalid_anchor.clone(),
+                cursor_anchor: invalid_anchor,
                 cursor_position: invalid_point,
                 scroll_top_anchor: invalid_anchor,
                 scroll_top_row: invalid_point.row,

crates/editor/src/element.rs 🔗

@@ -1607,16 +1607,13 @@ impl Element for EditorElement {
 
             highlighted_rows = view.highlighted_rows();
             let theme = cx.global::<Settings>().theme.as_ref();
-            highlighted_ranges = view.background_highlights_in_range(
-                start_anchor.clone()..end_anchor.clone(),
-                &display_map,
-                theme,
-            );
+            highlighted_ranges =
+                view.background_highlights_in_range(start_anchor..end_anchor, &display_map, theme);
 
             let mut remote_selections = HashMap::default();
             for (replica_id, line_mode, cursor_shape, selection) in display_map
                 .buffer_snapshot
-                .remote_selections_in_range(&(start_anchor.clone()..end_anchor.clone()))
+                .remote_selections_in_range(&(start_anchor..end_anchor))
             {
                 // The local selections match the leader's selections.
                 if Some(replica_id) == view.leader_replica_id {

crates/editor/src/multi_buffer.rs 🔗

@@ -2305,7 +2305,7 @@ impl MultiBufferSnapshot {
                     break;
                 }
                 let (anchor_ix, anchor) = anchors.next().unwrap();
-                let mut anchor = anchor.clone();
+                let mut anchor = *anchor;
 
                 // Leave min and max anchors unchanged if invalid or
                 // if the old excerpt still exists at this location

crates/editor/src/multi_buffer/anchor.rs 🔗

@@ -6,7 +6,7 @@ use std::{
 };
 use sum_tree::Bias;
 
-#[derive(Clone, Eq, PartialEq, Debug, Hash)]
+#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
 pub struct Anchor {
     pub(crate) buffer_id: Option<usize>,
     pub(crate) excerpt_id: ExcerptId,

crates/vim/src/visual.rs 🔗

@@ -114,12 +114,12 @@ pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspac
                         };
 
                         edits.push((expanded_range, "\n"));
-                        new_selections.push(selection.map(|_| anchor.clone()));
+                        new_selections.push(selection.map(|_| anchor));
                     } else {
                         let range = selection.map(|p| p.to_point(map)).range();
                         let anchor = map.buffer_snapshot.anchor_after(range.end);
                         edits.push((range, ""));
-                        new_selections.push(selection.map(|_| anchor.clone()));
+                        new_selections.push(selection.map(|_| anchor));
                     }
                     selection.goal = SelectionGoal::None;
                 });