Navigate to previous positions in editors when using navigation history

This commit is contained in:
Max Brunsfeld
2022-01-17 16:01:52 -08:00
parent e43d33cdad
commit 11b7270f68
4 changed files with 107 additions and 66 deletions
+13
View File
@@ -2399,6 +2399,8 @@ impl Editor {
return;
}
self.push_to_navigation_history(cx);
let selection = Selection {
id: post_inc(&mut self.next_selection_id),
start: 0,
@@ -2421,6 +2423,8 @@ impl Editor {
return;
}
self.push_to_navigation_history(cx);
let cursor = self.buffer.read(cx).read(cx).len();
let selection = Selection {
id: post_inc(&mut self.next_selection_id),
@@ -2432,6 +2436,15 @@ impl Editor {
self.update_selections(vec![selection], Some(Autoscroll::Fit), cx);
}
fn push_to_navigation_history(&self, cx: &mut ViewContext<Self>) {
if let Some(navigation) = &self.navigation {
if let Some(last_selection) = self.selections.iter().max_by_key(|s| s.id) {
let cursor = last_selection.head();
navigation.push(Some(cursor), cx);
}
}
}
pub fn select_to_end(&mut self, _: &SelectToEnd, cx: &mut ViewContext<Self>) {
let mut selection = self.local_selections::<usize>(cx).first().unwrap().clone();
selection.set_head(self.buffer.read(cx).read(cx).len());
+9 -5
View File
@@ -1,5 +1,4 @@
use crate::{Autoscroll, Editor, Event};
use crate::{MultiBuffer, ToPoint as _};
use crate::{Anchor, Autoscroll, Editor, Event, MultiBuffer, ToOffset, ToPoint as _};
use anyhow::Result;
use gpui::{
elements::*, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, RenderContext,
@@ -106,6 +105,13 @@ impl ItemView for Editor {
BufferItemHandle(self.buffer.read(cx).as_singleton().unwrap())
}
fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) {
if let Some(anchor) = data.downcast_ref::<Anchor>() {
let offset = anchor.to_offset(&self.buffer.read(cx).read(cx));
self.select_ranges([offset..offset], Some(Autoscroll::Fit), cx);
}
}
fn title(&self, cx: &AppContext) -> String {
let filename = self
.buffer()
@@ -134,9 +140,7 @@ impl ItemView for Editor {
}
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
if let Some(navigation) = self.navigation.as_ref() {
navigation.push::<(), _>(None, cx);
}
self.push_to_navigation_history(cx);
}
fn is_dirty(&self, cx: &AppContext) -> bool {