From bdd0cbb71737bb206312fa96bbe9812aa12eea03 Mon Sep 17 00:00:00 2001 From: redforks Date: Thu, 17 Apr 2025 22:43:49 +0800 Subject: [PATCH] Fix snippets from extensions being listed twice (#28940) lookup_snippets() merges global snippets and extension snippets, but global_snippets::lookup_snippets() also returns extension snippets, make them double Closes #28661 Release Notes: - Fixed a bug where extension provided snippets were being displayed in duplicate. --- Cargo.lock | 1 + crates/snippet_provider/Cargo.toml | 5 +++ crates/snippet_provider/src/lib.rs | 49 +++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f4c7c67f1..6a290a1faa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13330,6 +13330,7 @@ dependencies = [ "fs", "futures 0.3.31", "gpui", + "indoc", "parking_lot", "paths", "schemars", diff --git a/crates/snippet_provider/Cargo.toml b/crates/snippet_provider/Cargo.toml index f523d052e0..3e47c96c66 100644 --- a/crates/snippet_provider/Cargo.toml +++ b/crates/snippet_provider/Cargo.toml @@ -23,3 +23,8 @@ snippet.workspace = true util.workspace = true schemars.workspace = true workspace-hack.workspace = true + +[dev-dependencies] +fs = { workspace = true, features = ["test-support"] } +gpui = { workspace = true, features = ["test-support"] } +indoc.workspace = true diff --git a/crates/snippet_provider/src/lib.rs b/crates/snippet_provider/src/lib.rs index 3058b4ac70..82bae322e4 100644 --- a/crates/snippet_provider/src/lib.rs +++ b/crates/snippet_provider/src/lib.rs @@ -222,15 +222,15 @@ impl SnippetProvider { .lookup_snippets::(language, cx), ); } + + let Some(registry) = SnippetRegistry::try_global(cx) else { + return user_snippets; + }; + + let registry_snippets = registry.get_snippets(language); + user_snippets.extend(registry_snippets); } - let Some(registry) = SnippetRegistry::try_global(cx) else { - return user_snippets; - }; - - let registry_snippets = registry.get_snippets(language); - user_snippets.extend(registry_snippets); - user_snippets } @@ -244,3 +244,38 @@ impl SnippetProvider { requested_snippets } } + +#[cfg(test)] +mod tests { + use super::*; + use fs::FakeFs; + use gpui; + use gpui::TestAppContext; + use indoc::indoc; + + #[gpui::test] + fn test_lookup_snippets_dup_registry_snippets(cx: &mut TestAppContext) { + let fs = FakeFs::new(cx.background_executor.clone()); + cx.update(|cx| { + SnippetRegistry::init_global(cx); + SnippetRegistry::global(cx) + .register_snippets( + "ruby".as_ref(), + indoc! {r#" + { + "Log to console": { + "prefix": "log", + "body": ["console.info(\"Hello, ${1:World}!\")", "$0"], + "description": "Logs to console" + } + } + "#}, + ) + .unwrap(); + let provider = SnippetProvider::new(fs.clone(), Default::default(), cx); + cx.update_entity(&provider, |provider, cx| { + assert_eq!(1, provider.snippets_for(Some("ruby".to_owned()), cx).len()); + }); + }); + } +}