Skip to content

Commit 9f6553e

Browse files
committed
add config for import filtering
1 parent eba54c2 commit 9f6553e

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

crates/ide/src/annotations.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation)
158158
&Semantics::new(db),
159159
FilePosition { file_id, offset: annotation.range.start() },
160160
None,
161+
false,
161162
)
162163
.map(|result| {
163164
result

crates/ide/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,11 @@ impl Analysis {
425425
&self,
426426
position: FilePosition,
427427
search_scope: Option<SearchScope>,
428+
exclude_imports: bool,
428429
) -> Cancellable<Option<Vec<ReferenceSearchResult>>> {
429-
self.with_db(|db| references::find_all_refs(&Semantics::new(db), position, search_scope))
430+
self.with_db(|db| {
431+
references::find_all_refs(&Semantics::new(db), position, search_scope, exclude_imports)
432+
})
430433
}
431434

432435
/// Finds all methods and free functions for the file. Does not return tests!

crates/ide/src/references.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub(crate) fn find_all_refs(
5454
sema: &Semantics<'_, RootDatabase>,
5555
position: FilePosition,
5656
search_scope: Option<SearchScope>,
57+
exclude_imports: bool,
5758
) -> Option<Vec<ReferenceSearchResult>> {
5859
let _p = profile::span("find_all_refs");
5960
let syntax = sema.parse(position.file_id).syntax().clone();
@@ -79,7 +80,9 @@ pub(crate) fn find_all_refs(
7980
retain_adt_literal_usages(&mut usages, def, sema);
8081
}
8182

82-
retain_import_usages(&mut usages);
83+
if exclude_imports {
84+
filter_import_references(&mut usages);
85+
}
8386

8487
let references = usages
8588
.into_iter()
@@ -114,7 +117,7 @@ pub(crate) fn find_all_refs(
114117
}
115118
}
116119

117-
fn retain_import_usages(usages: &mut UsageSearchResult) {
120+
fn filter_import_references(usages: &mut UsageSearchResult) {
118121
// todo use this https://github.com/rust-lang/rust-analyzer/blob/master/crates/rust-analyzer/src/config.rs#L432
119122

120123
for (_file_id, refs) in &mut usages.references {
@@ -1109,7 +1112,7 @@ impl Foo {
11091112

11101113
fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect: Expect) {
11111114
let (analysis, pos) = fixture::position(ra_fixture);
1112-
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
1115+
let refs = analysis.find_all_refs(pos, search_scope, false).unwrap().unwrap();
11131116

11141117
let mut actual = String::new();
11151118
for refs in refs {

crates/rust-analyzer/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ config_data! {
220220
/// Controls file watching implementation.
221221
files_watcher: FilesWatcherDef = "\"client\"",
222222

223+
/// Exclude imports in "Find All References"
224+
findAllRefs_excludeImports: bool = "false",
225+
223226
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
224227
highlightRelated_breakPoints_enable: bool = "true",
225228
/// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`).
@@ -1147,6 +1150,10 @@ impl Config {
11471150
}
11481151
}
11491152

1153+
pub fn find_all_refs_exclude_imports(&self) -> bool {
1154+
self.data.findAllRefs_excludeImports
1155+
}
1156+
11501157
pub fn snippet_cap(&self) -> bool {
11511158
self.experimental("snippetTextEdit")
11521159
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ pub(crate) fn handle_references(
10121012
let _p = profile::span("handle_references");
10131013
let position = from_proto::file_position(&snap, params.text_document_position)?;
10141014

1015-
let refs = match snap.analysis.find_all_refs(position, None)? {
1015+
let exclude_imports = snap.config.find_all_refs_exclude_imports();
1016+
1017+
let refs = match snap.analysis.find_all_refs(position, None, exclude_imports)? {
10161018
None => return Ok(None),
10171019
Some(refs) => refs,
10181020
};
@@ -1652,7 +1654,9 @@ fn show_ref_command_link(
16521654
position: &FilePosition,
16531655
) -> Option<lsp_ext::CommandLinkGroup> {
16541656
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1655-
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1657+
if let Some(ref_search_res) =
1658+
snap.analysis.find_all_refs(*position, None, false).unwrap_or(None)
1659+
{
16561660
let uri = to_proto::url(snap, position.file_id);
16571661
let line_index = snap.file_line_index(position.file_id).ok()?;
16581662
let position = to_proto::position(&line_index, position.offset);

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@
839839
"type": "integer",
840840
"minimum": 0
841841
},
842+
"rust-analyzer.findAllRefs.excludeImports": {
843+
"markdownDescription": "Exclude imports from Find All References results",
844+
"default": false,
845+
"type": "boolean"
846+
},
842847
"rust-analyzer.inlayHints.closureReturnTypeHints.enable": {
843848
"markdownDescription": "Whether to show inlay type hints for return types of closures.",
844849
"default": "never",

0 commit comments

Comments
 (0)