Remove `snippets_only` and replicate similar behavior but only in `impl

HactarCE and Cole Miller created

CompletionProvider for Entity<Project>`

Co-authored-by: Cole Miller <cole@zed.dev>

Change summary

crates/agent_ui/src/acp/completion_provider.rs            |   2 
crates/agent_ui/src/acp/message_editor.rs                 |   2 
crates/agent_ui/src/context_picker/completion_provider.rs |   2 
crates/agent_ui/src/slash_command.rs                      |   2 
crates/debugger_ui/src/session/running/console.rs         |   2 
crates/editor/src/actions.rs                              |   2 
crates/editor/src/editor.rs                               |  98 +++----
crates/editor/src/editor_tests.rs                         | 109 --------
crates/inspector_ui/src/div_inspector.rs                  |   2 
crates/keymap_editor/src/keymap_editor.rs                 |   2 
10 files changed, 66 insertions(+), 157 deletions(-)

Detailed changes

crates/agent_ui/src/acp/completion_provider.rs 🔗

@@ -702,7 +702,7 @@ impl CompletionProvider for ContextPickerCompletionProvider {
         buffer: &Entity<Buffer>,
         buffer_position: Anchor,
         _trigger: CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>> {

crates/agent_ui/src/context_picker/completion_provider.rs 🔗

@@ -745,7 +745,7 @@ impl CompletionProvider for ContextPickerCompletionProvider {
         buffer: &Entity<Buffer>,
         buffer_position: Anchor,
         _trigger: CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>> {

crates/agent_ui/src/slash_command.rs 🔗

@@ -265,7 +265,7 @@ impl CompletionProvider for SlashCommandCompletionProvider {
         buffer: &Entity<Buffer>,
         buffer_position: Anchor,
         _: editor::CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<project::CompletionResponse>>> {

crates/debugger_ui/src/session/running/console.rs 🔗

@@ -520,7 +520,7 @@ impl CompletionProvider for ConsoleQueryBarCompletionProvider {
         buffer: &Entity<Buffer>,
         buffer_position: language::Anchor,
         _trigger: editor::CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>> {

crates/editor/src/actions.rs 🔗

@@ -220,8 +220,6 @@ pub struct ExpandExcerptsDown {
 pub struct ShowCompletions {
     #[serde(default)]
     pub(super) trigger: Option<String>,
-    #[serde(default)]
-    pub(super) snippets_only: bool,
 }
 
 /// Handles text input in the editor.

crates/editor/src/editor.rs 🔗

@@ -3118,14 +3118,7 @@ impl Editor {
                 };
 
                 if continue_showing {
-                    self.show_completions(
-                        &ShowCompletions {
-                            trigger: None,
-                            snippets_only: false,
-                        },
-                        window,
-                        cx,
-                    );
+                    self.show_completions(&ShowCompletions { trigger: None }, window, cx);
                 } else {
                     self.hide_context_menu(window, cx);
                 }
@@ -4414,6 +4407,7 @@ impl Editor {
                     )
                 }
             }
+
             this.trigger_completion_on_input(&text, trigger_in_words, window, cx);
             refresh_linked_ranges(this, window, cx);
             this.refresh_edit_prediction(true, false, window, cx);
@@ -4954,28 +4948,30 @@ impl Editor {
                         ignore_threshold: false,
                     }),
                     None,
-                    false,
                     window,
                     cx,
                 );
             }
             Some(CompletionsMenuSource::Normal)
             | Some(CompletionsMenuSource::SnippetChoices)
-            | None => {
-                let snippets_only = !self.is_completion_trigger(
+            | None
+                if self.is_completion_trigger(
                     text,
                     trigger_in_words,
                     completions_source.is_some(),
                     cx,
-                );
+                ) =>
+            {
                 self.show_completions(
                     &ShowCompletions {
                         trigger: Some(text.to_owned()).filter(|x| !x.is_empty()),
-                        snippets_only,
                     },
                     window,
                     cx,
-                );
+                )
+            }
+            _ => {
+                self.hide_context_menu(window, cx);
             }
         }
     }
@@ -5280,7 +5276,6 @@ impl Editor {
                 ignore_threshold: true,
             }),
             None,
-            false,
             window,
             cx,
         );
@@ -5292,20 +5287,13 @@ impl Editor {
         window: &mut Window,
         cx: &mut Context<Self>,
     ) {
-        self.open_or_update_completions_menu(
-            None,
-            options.trigger.as_deref(),
-            options.snippets_only,
-            window,
-            cx,
-        );
+        self.open_or_update_completions_menu(None, options.trigger.as_deref(), window, cx);
     }
 
     fn open_or_update_completions_menu(
         &mut self,
         requested_source: Option<CompletionsMenuSource>,
         trigger: Option<&str>,
-        snippets_only: bool,
         window: &mut Window,
         cx: &mut Context<Self>,
     ) {
@@ -5494,7 +5482,7 @@ impl Editor {
                     &buffer,
                     buffer_position,
                     completion_context,
-                    snippets_only,
+                    None,
                     window,
                     cx,
                 );
@@ -5929,14 +5917,7 @@ impl Editor {
             .as_ref()
             .is_some_and(|confirm| confirm(intent, window, cx));
         if show_new_completions_on_confirm {
-            self.show_completions(
-                &ShowCompletions {
-                    trigger: None,
-                    snippets_only: false,
-                },
-                window,
-                cx,
-            );
+            self.show_completions(&ShowCompletions { trigger: None }, window, cx);
         }
 
         let provider = self.completion_provider.as_ref()?;
@@ -22677,13 +22658,18 @@ pub trait SemanticsProvider {
 }
 
 pub trait CompletionProvider {
+    /// Provide completions.
+    ///
+    /// `text` is the text that was typed to trigger the completion, or `None`
+    /// if the completion was triggered by some other means (e.g., manually
+    /// invoked).
     fn completions(
         &self,
         excerpt_id: ExcerptId,
         buffer: &Entity<Buffer>,
         buffer_position: text::Anchor,
         trigger: CompletionContext,
-        snippets_only: bool,
+        text: Option<&str>,
         window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>>;
@@ -23036,19 +23022,38 @@ impl CompletionProvider for Entity<Project> {
         buffer: &Entity<Buffer>,
         buffer_position: text::Anchor,
         options: CompletionContext,
-        snippets_only: bool,
+        text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>> {
         self.update(cx, |project, cx| {
+            let buffer_snapshot = buffer.read(cx).snapshot();
+
+            // if show_all_completions=false, show only snippets
+            let show_all_completions = if let Some(text) = text {
+                let first_char_is_word = match text.chars().next() {
+                    None => return Task::ready(Ok(Vec::new())), // triggered by empty string
+                    Some(first_char) => {
+                        let classifier = buffer_snapshot
+                            .char_classifier_at(buffer_position)
+                            .scope_context(Some(CharScopeContext::Completion));
+                        classifier.is_word(first_char)
+                    }
+                };
+                first_char_is_word || buffer.read(cx).completion_triggers().contains(text)
+            } else {
+                true
+            };
+
             let snippets = snippet_completions(project, buffer, buffer_position, cx);
-            let project_completions = project.completions(buffer, buffer_position, options, cx);
+            let project_completions = if show_all_completions {
+                project.completions(buffer, buffer_position, options, cx)
+            } else {
+                Task::ready(Ok(Vec::new())) // snippets only
+            };
+
             cx.background_spawn(async move {
-                let mut responses = if snippets_only {
-                    Vec::new()
-                } else {
-                    project_completions.await?
-                };
+                let mut responses = project_completions.await?;
                 let snippets = snippets.await?;
                 if !snippets.completions.is_empty() {
                     responses.push(snippets);
@@ -23102,13 +23107,7 @@ impl CompletionProvider for Entity<Project> {
         menu_is_open: bool,
         cx: &mut Context<Editor>,
     ) -> bool {
-        let mut chars = text.chars();
-        let char = if let Some(char) = chars.next() {
-            char
-        } else {
-            return false;
-        };
-        if chars.next().is_some() {
+        if text.is_empty() {
             return false;
         }
 
@@ -23117,10 +23116,7 @@ impl CompletionProvider for Entity<Project> {
         if !menu_is_open && !snapshot.settings_at(position, cx).show_completions_on_input {
             return false;
         }
-        let classifier = snapshot
-            .char_classifier_at(position)
-            .scope_context(Some(CharScopeContext::Completion));
-        if trigger_in_words && classifier.is_word(char) {
+        if trigger_in_words {
             return true;
         }
 

crates/editor/src/editor_tests.rs 🔗

@@ -13742,14 +13742,7 @@ async fn test_completion_mode(cx: &mut TestAppContext) {
 
             cx.set_state(&run.initial_state);
             cx.update_editor(|editor, window, cx| {
-                editor.show_completions(
-                    &ShowCompletions {
-                        trigger: None,
-                        snippets_only: false,
-                    },
-                    window,
-                    cx,
-                );
+                editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
             });
 
             let counter = Arc::new(AtomicUsize::new(0));
@@ -13809,14 +13802,7 @@ async fn test_completion_with_mode_specified_by_action(cx: &mut TestAppContext)
 
     cx.set_state(initial_state);
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
 
     let counter = Arc::new(AtomicUsize::new(0));
@@ -13852,14 +13838,7 @@ async fn test_completion_with_mode_specified_by_action(cx: &mut TestAppContext)
 
     cx.set_state(initial_state);
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     handle_completion_request_with_insert_and_replace(
         &mut cx,
@@ -13946,14 +13925,7 @@ async fn test_completion_replacing_surrounding_text_with_multicursors(cx: &mut T
     "};
     cx.set_state(initial_state);
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     handle_completion_request_with_insert_and_replace(
         &mut cx,
@@ -14007,14 +13979,7 @@ async fn test_completion_replacing_surrounding_text_with_multicursors(cx: &mut T
     "};
     cx.set_state(initial_state);
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     handle_completion_request_with_insert_and_replace(
         &mut cx,
@@ -14063,14 +14028,7 @@ async fn test_completion_replacing_surrounding_text_with_multicursors(cx: &mut T
     "};
     cx.set_state(initial_state);
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     handle_completion_request_with_insert_and_replace(
         &mut cx,
@@ -14221,14 +14179,7 @@ async fn test_completion_in_multibuffer_with_replace_range(cx: &mut TestAppConte
     });
 
     editor.update_in(cx, |editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
 
     fake_server
@@ -14467,14 +14418,7 @@ async fn test_completion(cx: &mut TestAppContext) {
     cx.assert_editor_state("editor.cloˇ");
     assert!(cx.editor(|e, _, _| e.context_menu.borrow_mut().is_none()));
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     handle_completion_request(
         "editor.<clo|>",
@@ -15326,7 +15270,6 @@ async fn test_as_is_completions(cx: &mut TestAppContext) {
         editor.show_completions(
             &ShowCompletions {
                 trigger: Some("\n".into()),
-                snippets_only: false,
             },
             window,
             cx,
@@ -15428,14 +15371,7 @@ int fn_branch(bool do_branch1, bool do_branch2);
             })))
         });
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     cx.executor().run_until_parked();
     cx.update_editor(|editor, window, cx| {
@@ -15484,14 +15420,7 @@ int fn_branch(bool do_branch1, bool do_branch2);
             })))
         });
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     cx.executor().run_until_parked();
     cx.update_editor(|editor, window, cx| {
@@ -18016,14 +17945,7 @@ async fn test_context_menus_hide_hover_popover(cx: &mut gpui::TestAppContext) {
             }
         });
     cx.update_editor(|editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     completion_requests.next().await;
     cx.condition(|editor, _| editor.context_menu_visible())
@@ -24419,14 +24341,7 @@ async fn test_html_linked_edits_on_completion(cx: &mut TestAppContext) {
             ])))
         });
     editor.update_in(cx, |editor, window, cx| {
-        editor.show_completions(
-            &ShowCompletions {
-                trigger: None,
-                snippets_only: false,
-            },
-            window,
-            cx,
-        );
+        editor.show_completions(&ShowCompletions { trigger: None }, window, cx);
     });
     cx.run_until_parked();
     completion_handle.next().await.unwrap();

crates/inspector_ui/src/div_inspector.rs 🔗

@@ -645,7 +645,7 @@ impl CompletionProvider for RustStyleCompletionProvider {
         buffer: &Entity<Buffer>,
         position: Anchor,
         _: editor::CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> Task<Result<Vec<CompletionResponse>>> {

crates/keymap_editor/src/keymap_editor.rs 🔗

@@ -2911,7 +2911,7 @@ impl CompletionProvider for KeyContextCompletionProvider {
         buffer: &Entity<language::Buffer>,
         buffer_position: language::Anchor,
         _trigger: editor::CompletionContext,
-        _snippets_only: bool,
+        _text: Option<&str>,
         _window: &mut Window,
         cx: &mut Context<Editor>,
     ) -> gpui::Task<anyhow::Result<Vec<project::CompletionResponse>>> {