Recognize __ prefixes for symbol search query

This commit is contained in:
Lukas Wirth 2024-06-04 10:36:04 +02:00
parent 042bd0b78d
commit 0110cfcae0
2 changed files with 8 additions and 16 deletions

View file

@ -53,7 +53,6 @@ pub struct Query {
case_sensitive: bool, case_sensitive: bool,
only_types: bool, only_types: bool,
libs: bool, libs: bool,
include_hidden: bool,
} }
impl Query { impl Query {
@ -67,14 +66,9 @@ impl Query {
mode: SearchMode::Fuzzy, mode: SearchMode::Fuzzy,
assoc_mode: AssocSearchMode::Include, assoc_mode: AssocSearchMode::Include,
case_sensitive: false, case_sensitive: false,
include_hidden: false,
} }
} }
pub fn include_hidden(&mut self) {
self.include_hidden = true;
}
pub fn only_types(&mut self) { pub fn only_types(&mut self) {
self.only_types = true; self.only_types = true;
} }
@ -363,6 +357,7 @@ impl Query {
mut stream: fst::map::Union<'_>, mut stream: fst::map::Union<'_>,
mut cb: impl FnMut(&'sym FileSymbol), mut cb: impl FnMut(&'sym FileSymbol),
) { ) {
let ignore_underscore_prefixed = !self.query.starts_with("__");
while let Some((_, indexed_values)) = stream.next() { while let Some((_, indexed_values)) = stream.next() {
for &IndexedValue { index, value } in indexed_values { for &IndexedValue { index, value } in indexed_values {
let symbol_index = &indices[index]; 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) { if non_type_for_type_only_query || !self.matches_assoc_mode(symbol.is_assoc) {
continue; 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; continue;
} }
if self.mode.check(&self.query, self.case_sensitive, &symbol.name) { 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 { fn matches_assoc_mode(&self, is_trait_assoc_item: bool) -> bool {
!matches!( !matches!(
(is_trait_assoc_item, self.assoc_mode), (is_trait_assoc_item, self.assoc_mode),

View file

@ -940,11 +940,12 @@ static __FOO_CALLSITE: () = ();
// It doesn't show the hidden symbol // It doesn't show the hidden symbol
let navs = analysis.symbol_search(Query::new("foo".to_owned()), !0).unwrap(); let navs = analysis.symbol_search(Query::new("foo".to_owned()), !0).unwrap();
assert_eq!(navs.len(), 2); 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 // Unless we explicitly search for a `__` prefix
let mut query = Query::new("foo".to_owned()); let query = Query::new("__foo".to_owned());
query.include_hidden();
let navs = analysis.symbol_search(query, !0).unwrap(); let navs = analysis.symbol_search(query, !0).unwrap();
assert_eq!(navs.len(), 3); assert_eq!(navs.len(), 1);
} }
} }