Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #17282 - jkelleyrtp:jk/filter-by-underscorte, r=Veykril
Feat: hide double underscored symbols from symbol search Fixes #17272 by changing the default behavior of query to skip results that start with `__` (two underscores). Not sure if this has any far reaching implications - a review would help to understand if this is the right place to do the filtering, and if it's fine to do it by default on the query. If you type `__` as your search, then we'll show the matching double unders, just in case you actually need the symbol.
bors 2024-06-04
parent 09d7dcc · parent 0110cfc · commit 6bae8e3
-rw-r--r--crates/ide-db/src/symbol_index.rs8
-rw-r--r--crates/ide/src/navigation_target.rs22
2 files changed, 29 insertions, 1 deletions
diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs
index 12085f9ebd..3539c7fbf8 100644
--- a/crates/ide-db/src/symbol_index.rs
+++ b/crates/ide-db/src/symbol_index.rs
@@ -192,7 +192,8 @@ impl<DB> std::ops::Deref for Snap<DB> {
// Note that filtering does not currently work in VSCode due to the editor never
// sending the special symbols to the language server. Instead, you can configure
// the filtering via the `rust-analyzer.workspace.symbol.search.scope` and
-// `rust-analyzer.workspace.symbol.search.kind` settings.
+// `rust-analyzer.workspace.symbol.search.kind` settings. Symbols prefixed
+// with `__` are hidden from the search results unless configured otherwise.
//
// |===
// | Editor | Shortcut
@@ -356,6 +357,7 @@ impl Query {
mut stream: fst::map::Union<'_>,
mut cb: impl FnMut(&'sym FileSymbol),
) {
+ let ignore_underscore_prefixed = !self.query.starts_with("__");
while let Some((_, indexed_values)) = stream.next() {
for &IndexedValue { index, value } in indexed_values {
let symbol_index = &indices[index];
@@ -374,6 +376,10 @@ impl Query {
if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) {
continue;
}
+ // Hide symbols that start with `__` unless the query starts with `__`
+ if ignore_underscore_prefixed && symbol.name.starts_with("__") {
+ continue;
+ }
if self.mode.check(&self.query, self.case_sensitive, &symbol.name) {
cb(symbol);
}
diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs
index fc836d5540..a93a8da57e 100644
--- a/crates/ide/src/navigation_target.rs
+++ b/crates/ide/src/navigation_target.rs
@@ -926,4 +926,26 @@ struct Foo;
let navs = analysis.symbol_search(Query::new("foo".to_owned()), !0).unwrap();
assert_eq!(navs.len(), 2)
}
+
+ #[test]
+ fn test_ensure_hidden_symbols_are_not_returned() {
+ let (analysis, _) = fixture::file(
+ r#"
+fn foo() {}
+struct Foo;
+static __FOO_CALLSITE: () = ();
+"#,
+ );
+
+ // It doesn't show the hidden symbol
+ let navs = analysis.symbol_search(Query::new("foo".to_owned()), !0).unwrap();
+ assert_eq!(navs.len(), 2);
+ let navs = analysis.symbol_search(Query::new("_foo".to_owned()), !0).unwrap();
+ assert_eq!(navs.len(), 0);
+
+ // Unless we explicitly search for a `__` prefix
+ let query = Query::new("__foo".to_owned());
+ let navs = analysis.symbol_search(query, !0).unwrap();
+ assert_eq!(navs.len(), 1);
+ }
}