Skip to content

Commit

Permalink
Feat: hide double underscored symbols from symbol search
Browse files Browse the repository at this point in the history
  • Loading branch information
jkelleyrtp committed May 22, 2024
1 parent ad810a5 commit ada256c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/tools/rust-analyzer/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,
}
}

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.include_hidden && symbol.name.starts_with("__") {
continue;
}
if self.mode.check(&self.query, self.case_sensitive, &symbol.name) {
cb(symbol);
}
Expand Down
21 changes: 21 additions & 0 deletions src/tools/rust-analyzer/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);
}
}

0 comments on commit ada256c

Please sign in to comment.