From 926afb05470bdec4cd2acd3f461165dff6331a15 Mon Sep 17 00:00:00 2001 From: HactarCE <6060305+HactarCE@users.noreply.github.com> Date: Wed, 29 Oct 2025 17:16:19 -0400 Subject: [PATCH] Remove `snippets_only` and replicate similar behavior but only in `impl CompletionProvider for Entity` Co-authored-by: Cole Miller --- .../agent_ui/src/acp/completion_provider.rs | 2 +- crates/agent_ui/src/acp/message_editor.rs | 2 +- .../src/context_picker/completion_provider.rs | 2 +- crates/agent_ui/src/slash_command.rs | 2 +- .../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(-) diff --git a/crates/agent_ui/src/acp/completion_provider.rs b/crates/agent_ui/src/acp/completion_provider.rs index 0329ee85e65a079771a0b878c38b743f7e874c8e..d3044b6c54d5797463cad9898377e4393b4f7185 100644 --- a/crates/agent_ui/src/acp/completion_provider.rs +++ b/crates/agent_ui/src/acp/completion_provider.rs @@ -702,7 +702,7 @@ impl CompletionProvider for ContextPickerCompletionProvider { buffer: &Entity, buffer_position: Anchor, _trigger: CompletionContext, - _snippets_only: bool, + _text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> Task>> { diff --git a/crates/agent_ui/src/acp/message_editor.rs b/crates/agent_ui/src/acp/message_editor.rs index e06f5af0f28eadb0533b732ce8233dd7689e096d..994233bb30ca07e5be6979d17357f4a2c689d8e0 100644 --- a/crates/agent_ui/src/acp/message_editor.rs +++ b/crates/agent_ui/src/acp/message_editor.rs @@ -1670,7 +1670,7 @@ mod tests { trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER, trigger_character: Some("@".into()), }, - false, + "@", window, cx, ) diff --git a/crates/agent_ui/src/context_picker/completion_provider.rs b/crates/agent_ui/src/context_picker/completion_provider.rs index fc77f92c8fd4cb5b6d8cbe4095904aa3ffac3741..3d2d3dab67408ed8d7633fac1389485a791a9324 100644 --- a/crates/agent_ui/src/context_picker/completion_provider.rs +++ b/crates/agent_ui/src/context_picker/completion_provider.rs @@ -745,7 +745,7 @@ impl CompletionProvider for ContextPickerCompletionProvider { buffer: &Entity, buffer_position: Anchor, _trigger: CompletionContext, - _snippets_only: bool, + _text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> Task>> { diff --git a/crates/agent_ui/src/slash_command.rs b/crates/agent_ui/src/slash_command.rs index 496a71f6f9229e2d20e9eff2bca7fff7724c1f38..eda0f35f4cd69866545311802b69d04ce2bbe0b8 100644 --- a/crates/agent_ui/src/slash_command.rs +++ b/crates/agent_ui/src/slash_command.rs @@ -265,7 +265,7 @@ impl CompletionProvider for SlashCommandCompletionProvider { buffer: &Entity, buffer_position: Anchor, _: editor::CompletionContext, - _snippets_only: bool, + _text: Option<&str>, window: &mut Window, cx: &mut Context, ) -> Task>> { diff --git a/crates/debugger_ui/src/session/running/console.rs b/crates/debugger_ui/src/session/running/console.rs index b1ed1a501b21a6519da099986ffe59dd2ec3d37a..68b7b80ad576a9772d8464d81cbb08488d7450f1 100644 --- a/crates/debugger_ui/src/session/running/console.rs +++ b/crates/debugger_ui/src/session/running/console.rs @@ -520,7 +520,7 @@ impl CompletionProvider for ConsoleQueryBarCompletionProvider { buffer: &Entity, buffer_position: language::Anchor, _trigger: editor::CompletionContext, - _snippets_only: bool, + _text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> Task>> { diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 11b404db5e3e50b613334e05558244329e11389d..810b84efcd40de6e507dfe12b1a1a7f89d2ec4cf 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -220,8 +220,6 @@ pub struct ExpandExcerptsDown { pub struct ShowCompletions { #[serde(default)] pub(super) trigger: Option, - #[serde(default)] - pub(super) snippets_only: bool, } /// Handles text input in the editor. diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index ca0fe99ccec0c4d1e84fbc666f3641956d62d1e7..2f161afbeab739081bb8eefae07d39e70d1649d6 100644 --- a/crates/editor/src/editor.rs +++ b/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.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, trigger: Option<&str>, - snippets_only: bool, window: &mut Window, cx: &mut Context, ) { @@ -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_position: text::Anchor, trigger: CompletionContext, - snippets_only: bool, + text: Option<&str>, window: &mut Window, cx: &mut Context, ) -> Task>>; @@ -23036,19 +23022,38 @@ impl CompletionProvider for Entity { buffer: &Entity, buffer_position: text::Anchor, options: CompletionContext, - snippets_only: bool, + text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> Task>> { 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 { menu_is_open: bool, cx: &mut Context, ) -> 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 { 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; } diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index f1a0029d0e39b9d6e2fdc7e5663954ce724115ae..6c69239214c855ffcc70619900c732e1ec7f36bc 100644 --- a/crates/editor/src/editor_tests.rs +++ b/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.", @@ -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(); diff --git a/crates/inspector_ui/src/div_inspector.rs b/crates/inspector_ui/src/div_inspector.rs index 7088a98727e7bd605f7aa6b8a373df7fe067189c..081a1cd8a39f09ca65312bdbc2bea69dfb9b72d8 100644 --- a/crates/inspector_ui/src/div_inspector.rs +++ b/crates/inspector_ui/src/div_inspector.rs @@ -645,7 +645,7 @@ impl CompletionProvider for RustStyleCompletionProvider { buffer: &Entity, position: Anchor, _: editor::CompletionContext, - _snippets_only: bool, + _text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> Task>> { diff --git a/crates/keymap_editor/src/keymap_editor.rs b/crates/keymap_editor/src/keymap_editor.rs index ed433c59db5828e68a629d09c84da0e4d9a42a6a..0f686eadeb125cad79c706f53051a1f78967e877 100644 --- a/crates/keymap_editor/src/keymap_editor.rs +++ b/crates/keymap_editor/src/keymap_editor.rs @@ -2911,7 +2911,7 @@ impl CompletionProvider for KeyContextCompletionProvider { buffer: &Entity, buffer_position: language::Anchor, _trigger: editor::CompletionContext, - _snippets_only: bool, + _text: Option<&str>, _window: &mut Window, cx: &mut Context, ) -> gpui::Task>> {