Restore scroll position and selections when cancelling go-to-line
But preserve the line when confirming.
This commit is contained in:
parent
8c0541b455
commit
1e49b56626
2 changed files with 39 additions and 6 deletions
|
@ -534,7 +534,7 @@ impl Editor {
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {
|
pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {
|
||||||
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||||
let scroll_top_buffer_offset =
|
let scroll_top_buffer_offset =
|
||||||
DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right);
|
DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right);
|
||||||
|
@ -555,6 +555,11 @@ impl Editor {
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> 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 {
|
pub fn clamp_scroll_left(&mut self, max: f32) -> bool {
|
||||||
if max < self.scroll_position.x() {
|
if max < self.scroll_position.x() {
|
||||||
self.scroll_position.set_x(max);
|
self.scroll_position.set_x(max);
|
||||||
|
@ -3029,7 +3034,7 @@ impl Editor {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_selections<T>(
|
pub fn update_selections<T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut selections: Vec<Selection<T>>,
|
mut selections: Vec<Selection<T>>,
|
||||||
autoscroll: Option<Autoscroll>,
|
autoscroll: Option<Autoscroll>,
|
||||||
|
|
|
@ -1,26 +1,35 @@
|
||||||
use buffer::{Bias, Point};
|
use buffer::{Bias, Point, Selection};
|
||||||
use editor::{Autoscroll, Editor, EditorSettings};
|
use editor::{Autoscroll, Editor, EditorSettings};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
action, elements::*, keymap::Binding, Entity, MutableAppContext, RenderContext, View,
|
action, elements::*, geometry::vector::Vector2F, keymap::Binding, Entity, MutableAppContext,
|
||||||
ViewContext, ViewHandle,
|
RenderContext, View, ViewContext, ViewHandle,
|
||||||
};
|
};
|
||||||
use postage::watch;
|
use postage::watch;
|
||||||
use workspace::{Settings, Workspace};
|
use workspace::{Settings, Workspace};
|
||||||
|
|
||||||
action!(Toggle);
|
action!(Toggle);
|
||||||
|
action!(Confirm);
|
||||||
|
|
||||||
pub fn init(cx: &mut MutableAppContext) {
|
pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_bindings([
|
cx.add_bindings([
|
||||||
Binding::new("ctrl-g", Toggle, Some("Editor")),
|
Binding::new("ctrl-g", Toggle, Some("Editor")),
|
||||||
Binding::new("escape", Toggle, Some("GoToLine")),
|
Binding::new("escape", Toggle, Some("GoToLine")),
|
||||||
|
Binding::new("enter", Confirm, Some("GoToLine")),
|
||||||
]);
|
]);
|
||||||
cx.add_action(GoToLine::toggle);
|
cx.add_action(GoToLine::toggle);
|
||||||
|
cx.add_action(GoToLine::confirm);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GoToLine {
|
pub struct GoToLine {
|
||||||
settings: watch::Receiver<Settings>,
|
settings: watch::Receiver<Settings>,
|
||||||
line_editor: ViewHandle<Editor>,
|
line_editor: ViewHandle<Editor>,
|
||||||
active_editor: ViewHandle<Editor>,
|
active_editor: ViewHandle<Editor>,
|
||||||
|
restore_state: Option<RestoreState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RestoreState {
|
||||||
|
scroll_position: Vector2F,
|
||||||
|
selections: Vec<Selection<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
|
@ -50,10 +59,19 @@ impl GoToLine {
|
||||||
});
|
});
|
||||||
cx.subscribe(&line_editor, Self::on_line_editor_event)
|
cx.subscribe(&line_editor, Self::on_line_editor_event)
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
|
let restore_state = active_editor.update(cx, |editor, cx| {
|
||||||
|
Some(RestoreState {
|
||||||
|
scroll_position: editor.scroll_position(cx),
|
||||||
|
selections: editor.selections::<usize>(cx).collect(),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
settings: settings.clone(),
|
settings: settings.clone(),
|
||||||
line_editor,
|
line_editor,
|
||||||
active_editor,
|
active_editor,
|
||||||
|
restore_state,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +89,11 @@ impl GoToLine {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
||||||
|
self.restore_state.take();
|
||||||
|
cx.emit(Event::Dismissed);
|
||||||
|
}
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
workspace: &mut Workspace,
|
workspace: &mut Workspace,
|
||||||
_: ViewHandle<Self>,
|
_: ViewHandle<Self>,
|
||||||
|
@ -119,8 +142,13 @@ impl Entity for GoToLine {
|
||||||
type Event = Event;
|
type Event = Event;
|
||||||
|
|
||||||
fn release(&mut self, cx: &mut MutableAppContext) {
|
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);
|
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);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue