History

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

Kirill Bulatov committed 2 years ago

Commit 146971f1 parent 34717803 files · +14 −12Browse files

Unified · Split

3 files
  1. crates/copilot_ui/src/copilot_button.rs +2 −2
  2. crates/editor/src/editor.rs +11 −9
  3. crates/editor/src/inlay_hint_cache.rs +1 −1
crates/copilot_ui/src/copilot_button.rs+2−2
@@ -149,7 +149,7 @@ impl CopilotButton { · 148 unmodified lines
149149 pub fn build_copilot_menu(&mut self, cx: &mut ViewContext<Self>) -> View<ContextMenu> {
150150 let fs = self.fs.clone();
151151
152− return ContextMenu::build(cx, move |mut menu, cx| {
152+ ContextMenu::build(cx, move |mut menu, cx| {
153153 if let Some(language) = self.language.clone() {
154154 let fs = fs.clone();
155155 let language_enabled =
@@ -216,7 +216,7 @@ impl CopilotButton { · 60 unmodified lines
216216 .boxed_clone(),
217217 )
218218 .action("Sign Out", SignOut.boxed_clone())
219− });
219+ })
220220 }
221221
222222 pub fn update_enabled(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
crates/editor/src/editor.rs+11−9
@@ -1215,6 +1215,7 @@ impl CodeActionsMenu { · 1214 unmodified lines
12151215 }
12161216 }
12171217
1218+#[derive(Debug)]
12181219 pub(crate) struct CopilotState {
12191220 excerpt_id: Option<ExcerptId>,
12201221 pending_refresh: Task<Option<()>>,
@@ -3114,7 +3115,7 @@ impl Editor { · 1893 unmodified lines
31143115 (InvalidationStrategy::RefreshRequested, None)
31153116 } else {
31163117 self.inlay_hint_cache.clear();
3117− self.splice_inlay_hints(
3118+ self.splice_inlays(
31183119 self.visible_inlay_hints(cx)
31193120 .iter()
31203121 .map(|inlay| inlay.id)
@@ -3136,7 +3137,7 @@ impl Editor { · 15 unmodified lines
31363137 to_remove,
31373138 to_insert,
31383139 })) => {
3139− self.splice_inlay_hints(to_remove, to_insert, cx);
3140+ self.splice_inlays(to_remove, to_insert, cx);
31403141 return;
31413142 }
31423143 ControlFlow::Break(None) => return,
@@ -3149,7 +3150,7 @@ impl Editor { · 6 unmodified lines
31493150 to_insert,
31503151 }) = self.inlay_hint_cache.remove_excerpts(excerpts_removed)
31513152 {
3152− self.splice_inlay_hints(to_remove, to_insert, cx);
3153+ self.splice_inlays(to_remove, to_insert, cx);
31533154 }
31543155 return;
31553156 }
@@ -3172,7 +3173,7 @@ impl Editor { · 16 unmodified lines
31723173 ignore_debounce,
31733174 cx,
31743175 ) {
3175− self.splice_inlay_hints(to_remove, to_insert, cx);
3176+ self.splice_inlays(to_remove, to_insert, cx);
31763177 }
31773178 }
31783179
@@ -3180,9 +3181,7 @@ impl Editor { · 1 unmodified line
31803181 self.display_map
31813182 .read(cx)
31823183 .current_inlays()
3183− .filter(move |inlay| {
3184− Some(inlay.id) != self.copilot_state.suggestion.as_ref().map(|h| h.id)
3185− })
3184+ .filter(move |inlay| matches!(inlay.id, InlayId::Hint(_)))
31863185 .cloned()
31873186 .collect()
31883187 }
@@ -3253,7 +3252,7 @@ impl Editor { · 64 unmodified lines
32533252 }
32543253 }
32553254
3256− fn splice_inlay_hints(
3255+ fn splice_inlays(
32573256 &self,
32583257 to_remove: Vec<InlayId>,
32593258 to_insert: Vec<Inlay>,
@@ -4161,7 +4160,10 @@ impl Editor { · 901 unmodified lines
41614160 }
41624161
41634162 fn clear_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) {
4164− self.copilot_state = Default::default();
4163+ if let Some(old_suggestion) = self.copilot_state.suggestion.take() {
4164+ self.splice_inlays(vec![old_suggestion.id], Vec::new(), cx);
4165+ }
4166+ self.copilot_state = CopilotState::default();
41654167 self.discard_copilot_suggestion(cx);
41664168 }
41674169
crates/editor/src/inlay_hint_cache.rs+1−1
@@ -1255,7 +1255,7 @@ fn apply_hint_update( · 1254 unmodified lines
12551255 editor.inlay_hint_cache.version += 1;
12561256 }
12571257 if displayed_inlays_changed {
1258− editor.splice_inlay_hints(to_remove, to_insert, cx)
1258+ editor.splice_inlays(to_remove, to_insert, cx)
12591259 }
12601260 }
12611261