From 718f802157315260527dc8f21ddda1081673f5a0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 23 Nov 2022 16:56:22 -0800 Subject: [PATCH] Implement Copy for multibuffer anchors --- 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(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 426215eb15aed4ce571bec8f16f22ec63091af17..5bbeed3fb56dd754aa181f867a85956ba71d90b4 100644 --- a/crates/editor/src/editor.rs +++ b/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::>() }; @@ -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, diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 0ed5023c43e691997c2103f8424d02a6f2fae846..8ac1f9a3fc529426632c8020fe59b53cc8c8f9cc 100644 --- a/crates/editor/src/editor_tests.rs +++ b/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, diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 1fd90b7a33cc549ac546024a753696c837d9d6ff..8409786637f1444f36c2814c94e3dc4613d32670 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1607,16 +1607,13 @@ impl Element for EditorElement { highlighted_rows = view.highlighted_rows(); let theme = cx.global::().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 { diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index e4b4da68d35ca33e42a7ec47103c3f2b1d2aa1a1..7369b0a6f40b5a20718d354e3a1ece9fe5c16aa4 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -221,7 +221,7 @@ fn show_hover( start..end } else { - anchor.clone()..anchor.clone() + anchor..anchor }; Some(InfoPopover { diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index f8bfd80335965fa92194404ea96327eb8a9ed8c2..d758792e6c29f88e1b17a344c949d2c50f999454 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/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 diff --git a/crates/editor/src/multi_buffer/anchor.rs b/crates/editor/src/multi_buffer/anchor.rs index 84b542781619c8ac12d741b39c76c3f425fa40d9..4ecab76c48d7f469b4b844b821dd93c97a44dcb9 100644 --- a/crates/editor/src/multi_buffer/anchor.rs +++ b/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, pub(crate) excerpt_id: ExcerptId, diff --git a/crates/vim/src/visual.rs b/crates/vim/src/visual.rs index ff454d81a86dc410b6ce997296a0d2d1c93a53e2..95f6c3d8b4caa1f6ad347c7788334aeb0835928a 100644 --- a/crates/vim/src/visual.rs +++ b/crates/vim/src/visual.rs @@ -114,12 +114,12 @@ pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext