Preserve selection when clicking on editor dismisses go-to-line dialog

This commit is contained in:
Nathan Sobo 2021-11-25 14:10:43 -07:00
parent bf7acb5f34
commit ad26362a82

View file

@ -25,6 +25,7 @@ pub struct GoToLine {
line_editor: ViewHandle<Editor>, line_editor: ViewHandle<Editor>,
active_editor: ViewHandle<Editor>, active_editor: ViewHandle<Editor>,
restore_state: Option<RestoreState>, restore_state: Option<RestoreState>,
line_selection: Option<Selection<usize>>,
cursor_point: Point, cursor_point: Point,
max_point: Point, max_point: Point,
} }
@ -80,6 +81,7 @@ impl GoToLine {
line_editor, line_editor,
active_editor, active_editor,
restore_state, restore_state,
line_selection: None,
cursor_point, cursor_point,
max_point, max_point,
} }
@ -134,11 +136,12 @@ impl GoToLine {
column.map(|column| column.saturating_sub(1)).unwrap_or(0), 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 buffer = active_editor.buffer().read(cx);
let point = buffer.clip_point(point, Bias::Left); let point = buffer.clip_point(point, Bias::Left);
active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx); active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx);
active_editor.set_highlighted_row(Some(point.row)); active_editor.set_highlighted_row(Some(point.row));
Some(active_editor.newest_selection(cx))
}); });
cx.notify(); cx.notify();
} }
@ -152,12 +155,15 @@ impl Entity for GoToLine {
type Event = Event; type Event = Event;
fn release(&mut self, cx: &mut MutableAppContext) { fn release(&mut self, cx: &mut MutableAppContext) {
let line_selection = self.line_selection.take();
let restore_state = self.restore_state.take(); let restore_state = self.restore_state.take();
self.active_editor.update(cx, |editor, cx| { self.active_editor.update(cx, |editor, cx| {
editor.set_highlighted_row(None); editor.set_highlighted_row(None);
if let Some(restore_state) = restore_state { if let Some((line_selection, restore_state)) = line_selection.zip(restore_state) {
editor.set_scroll_position(restore_state.scroll_position, cx); if line_selection.id == editor.newest_selection::<usize>(cx).id {
editor.update_selections(restore_state.selections, None, cx); editor.set_scroll_position(restore_state.scroll_position, cx);
editor.update_selections(restore_state.selections, None, cx);
}
} }
}) })
} }