Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: hide double underscored symbols from symbol search #17282

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/ide-db/src/symbol_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct Query {
case_sensitive: bool,
only_types: bool,
libs: bool,
include_hidden: bool,
}

impl Query {
Expand All @@ -66,9 +67,14 @@ impl Query {
mode: SearchMode::Fuzzy,
assoc_mode: AssocSearchMode::Include,
case_sensitive: false,
include_hidden: false,
Veykril marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub fn include_hidden(&mut self) {
self.include_hidden = true;
}

pub fn only_types(&mut self) {
self.only_types = true;
}
Expand Down Expand Up @@ -192,7 +198,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
Expand Down Expand Up @@ -374,6 +381,9 @@ impl Query {
if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) {
continue;
}
if self.should_hide_query(symbol) {
continue;
}
if self.mode.check(&self.query, self.case_sensitive, &symbol.name) {
cb(symbol);
}
Expand All @@ -382,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),
Expand Down
21 changes: 21 additions & 0 deletions crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}