Skip to content

Commit ac79d3f

Browse files
Merge pull request rust-lang#19996 from LHolten/exclude-imports
Add support for excluding imports from symbol search
2 parents 2ee912c + 6cf1a89 commit ac79d3f

File tree

10 files changed

+214
-1
lines changed

10 files changed

+214
-1
lines changed

src/tools/rust-analyzer/crates/hir/src/symbols.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct FileSymbol {
3333
/// Whether this symbol is a doc alias for the original symbol.
3434
pub is_alias: bool,
3535
pub is_assoc: bool,
36+
pub is_import: bool,
3637
pub do_not_complete: Complete,
3738
}
3839

@@ -196,6 +197,7 @@ impl<'a> SymbolCollector<'a> {
196197
loc: dec_loc,
197198
is_alias: false,
198199
is_assoc: false,
200+
is_import: true,
199201
do_not_complete: Complete::Yes,
200202
});
201203
};
@@ -226,6 +228,7 @@ impl<'a> SymbolCollector<'a> {
226228
loc: dec_loc,
227229
is_alias: false,
228230
is_assoc: false,
231+
is_import: false,
229232
do_not_complete: Complete::Yes,
230233
});
231234
};
@@ -397,6 +400,7 @@ impl<'a> SymbolCollector<'a> {
397400
container_name: self.current_container_name.clone(),
398401
is_alias: true,
399402
is_assoc,
403+
is_import: false,
400404
do_not_complete,
401405
});
402406
}
@@ -409,6 +413,7 @@ impl<'a> SymbolCollector<'a> {
409413
loc: dec_loc,
410414
is_alias: false,
411415
is_assoc,
416+
is_import: false,
412417
do_not_complete,
413418
});
414419

@@ -441,6 +446,7 @@ impl<'a> SymbolCollector<'a> {
441446
container_name: self.current_container_name.clone(),
442447
is_alias: true,
443448
is_assoc: false,
449+
is_import: false,
444450
do_not_complete,
445451
});
446452
}
@@ -453,6 +459,7 @@ impl<'a> SymbolCollector<'a> {
453459
loc: dec_loc,
454460
is_alias: false,
455461
is_assoc: false,
462+
is_import: false,
456463
do_not_complete,
457464
});
458465
}

src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct Query {
5050
case_sensitive: bool,
5151
only_types: bool,
5252
libs: bool,
53+
exclude_imports: bool,
5354
}
5455

5556
impl Query {
@@ -63,6 +64,7 @@ impl Query {
6364
mode: SearchMode::Fuzzy,
6465
assoc_mode: AssocSearchMode::Include,
6566
case_sensitive: false,
67+
exclude_imports: false,
6668
}
6769
}
6870

@@ -94,6 +96,10 @@ impl Query {
9496
pub fn case_sensitive(&mut self) {
9597
self.case_sensitive = true;
9698
}
99+
100+
pub fn exclude_imports(&mut self) {
101+
self.exclude_imports = true;
102+
}
97103
}
98104

99105
#[query_group::query_group]
@@ -362,6 +368,9 @@ impl Query {
362368
if ignore_underscore_prefixed && symbol_name.starts_with("__") {
363369
continue;
364370
}
371+
if self.exclude_imports && symbol.is_import {
372+
continue;
373+
}
365374
if self.mode.check(&self.query, self.case_sensitive, symbol_name) {
366375
if let Some(b) = cb(symbol).break_value() {
367376
return Some(b);
@@ -385,7 +394,8 @@ impl Query {
385394
mod tests {
386395

387396
use expect_test::expect_file;
388-
use test_fixture::WithFixture;
397+
use salsa::Durability;
398+
use test_fixture::{WORKSPACE, WithFixture};
389399

390400
use super::*;
391401

@@ -506,4 +516,31 @@ struct Duplicate;
506516

507517
expect_file!["./test_data/test_doc_alias.txt"].assert_debug_eq(&symbols);
508518
}
519+
520+
#[test]
521+
fn test_exclude_imports() {
522+
let (mut db, _) = RootDatabase::with_many_files(
523+
r#"
524+
//- /lib.rs
525+
mod foo;
526+
pub use foo::Foo;
527+
528+
//- /foo.rs
529+
pub struct Foo;
530+
"#,
531+
);
532+
533+
let mut local_roots = FxHashSet::default();
534+
local_roots.insert(WORKSPACE);
535+
db.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
536+
537+
let mut query = Query::new("Foo".to_owned());
538+
let mut symbols = world_symbols(&db, query.clone());
539+
symbols.sort_by_key(|x| x.is_import);
540+
expect_file!["./test_data/test_symbols_with_imports.txt"].assert_debug_eq(&symbols);
541+
542+
query.exclude_imports();
543+
let symbols = world_symbols(&db, query);
544+
expect_file!["./test_data/test_symbols_exclude_imports.txt"].assert_debug_eq(&symbols);
545+
}
509546
}

src/tools/rust-analyzer/crates/ide-db/src/test_data/test_doc_alias.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
container_name: None,
4242
is_alias: false,
4343
is_assoc: false,
44+
is_import: false,
4445
do_not_complete: Yes,
4546
},
4647
FileSymbol {
@@ -74,6 +75,7 @@
7475
container_name: None,
7576
is_alias: false,
7677
is_assoc: false,
78+
is_import: false,
7779
do_not_complete: Yes,
7880
},
7981
FileSymbol {
@@ -107,6 +109,7 @@
107109
container_name: None,
108110
is_alias: true,
109111
is_assoc: false,
112+
is_import: false,
110113
do_not_complete: Yes,
111114
},
112115
FileSymbol {
@@ -140,6 +143,7 @@
140143
container_name: None,
141144
is_alias: true,
142145
is_assoc: false,
146+
is_import: false,
143147
do_not_complete: Yes,
144148
},
145149
FileSymbol {
@@ -173,6 +177,7 @@
173177
container_name: None,
174178
is_alias: true,
175179
is_assoc: false,
180+
is_import: false,
176181
do_not_complete: Yes,
177182
},
178183
FileSymbol {
@@ -206,6 +211,7 @@
206211
container_name: None,
207212
is_alias: true,
208213
is_assoc: false,
214+
is_import: false,
209215
do_not_complete: Yes,
210216
},
211217
FileSymbol {
@@ -239,6 +245,7 @@
239245
container_name: None,
240246
is_alias: true,
241247
is_assoc: false,
248+
is_import: false,
242249
do_not_complete: Yes,
243250
},
244251
],

0 commit comments

Comments
 (0)