Splice remove suggesion hints when those are cleared in the editor. (#9088)

Kirill Bulatov created

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))

Change summary

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(-)

Detailed changes

crates/copilot_ui/src/copilot_button.rs 🔗

@@ -149,7 +149,7 @@ impl CopilotButton {
     pub fn build_copilot_menu(&mut self, cx: &mut ViewContext<Self>) -> View<ContextMenu> {
         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<Editor>, cx: &mut ViewContext<Self>) {

crates/editor/src/editor.rs 🔗

@@ -1215,6 +1215,7 @@ impl CodeActionsMenu {
     }
 }
 
+#[derive(Debug)]
 pub(crate) struct CopilotState {
     excerpt_id: Option<ExcerptId>,
     pending_refresh: Task<Option<()>>,
@@ -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<InlayId>,
         to_insert: Vec<Inlay>,
@@ -4161,7 +4160,10 @@ impl Editor {
     }
 
     fn clear_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) {
-        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);
     }
 

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)
     }
 }