From 146971fb02f1130e16d9f653e30601ac1abc0bb3 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 9 Mar 2024 02:00:01 +0200 Subject: [PATCH] Splice remove suggesion hints when those are cleared in the editor. (#9088) Closes https://github.com/zed-industries/zed/issues/6793 Release Notes: - Fixed copilot suggestions not disappearing after disabling the tool ([6793](https://github.com/zed-industries/zed/issues/6793)) --- crates/copilot_ui/src/copilot_button.rs | 4 ++-- crates/editor/src/editor.rs | 20 +++++++++++--------- crates/editor/src/inlay_hint_cache.rs | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/copilot_ui/src/copilot_button.rs b/crates/copilot_ui/src/copilot_button.rs index afc39c3d1922bbf5f84941fd3984ddcc6290acac..1ca66dda3680e919854f1e9b8949d15975c22d6f 100644 --- a/crates/copilot_ui/src/copilot_button.rs +++ b/crates/copilot_ui/src/copilot_button.rs @@ -149,7 +149,7 @@ impl CopilotButton { pub fn build_copilot_menu(&mut self, cx: &mut ViewContext) -> View { let fs = self.fs.clone(); - return ContextMenu::build(cx, move |mut menu, cx| { + ContextMenu::build(cx, move |mut menu, cx| { if let Some(language) = self.language.clone() { let fs = fs.clone(); let language_enabled = @@ -216,7 +216,7 @@ impl CopilotButton { .boxed_clone(), ) .action("Sign Out", SignOut.boxed_clone()) - }); + }) } pub fn update_enabled(&mut self, editor: View, cx: &mut ViewContext) { diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index cbb1412b531348bb1581488e8b363ad58068ecee..7518851212b3728724e758fd3d2c20349eb01ed3 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1215,6 +1215,7 @@ impl CodeActionsMenu { } } +#[derive(Debug)] pub(crate) struct CopilotState { excerpt_id: Option, pending_refresh: Task>, @@ -3114,7 +3115,7 @@ impl Editor { (InvalidationStrategy::RefreshRequested, None) } else { self.inlay_hint_cache.clear(); - self.splice_inlay_hints( + self.splice_inlays( self.visible_inlay_hints(cx) .iter() .map(|inlay| inlay.id) @@ -3136,7 +3137,7 @@ impl Editor { to_remove, to_insert, })) => { - self.splice_inlay_hints(to_remove, to_insert, cx); + self.splice_inlays(to_remove, to_insert, cx); return; } ControlFlow::Break(None) => return, @@ -3149,7 +3150,7 @@ impl Editor { to_insert, }) = self.inlay_hint_cache.remove_excerpts(excerpts_removed) { - self.splice_inlay_hints(to_remove, to_insert, cx); + self.splice_inlays(to_remove, to_insert, cx); } return; } @@ -3172,7 +3173,7 @@ impl Editor { ignore_debounce, cx, ) { - self.splice_inlay_hints(to_remove, to_insert, cx); + self.splice_inlays(to_remove, to_insert, cx); } } @@ -3180,9 +3181,7 @@ impl Editor { self.display_map .read(cx) .current_inlays() - .filter(move |inlay| { - Some(inlay.id) != self.copilot_state.suggestion.as_ref().map(|h| h.id) - }) + .filter(move |inlay| matches!(inlay.id, InlayId::Hint(_))) .cloned() .collect() } @@ -3253,7 +3252,7 @@ impl Editor { } } - fn splice_inlay_hints( + fn splice_inlays( &self, to_remove: Vec, to_insert: Vec, @@ -4161,7 +4160,10 @@ impl Editor { } fn clear_copilot_suggestions(&mut self, cx: &mut ViewContext) { - self.copilot_state = Default::default(); + if let Some(old_suggestion) = self.copilot_state.suggestion.take() { + self.splice_inlays(vec![old_suggestion.id], Vec::new(), cx); + } + self.copilot_state = CopilotState::default(); self.discard_copilot_suggestion(cx); } diff --git a/crates/editor/src/inlay_hint_cache.rs b/crates/editor/src/inlay_hint_cache.rs index 73a997e3511e9a63f7f0d36b67432f1b5f069d6e..c9b066f56dc19193a408d2db1850633addb6d90b 100644 --- a/crates/editor/src/inlay_hint_cache.rs +++ b/crates/editor/src/inlay_hint_cache.rs @@ -1255,7 +1255,7 @@ fn apply_hint_update( editor.inlay_hint_cache.version += 1; } if displayed_inlays_changed { - editor.splice_inlay_hints(to_remove, to_insert, cx) + editor.splice_inlays(to_remove, to_insert, cx) } }