From ad26362a82e43a637b3eefa00db4c67d771bf08e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 25 Nov 2021 14:10:43 -0700 Subject: [PATCH] Preserve selection when clicking on editor dismisses go-to-line dialog --- crates/go_to_line/src/lib.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/go_to_line/src/lib.rs b/crates/go_to_line/src/lib.rs index 266d8f433a..6d623bfe6f 100644 --- a/crates/go_to_line/src/lib.rs +++ b/crates/go_to_line/src/lib.rs @@ -25,6 +25,7 @@ pub struct GoToLine { line_editor: ViewHandle, active_editor: ViewHandle, restore_state: Option, + line_selection: Option>, cursor_point: Point, max_point: Point, } @@ -80,6 +81,7 @@ impl GoToLine { line_editor, active_editor, restore_state, + line_selection: None, cursor_point, max_point, } @@ -134,11 +136,12 @@ impl GoToLine { column.map(|column| column.saturating_sub(1)).unwrap_or(0), ) }) { - self.active_editor.update(cx, |active_editor, cx| { + self.line_selection = self.active_editor.update(cx, |active_editor, cx| { let buffer = active_editor.buffer().read(cx); let point = buffer.clip_point(point, Bias::Left); active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx); active_editor.set_highlighted_row(Some(point.row)); + Some(active_editor.newest_selection(cx)) }); cx.notify(); } @@ -152,12 +155,15 @@ impl Entity for GoToLine { type Event = Event; fn release(&mut self, cx: &mut MutableAppContext) { + let line_selection = self.line_selection.take(); let restore_state = self.restore_state.take(); self.active_editor.update(cx, |editor, cx| { editor.set_highlighted_row(None); - if let Some(restore_state) = restore_state { - editor.set_scroll_position(restore_state.scroll_position, cx); - editor.update_selections(restore_state.selections, None, cx); + if let Some((line_selection, restore_state)) = line_selection.zip(restore_state) { + if line_selection.id == editor.newest_selection::(cx).id { + editor.set_scroll_position(restore_state.scroll_position, cx); + editor.update_selections(restore_state.selections, None, cx); + } } }) }