From 7de8228efdf8778392e8b71fd35f45513d72595c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 15 Aug 2022 16:15:20 -0700 Subject: [PATCH 1/2] Clear stale go-to-def link when resizing buffer font --- crates/editor/src/editor.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index e8d3ad4650..2fd3c06538 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -42,7 +42,7 @@ use language::{ DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Point, Selection, SelectionGoal, TransactionId, }; -use link_go_to_definition::LinkGoToDefinitionState; +use link_go_to_definition::{hide_link_definition, LinkGoToDefinitionState}; pub use multi_buffer::{ Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint, @@ -6010,6 +6010,7 @@ impl View for Editor { if let Some(editor) = handle.upgrade(cx) { editor.update(cx, |editor, cx| { hide_hover(editor, cx); + hide_link_definition(editor, cx); }) } }); From 754a130e5935e72a9217e70734a26501094508f9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 15 Aug 2022 17:30:41 -0700 Subject: [PATCH 2/2] Clear go-to-def link when deactivating the window --- crates/editor/src/items.rs | 8 +++++- crates/editor/src/link_go_to_definition.rs | 31 ++++++++++++++++++++-- crates/workspace/src/workspace.rs | 29 +++++++++++++------- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 58bbda2e4d..04d649f910 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -1,5 +1,6 @@ use crate::{ - Anchor, Autoscroll, Editor, Event, ExcerptId, MultiBuffer, NavigationData, ToPoint as _, + link_go_to_definition::hide_link_definition, Anchor, Autoscroll, Editor, Event, ExcerptId, + MultiBuffer, NavigationData, ToPoint as _, }; use anyhow::{anyhow, Result}; use futures::FutureExt; @@ -376,6 +377,11 @@ impl Item for Editor { self.push_to_nav_history(selection.head(), None, cx); } + fn workspace_deactivated(&mut self, cx: &mut ViewContext) { + hide_link_definition(self, cx); + self.link_go_to_definition_state.last_mouse_location = None; + } + fn is_dirty(&self, cx: &AppContext) -> bool { self.buffer().read(cx).read(cx).is_dirty() } diff --git a/crates/editor/src/link_go_to_definition.rs b/crates/editor/src/link_go_to_definition.rs index c1544306d6..6eb38df758 100644 --- a/crates/editor/src/link_go_to_definition.rs +++ b/crates/editor/src/link_go_to_definition.rs @@ -52,7 +52,7 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(go_to_fetched_type_definition); } -#[derive(Default)] +#[derive(Debug, Default)] pub struct LinkGoToDefinitionState { pub last_mouse_location: Option, pub symbol_range: Option>, @@ -706,7 +706,34 @@ mod tests { fn do_work() { «test»(); } "}); - // Moving within symbol range doesn't re-request + // Deactivating the window dismisses the highlight + cx.update_workspace(|workspace, cx| { + workspace.on_window_activation_changed(false, cx); + }); + cx.assert_editor_text_highlights::(indoc! {" + fn test() { do_work(); } + fn do_work() { test(); } + "}); + + // Moving the mouse restores the highlights. + cx.update_editor(|editor, cx| { + update_go_to_definition_link( + editor, + &UpdateGoToDefinitionLink { + point: Some(hover_point), + cmd_held: true, + shift_held: false, + }, + cx, + ); + }); + cx.foreground().run_until_parked(); + cx.assert_editor_text_highlights::(indoc! {" + fn test() { do_work(); } + fn do_work() { «test»(); } + "}); + + // Moving again within the same symbol range doesn't re-request let hover_point = cx.display_point(indoc! {" fn test() { do_work(); } fn do_work() { tesˇt(); } diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 40934f4ed7..1abf6a54fc 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -261,6 +261,7 @@ pub struct AppState { pub trait Item: View { fn deactivated(&mut self, _: &mut ViewContext) {} + fn workspace_deactivated(&mut self, _: &mut ViewContext) {} fn navigate(&mut self, _: Box, _: &mut ViewContext) -> bool { false } @@ -433,6 +434,7 @@ pub trait ItemHandle: 'static + fmt::Debug { cx: &mut ViewContext, ); fn deactivated(&self, cx: &mut MutableAppContext); + fn workspace_deactivated(&self, cx: &mut MutableAppContext); fn navigate(&self, data: Box, cx: &mut MutableAppContext) -> bool; fn id(&self) -> usize; fn to_any(&self) -> AnyViewHandle; @@ -629,6 +631,10 @@ impl ItemHandle for ViewHandle { self.update(cx, |this, cx| this.deactivated(cx)); } + fn workspace_deactivated(&self, cx: &mut MutableAppContext) { + self.update(cx, |this, cx| this.workspace_deactivated(cx)); + } + fn navigate(&self, data: Box, cx: &mut MutableAppContext) -> bool { self.update(cx, |this, cx| this.navigate(data, cx)) } @@ -2383,18 +2389,21 @@ impl Workspace { None } - fn on_window_activation_changed(&mut self, active: bool, cx: &mut ViewContext) { - if !active - && matches!( - cx.global::().autosave, - Autosave::OnWindowChange | Autosave::OnFocusChange - ) - { + pub fn on_window_activation_changed(&mut self, active: bool, cx: &mut ViewContext) { + if !active { for pane in &self.panes { pane.update(cx, |pane, cx| { - for item in pane.items() { - Pane::autosave_item(item.as_ref(), self.project.clone(), cx) - .detach_and_log_err(cx); + if let Some(item) = pane.active_item() { + item.workspace_deactivated(cx); + } + if matches!( + cx.global::().autosave, + Autosave::OnWindowChange | Autosave::OnFocusChange + ) { + for item in pane.items() { + Pane::autosave_item(item.as_ref(), self.project.clone(), cx) + .detach_and_log_err(cx); + } } }); }