From 28ddddd09109982566dbec62b7c04f71f23d6b02 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 22 May 2024 13:43:07 -0700 Subject: [PATCH 1/4] Feat: hide double underscored symbols from symbol search --- crates/ide-db/src/symbol_index.rs | 12 +++++++++++- crates/ide/src/navigation_target.rs | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index 12085f9ebd..e14cf0eb1f 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -53,6 +53,7 @@ pub struct Query { case_sensitive: bool, only_types: bool, libs: bool, + include_hidden: bool, } impl Query { @@ -66,9 +67,14 @@ impl Query { mode: SearchMode::Fuzzy, assoc_mode: AssocSearchMode::Include, case_sensitive: false, + include_hidden: false, } } + pub fn include_hidden(&mut self) { + self.include_hidden = true; + } + pub fn only_types(&mut self) { self.only_types = true; } @@ -192,7 +198,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 @@ -374,6 +381,9 @@ impl Query { if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) { continue; } + if !self.include_hidden && 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..e40c7ecef0 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -926,4 +926,25 @@ 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); + + // Unless we configure a query to show hidden symbols + let mut query = Query::new("foo".to_owned()); + query.include_hidden(); + let navs = analysis.symbol_search(query, !0).unwrap(); + assert_eq!(navs.len(), 3); + } } From bdfcae556d3c54b0c138382c295207fe706230ef Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 22 May 2024 13:47:05 -0700 Subject: [PATCH 2/4] Allow searching with prefix --- crates/ide-db/src/symbol_index.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index e14cf0eb1f..5e1930e602 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -381,7 +381,7 @@ impl Query { if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) { continue; } - if !self.include_hidden && symbol.name.starts_with("__") { + if self.should_hide_query(&symbol) { continue; } if self.mode.check(&self.query, self.case_sensitive, &symbol.name) { @@ -392,6 +392,11 @@ impl Query { } } + fn should_hide_query(&self, symbol: &FileSymbol) -> bool { + // Hide symbols that start with `__` unless the query starts with `__` + !self.include_hidden && symbol.name.starts_with("__") && !self.query.starts_with("__") + } + fn matches_assoc_mode(&self, is_trait_assoc_item: bool) -> bool { !matches!( (is_trait_assoc_item, self.assoc_mode), From 042bd0b78d84498e53362d9bcce3a7db4c5326c5 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 22 May 2024 14:39:16 -0700 Subject: [PATCH 3/4] Fix: clippy --- crates/ide-db/src/symbol_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index 5e1930e602..365a727245 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -381,7 +381,7 @@ impl Query { if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) { continue; } - if self.should_hide_query(&symbol) { + if self.should_hide_query(symbol) { continue; } if self.mode.check(&self.query, self.case_sensitive, &symbol.name) { From 0110cfcae0e18627ab2d6fc8e60654d63991524b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 4 Jun 2024 10:36:04 +0200 Subject: [PATCH 4/4] Recognize `__` prefixes for symbol search query --- crates/ide-db/src/symbol_index.rs | 15 +++------------ crates/ide/src/navigation_target.rs | 9 +++++---- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index 365a727245..3539c7fbf8 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -53,7 +53,6 @@ pub struct Query { case_sensitive: bool, only_types: bool, libs: bool, - include_hidden: bool, } impl Query { @@ -67,14 +66,9 @@ impl Query { mode: SearchMode::Fuzzy, assoc_mode: AssocSearchMode::Include, case_sensitive: false, - include_hidden: false, } } - pub fn include_hidden(&mut self) { - self.include_hidden = true; - } - pub fn only_types(&mut self) { self.only_types = true; } @@ -363,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]; @@ -381,7 +376,8 @@ impl Query { if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) { continue; } - if self.should_hide_query(symbol) { + // 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) { @@ -392,11 +388,6 @@ impl Query { } } - fn should_hide_query(&self, symbol: &FileSymbol) -> bool { - // Hide symbols that start with `__` unless the query starts with `__` - !self.include_hidden && symbol.name.starts_with("__") && !self.query.starts_with("__") - } - fn matches_assoc_mode(&self, is_trait_assoc_item: bool) -> bool { !matches!( (is_trait_assoc_item, self.assoc_mode), diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs index e40c7ecef0..a93a8da57e 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -940,11 +940,12 @@ 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 configure a query to show hidden symbols - let mut query = Query::new("foo".to_owned()); - query.include_hidden(); + // 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(), 3); + assert_eq!(navs.len(), 1); } }