From 992fc071334dd22b2c8ee7cbecab7cf8b4048e05 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 12 Mar 2022 10:45:11 -0700 Subject: [PATCH] Hide selections in original editor when renaming Co-Authored-By: Antonio Scandurra --- crates/editor/src/editor.rs | 5 ++- crates/editor/src/element.rs | 59 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 57e43f874ef7b8d5cafa7be234a1effff667c3e8..1741a52623898ec01d1a20d67bd2e21a1cc28ca5 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -440,6 +440,7 @@ pub struct Editor { project: Option>, 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::(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.focused = false; - self.show_local_cursors = false; self.buffer .update(cx, |buffer, cx| buffer.remove_active_selections(cx)); self.hide_context_menu(cx); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 7537f696700acbc1f63a3b2fac9830227dfd6e43..78aa2a22323f4a6c514fa92b77df8a318b38eb96 100644 --- a/crates/editor/src/element.rs +++ b/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);