Hide selections in original editor when renaming

Nathan Sobo and Antonio Scandurra created

Co-Authored-By: Antonio Scandurra <me@as-cii.com>

Change summary

crates/editor/src/editor.rs  |  5 ++
crates/editor/src/element.rs | 59 +++++++++++++++++++++----------------
2 files changed, 38 insertions(+), 26 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -440,6 +440,7 @@ pub struct Editor {
     project: Option<ModelHandle<Project>>,
     focused: bool,
     show_local_cursors: bool,
+    show_local_selections: bool,
     blink_epoch: usize,
     blinking_paused: bool,
     mode: EditorMode,
@@ -921,6 +922,7 @@ impl Editor {
             autoscroll_request: None,
             focused: false,
             show_local_cursors: false,
+            show_local_selections: true,
             blink_epoch: 0,
             blinking_paused: false,
             mode,
@@ -4432,6 +4434,7 @@ impl Editor {
                     drop(buffer);
 
                     // Position the selection in the rename editor so that it matches the current selection.
+                    this.show_local_selections = false;
                     let rename_editor = cx.add_view(|cx| {
                         let mut editor = Editor::single_line(this.settings.clone(), None, cx);
                         if let Some(old_highlight_id) = old_highlight_id {
@@ -4552,6 +4555,7 @@ impl Editor {
         let rename = self.pending_rename.take()?;
         self.remove_blocks([rename.block_id].into_iter().collect(), cx);
         self.clear_text_highlights::<Rename>(cx);
+        self.show_local_selections = true;
 
         if moving_cursor {
             let cursor_in_rename_editor =
@@ -5664,7 +5668,6 @@ impl View for Editor {
 
     fn on_blur(&mut self, cx: &mut ViewContext<Self>) {
         self.focused = false;
-        self.show_local_cursors = false;
         self.buffer
             .update(cx, |buffer, cx| buffer.remove_active_selections(cx));
         self.hide_context_menu(cx);

crates/editor/src/element.rs 🔗

@@ -910,32 +910,37 @@ impl Element for EditorElement {
                 &display_map,
             );
 
-            let local_selections = view
-                .local_selections_in_range(start_anchor.clone()..end_anchor.clone(), &display_map);
-            for selection in &local_selections {
-                let is_empty = selection.start == selection.end;
-                let selection_start = snapshot.prev_line_boundary(selection.start).1;
-                let selection_end = snapshot.next_line_boundary(selection.end).1;
-                for row in cmp::max(selection_start.row(), start_row)
-                    ..=cmp::min(selection_end.row(), end_row)
-                {
-                    let contains_non_empty_selection = active_rows.entry(row).or_insert(!is_empty);
-                    *contains_non_empty_selection |= !is_empty;
+            if view.show_local_selections {
+                let local_selections = view.local_selections_in_range(
+                    start_anchor.clone()..end_anchor.clone(),
+                    &display_map,
+                );
+                for selection in &local_selections {
+                    let is_empty = selection.start == selection.end;
+                    let selection_start = snapshot.prev_line_boundary(selection.start).1;
+                    let selection_end = snapshot.next_line_boundary(selection.end).1;
+                    for row in cmp::max(selection_start.row(), start_row)
+                        ..=cmp::min(selection_end.row(), end_row)
+                    {
+                        let contains_non_empty_selection =
+                            active_rows.entry(row).or_insert(!is_empty);
+                        *contains_non_empty_selection |= !is_empty;
+                    }
                 }
+                selections.insert(
+                    view.replica_id(cx),
+                    local_selections
+                        .into_iter()
+                        .map(|selection| crate::Selection {
+                            id: selection.id,
+                            goal: selection.goal,
+                            reversed: selection.reversed,
+                            start: selection.start.to_display_point(&display_map),
+                            end: selection.end.to_display_point(&display_map),
+                        })
+                        .collect(),
+                );
             }
-            selections.insert(
-                view.replica_id(cx),
-                local_selections
-                    .into_iter()
-                    .map(|selection| crate::Selection {
-                        id: selection.id,
-                        goal: selection.goal,
-                        reversed: selection.reversed,
-                        start: selection.start.to_display_point(&display_map),
-                        end: selection.end.to_display_point(&display_map),
-                    })
-                    .collect(),
-            );
 
             for (replica_id, selection) in display_map
                 .buffer_snapshot
@@ -1474,7 +1479,11 @@ mod tests {
         let (window_id, editor) = cx.add_window(Default::default(), |cx| {
             Editor::new(EditorMode::Full, buffer, None, settings.1, None, cx)
         });
-        let element = EditorElement::new(editor.downgrade(), editor.read(cx).style(cx), CursorShape::Bar);
+        let element = EditorElement::new(
+            editor.downgrade(),
+            editor.read(cx).style(cx),
+            CursorShape::Bar,
+        );
 
         let layouts = editor.update(cx, |editor, cx| {
             let snapshot = editor.snapshot(cx);