diff --git a/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs b/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs index 12085f9ebd25..3539c7fbf8f9 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs @@ -192,7 +192,8 @@ impl std::ops::Deref for Snap { // 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/src/tools/rust-analyzer/crates/ide/src/navigation_target.rs b/src/tools/rust-analyzer/crates/ide/src/navigation_target.rs index fc836d55409d..a93a8da57e38 100644 --- a/src/tools/rust-analyzer/crates/ide/src/navigation_target.rs +++ b/src/tools/rust-analyzer/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); + } }