about summary refs log tree commit diff
path: root/src/tools/rust-analyzer
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-04 08:37:56 +0000
committerbors <bors@rust-lang.org>2024-06-04 08:37:56 +0000
commit33a902147f330d89987ea4547fdf90dff8eb43b8 (patch)
tree5a62aabb354f9a9e55855a45870c6ada097341b9 /src/tools/rust-analyzer
parent845754af9c85b51c42686078d55485cad3c3eab1 (diff)
parent93b87e9719bfbe1e981639401f895c98ef0ce75f (diff)
downloadrust-33a902147f330d89987ea4547fdf90dff8eb43b8.tar.gz
rust-33a902147f330d89987ea4547fdf90dff8eb43b8.zip
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.
Diffstat (limited to 'src/tools/rust-analyzer')
-rw-r--r--src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs8
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/navigation_target.rs22
2 files changed, 29 insertions, 1 deletions
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 12085f9ebd2..3539c7fbf8f 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<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/src/tools/rust-analyzer/crates/ide/src/navigation_target.rs b/src/tools/rust-analyzer/crates/ide/src/navigation_target.rs
index fc836d55409..a93a8da57e3 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);
+    }
 }