fix visible edit prediction

Smit Barmase created

Change summary

crates/editor/src/edit_prediction_tests.rs | 18 +++++-----
crates/editor/src/editor.rs                | 37 ++++++++++++++---------
crates/editor/src/editor_tests.rs          |  2 
3 files changed, 32 insertions(+), 25 deletions(-)

Detailed changes

crates/editor/src/edit_prediction_tests.rs πŸ”—

@@ -22,7 +22,7 @@ async fn test_edit_prediction_insert(cx: &mut gpui::TestAppContext) {
     cx.set_state("let absolute_zero_celsius = Λ‡;");
 
     propose_edits(&provider, vec![(28..28, "-273.15")], &mut cx);
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
 
     assert_editor_active_edit_completion(&mut cx, |_, edits| {
         assert_eq!(edits.len(), 1);
@@ -44,7 +44,7 @@ async fn test_edit_prediction_modification(cx: &mut gpui::TestAppContext) {
     cx.set_state("let pi = Λ‡\"foo\";");
 
     propose_edits(&provider, vec![(9..14, "3.14159")], &mut cx);
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
 
     assert_editor_active_edit_completion(&mut cx, |_, edits| {
         assert_eq!(edits.len(), 1);
@@ -79,7 +79,7 @@ async fn test_edit_prediction_jump_button(cx: &mut gpui::TestAppContext) {
         &mut cx,
     );
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     assert_editor_active_move_completion(&mut cx, |snapshot, move_target| {
         assert_eq!(move_target.to_point(&snapshot), Point::new(4, 3));
     });
@@ -109,7 +109,7 @@ async fn test_edit_prediction_jump_button(cx: &mut gpui::TestAppContext) {
         &mut cx,
     );
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     assert_editor_active_move_completion(&mut cx, |snapshot, move_target| {
         assert_eq!(move_target.to_point(&snapshot), Point::new(1, 3));
     });
@@ -150,7 +150,7 @@ async fn test_edit_prediction_invalidation_range(cx: &mut gpui::TestAppContext)
         &mut cx,
     );
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     assert_editor_active_move_completion(&mut cx, |snapshot, move_target| {
         assert_eq!(move_target.to_point(&snapshot), edit_location);
     });
@@ -198,7 +198,7 @@ async fn test_edit_prediction_invalidation_range(cx: &mut gpui::TestAppContext)
         &mut cx,
     );
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     assert_editor_active_move_completion(&mut cx, |snapshot, move_target| {
         assert_eq!(move_target.to_point(&snapshot), edit_location);
     });
@@ -253,7 +253,7 @@ async fn test_edit_prediction_jump_disabled_for_non_zed_providers(cx: &mut gpui:
         &mut cx,
     );
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
 
     // For non-Zed providers, there should be no move completion (jump functionality disabled)
     cx.editor(|editor, _, _| {
@@ -291,7 +291,7 @@ async fn test_edit_predictions_disabled_in_scope(cx: &mut gpui::TestAppContext)
     // Test disabled inside of string
     cx.set_state("const x = \"hello Λ‡world\";");
     propose_edits(&provider, vec![(17..17, "beautiful ")], &mut cx);
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     cx.editor(|editor, _, _| {
         assert!(
             editor.active_edit_prediction.is_none(),
@@ -302,7 +302,7 @@ async fn test_edit_predictions_disabled_in_scope(cx: &mut gpui::TestAppContext)
     // Test enabled outside of string
     cx.set_state("const x = \"hello world\"; Λ‡");
     propose_edits(&provider, vec![(24..24, "// comment")], &mut cx);
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     cx.editor(|editor, _, _| {
         assert!(
             editor.active_edit_prediction.is_some(),

crates/editor/src/editor.rs πŸ”—

@@ -2818,7 +2818,7 @@ impl Editor {
         self.edit_prediction_provider = provider.map(|provider| RegisteredEditPredictionProvider {
             _subscription: cx.observe_in(&provider, window, |this, _, window, cx| {
                 if this.focus_handle.is_focused(window) {
-                    this.update_visible_edit_prediction(window, cx);
+                    this.update_visible_edit_prediction(false, window, cx);
                 }
             }),
             provider: Arc::new(provider),
@@ -2921,7 +2921,7 @@ impl Editor {
         if hidden != self.edit_predictions_hidden_for_vim_mode {
             self.edit_predictions_hidden_for_vim_mode = hidden;
             if hidden {
-                self.update_visible_edit_prediction(window, cx);
+                self.update_visible_edit_prediction(false, window, cx);
             } else {
                 self.refresh_edit_prediction(true, false, window, cx);
             }
@@ -3157,7 +3157,7 @@ impl Editor {
             self.refresh_document_highlights(cx);
             self.refresh_selected_text_highlights(false, window, cx);
             refresh_matching_bracket_highlights(self, window, cx);
-            self.update_visible_edit_prediction(window, cx);
+            self.update_visible_edit_prediction(false, window, cx);
             self.edit_prediction_requires_modifier_in_indent_conflict = true;
             linked_editing_ranges::refresh_linked_ranges(self, window, cx);
             self.inline_blame_popover.take();
@@ -5813,7 +5813,7 @@ impl Editor {
 
                         crate::hover_popover::hide_hover(editor, cx);
                         if editor.show_edit_predictions_in_menu() {
-                            editor.update_visible_edit_prediction(window, cx);
+                            editor.update_visible_edit_prediction(false, window, cx);
                         } else {
                             editor.discard_edit_prediction(false, cx);
                         }
@@ -5828,7 +5828,7 @@ impl Editor {
                         // If it was already hidden and we don't show edit predictions in the menu,
                         // we should also show the edit prediction when available.
                         if was_hidden && editor.show_edit_predictions_in_menu() {
-                            editor.update_visible_edit_prediction(window, cx);
+                            editor.update_visible_edit_prediction(false, window, cx);
                         }
                     }
                 })
@@ -7161,7 +7161,7 @@ impl Editor {
             return None;
         }
 
-        self.update_visible_edit_prediction(window, cx);
+        self.update_visible_edit_prediction(user_requested, window, cx);
         provider.refresh(
             self.project.clone(),
             buffer,
@@ -7319,7 +7319,7 @@ impl Editor {
         }
 
         provider.cycle(buffer, cursor_buffer_position, direction, cx);
-        self.update_visible_edit_prediction(window, cx);
+        self.update_visible_edit_prediction(true, window, cx);
 
         Some(())
     }
@@ -7335,7 +7335,7 @@ impl Editor {
             return;
         }
 
-        self.update_visible_edit_prediction(window, cx);
+        self.update_visible_edit_prediction(true, window, cx);
     }
 
     pub fn display_cursor_names(
@@ -7484,7 +7484,7 @@ impl Editor {
                     }
                 }
 
-                self.update_visible_edit_prediction(window, cx);
+                self.update_visible_edit_prediction(false, window, cx);
                 if self.active_edit_prediction.is_none() {
                     self.refresh_edit_prediction(true, true, window, cx);
                 }
@@ -7774,7 +7774,7 @@ impl Editor {
                     since: Instant::now(),
                 };
 
-                self.update_visible_edit_prediction(window, cx);
+                self.update_visible_edit_prediction(false, window, cx);
                 cx.notify();
             }
         } else if let EditPredictionPreview::Active {
@@ -7797,13 +7797,14 @@ impl Editor {
                 released_too_fast: since.elapsed() < Duration::from_millis(200),
             };
             self.clear_row_highlights::<EditPredictionPreview>();
-            self.update_visible_edit_prediction(window, cx);
+            self.update_visible_edit_prediction(false, window, cx);
             cx.notify();
         }
     }
 
     fn update_visible_edit_prediction(
         &mut self,
+        user_requested: bool,
         _window: &mut Window,
         cx: &mut Context<Self>,
     ) -> Option<()> {
@@ -7854,10 +7855,16 @@ impl Editor {
         self.edit_prediction_settings =
             self.edit_prediction_settings_at_position(&buffer, cursor_buffer_position, cx);
 
-        if let EditPredictionSettings::Disabled = self.edit_prediction_settings {
+        // if user explicitly requested edit prediction, we should show despite disabled setting
+        if !user_requested
+            && matches!(
+                self.edit_prediction_settings,
+                EditPredictionSettings::Disabled
+            )
+        {
             self.discard_edit_prediction(false, cx);
             return None;
-        };
+        }
 
         self.edit_prediction_indent_conflict = multibuffer.is_line_whitespace_upto(cursor);
 
@@ -9575,7 +9582,7 @@ impl Editor {
         self.completion_tasks.clear();
         let context_menu = self.context_menu.borrow_mut().take();
         self.stale_edit_prediction_in_menu.take();
-        self.update_visible_edit_prediction(window, cx);
+        self.update_visible_edit_prediction(false, window, cx);
         if let Some(CodeContextMenu::Completions(_)) = &context_menu
             && let Some(completion_provider) = &self.completion_provider
         {
@@ -20569,7 +20576,7 @@ impl Editor {
                 self.refresh_single_line_folds(window, cx);
                 refresh_matching_bracket_highlights(self, window, cx);
                 if self.has_active_edit_prediction() {
-                    self.update_visible_edit_prediction(window, cx);
+                    self.update_visible_edit_prediction(false, window, cx);
                 }
                 if let Some(project) = self.project.as_ref()
                     && let Some(edited_buffer) = edited_buffer

crates/editor/src/editor_tests.rs πŸ”—

@@ -8271,7 +8271,7 @@ async fn test_undo_edit_prediction_scrolls_to_edit_pos(cx: &mut TestAppContext)
         })
     });
 
-    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(window, cx));
+    cx.update_editor(|editor, window, cx| editor.update_visible_edit_prediction(false, window, cx));
     cx.update_editor(|editor, window, cx| {
         editor.accept_edit_prediction(&crate::AcceptEditPrediction, window, cx)
     });