From 5cd94b5b92b8835ef5220363b92dee0e0ea2c942 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 23 Mar 2022 19:05:46 +0100 Subject: [PATCH] WIP --- crates/diagnostics/src/diagnostics.rs | 4 +- crates/editor/src/items.rs | 18 +++++-- crates/search/src/project_search.rs | 4 +- crates/workspace/src/pane.rs | 71 +++++++++++++++------------ crates/workspace/src/workspace.rs | 10 ++-- 5 files changed, 62 insertions(+), 45 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 7bee815ad92523ef84f768c1686fe110fd0e8564..a0182902f1ca3af18dbcfddae60eaadc3c780d58 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -454,9 +454,9 @@ impl workspace::Item for ProjectDiagnosticsEditor { None } - fn navigate(&mut self, data: Box, cx: &mut ViewContext) { + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { self.editor - .update(cx, |editor, cx| editor.navigate(data, cx)); + .update(cx, |editor, cx| editor.navigate(data, cx)) } fn is_dirty(&self, cx: &AppContext) -> bool { diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 5971cbc07bb5dfbb87cf826418e546551d60637c..246a462019b53aeca2b6df0111ed7fbaf5025f1f 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -241,7 +241,7 @@ fn deserialize_selection( } impl Item for Editor { - fn navigate(&mut self, data: Box, cx: &mut ViewContext) { + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { if let Some(data) = data.downcast_ref::() { let buffer = self.buffer.read(cx).read(cx); let offset = if buffer.can_resolve(&data.anchor) { @@ -249,11 +249,19 @@ impl Item for Editor { } else { buffer.clip_offset(data.offset, Bias::Left) }; - + let newest_selection = self.newest_selection_with_snapshot::(&buffer); drop(buffer); - let nav_history = self.nav_history.take(); - self.select_ranges([offset..offset], Some(Autoscroll::Fit), cx); - self.nav_history = nav_history; + + if newest_selection.head() == offset { + false + } else { + let nav_history = self.nav_history.take(); + self.select_ranges([offset..offset], Some(Autoscroll::Fit), cx); + self.nav_history = nav_history; + true + } + } else { + false } } diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 1302040d19c3c02606db7995d47eac6b147680a2..212e1c66a7d9be5c41e657a2e7d97266eba85a3c 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -302,9 +302,9 @@ impl Item for ProjectSearchView { }); } - fn navigate(&mut self, data: Box, cx: &mut ViewContext) { + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { self.results_editor - .update(cx, |editor, cx| editor.navigate(data, cx)); + .update(cx, |editor, cx| editor.navigate(data, cx)) } fn should_update_tab_on_event(event: &ViewEvent) -> bool { diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index e04af153ba1d76e6ca48855ea98c397da7bc6ebf..df30d48dbec6010aa85e1d4e9b86461d39fc01a6 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -212,40 +212,47 @@ impl Pane { workspace.activate_pane(pane.clone(), cx); let to_load = pane.update(cx, |pane, cx| { - // Retrieve the weak item handle from the history. - let entry = pane.nav_history.borrow_mut().pop(mode)?; - - // If the item is still present in this pane, then activate it. - if let Some(index) = entry - .item - .upgrade(cx) - .and_then(|v| pane.index_for_item(v.as_ref())) - { - if let Some(item) = pane.active_item() { - pane.nav_history.borrow_mut().set_mode(mode); - item.deactivated(cx); - pane.nav_history - .borrow_mut() - .set_mode(NavigationMode::Normal); - } + loop { + // Retrieve the weak item handle from the history. + let entry = pane.nav_history.borrow_mut().pop(mode)?; + + // If the item is still present in this pane, then activate it. + if let Some(index) = entry + .item + .upgrade(cx) + .and_then(|v| pane.index_for_item(v.as_ref())) + { + if let Some(item) = pane.active_item() { + pane.nav_history.borrow_mut().set_mode(mode); + item.deactivated(cx); + pane.nav_history + .borrow_mut() + .set_mode(NavigationMode::Normal); + } + + let prev_active_index = mem::replace(&mut pane.active_item_index, index); + pane.focus_active_item(cx); + let mut navigated = prev_active_index != pane.active_item_index; + if let Some(data) = entry.data { + navigated |= pane.active_item()?.navigate(data, cx); + } - pane.active_item_index = index; - pane.focus_active_item(cx); - if let Some(data) = entry.data { - pane.active_item()?.navigate(data, cx); + if navigated { + cx.notify(); + break None; + } + } + // If the item is no longer present in this pane, then retrieve its + // project path in order to reopen it. + else { + break pane + .nav_history + .borrow_mut() + .paths_by_item + .get(&entry.item.id()) + .cloned() + .map(|project_path| (project_path, entry)); } - cx.notify(); - None - } - // If the item is no longer present in this pane, then retrieve its - // project path in order to reopen it. - else { - pane.nav_history - .borrow_mut() - .paths_by_item - .get(&entry.item.id()) - .cloned() - .map(|project_path| (project_path, entry)) } }); diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 691f17ca559deee7eb66c8f4c1f3b3bd0d4a6423..65300c5f343c8c9135214887d8eebdcc2870ad25 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -203,7 +203,9 @@ pub struct JoinProjectParams { pub trait Item: View { fn deactivated(&mut self, _: &mut ViewContext) {} - fn navigate(&mut self, _: Box, _: &mut ViewContext) {} + fn navigate(&mut self, _: Box, _: &mut ViewContext) -> bool { + false + } fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox; fn project_path(&self, cx: &AppContext) -> Option; fn project_entry_id(&self, cx: &AppContext) -> Option; @@ -362,7 +364,7 @@ pub trait ItemHandle: 'static + fmt::Debug { cx: &mut ViewContext, ); fn deactivated(&self, cx: &mut MutableAppContext); - fn navigate(&self, data: Box, cx: &mut MutableAppContext); + fn navigate(&self, data: Box, cx: &mut MutableAppContext) -> bool; fn id(&self) -> usize; fn to_any(&self) -> AnyViewHandle; fn is_dirty(&self, cx: &AppContext) -> bool; @@ -510,8 +512,8 @@ impl ItemHandle for ViewHandle { self.update(cx, |this, cx| this.deactivated(cx)); } - fn navigate(&self, data: Box, cx: &mut MutableAppContext) { - self.update(cx, |this, cx| this.navigate(data, cx)); + fn navigate(&self, data: Box, cx: &mut MutableAppContext) -> bool { + self.update(cx, |this, cx| this.navigate(data, cx)) } fn save(&self, project: ModelHandle, cx: &mut MutableAppContext) -> Task> {