diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs index 340ad60081..46a69c7ee6 100644 --- a/crates/editor/src/lib.rs +++ b/crates/editor/src/lib.rs @@ -534,7 +534,7 @@ impl Editor { cx.notify(); } - fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext) { + pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext) { let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let scroll_top_buffer_offset = DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right); @@ -555,6 +555,11 @@ impl Editor { cx.notify(); } + pub fn scroll_position(&self, cx: &mut ViewContext) -> Vector2F { + let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); + compute_scroll_position(&display_map, self.scroll_position, &self.scroll_top_anchor) + } + pub fn clamp_scroll_left(&mut self, max: f32) -> bool { if max < self.scroll_position.x() { self.scroll_position.set_x(max); @@ -3029,7 +3034,7 @@ impl Editor { .unwrap() } - fn update_selections( + pub fn update_selections( &mut self, mut selections: Vec>, autoscroll: Option, diff --git a/crates/go_to_line/src/lib.rs b/crates/go_to_line/src/lib.rs index 633220f683..35d8469d97 100644 --- a/crates/go_to_line/src/lib.rs +++ b/crates/go_to_line/src/lib.rs @@ -1,26 +1,35 @@ -use buffer::{Bias, Point}; +use buffer::{Bias, Point, Selection}; use editor::{Autoscroll, Editor, EditorSettings}; use gpui::{ - action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View, - ViewContext, ViewHandle, + action, elements::*, geometry::vector::Vector2F, keymap::Binding, Entity, MutableAppContext, + RenderContext, View, ViewContext, ViewHandle, }; use postage::watch; use workspace::{Settings, Workspace}; action!(Toggle); +action!(Confirm); pub fn init(cx: &mut MutableAppContext) { cx.add_bindings([ Binding::new("ctrl-g", Toggle, Some("Editor")), Binding::new("escape", Toggle, Some("GoToLine")), + Binding::new("enter", Confirm, Some("GoToLine")), ]); cx.add_action(GoToLine::toggle); + cx.add_action(GoToLine::confirm); } pub struct GoToLine { settings: watch::Receiver, line_editor: ViewHandle, active_editor: ViewHandle, + restore_state: Option, +} + +struct RestoreState { + scroll_position: Vector2F, + selections: Vec>, } pub enum Event { @@ -50,10 +59,19 @@ impl GoToLine { }); cx.subscribe(&line_editor, Self::on_line_editor_event) .detach(); + + let restore_state = active_editor.update(cx, |editor, cx| { + Some(RestoreState { + scroll_position: editor.scroll_position(cx), + selections: editor.selections::(cx).collect(), + }) + }); + Self { settings: settings.clone(), line_editor, active_editor, + restore_state, } } @@ -71,6 +89,11 @@ impl GoToLine { }); } + fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { + self.restore_state.take(); + cx.emit(Event::Dismissed); + } + fn on_event( workspace: &mut Workspace, _: ViewHandle, @@ -119,8 +142,13 @@ impl Entity for GoToLine { type Event = Event; fn release(&mut self, cx: &mut MutableAppContext) { - self.active_editor.update(cx, |editor, _| { + 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); + } }) } }