Skip to content

Commit e38dfe5

Browse files
committed
Auto merge of rust-lang#13186 - enomado:master, r=Veykril
Filter imports on find-all-references Attempt to rust-lang#13184
2 parents 352a5b8 + f7f4792 commit e38dfe5

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
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: 17 additions & 1 deletion
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,6 +80,10 @@ pub(crate) fn find_all_refs(
7980
retain_adt_literal_usages(&mut usages, def, sema);
8081
}
8182

83+
if exclude_imports {
84+
filter_import_references(&mut usages);
85+
}
86+
8287
let references = usages
8388
.into_iter()
8489
.map(|(file_id, refs)| {
@@ -112,6 +117,17 @@ pub(crate) fn find_all_refs(
112117
}
113118
}
114119

120+
fn filter_import_references(usages: &mut UsageSearchResult) {
121+
for (_file_id, refs) in &mut usages.references {
122+
refs.retain(|it| match it.name.as_name_ref() {
123+
Some(name_ref) => {
124+
!name_ref.syntax().ancestors().any(|it_ref| matches!(it_ref.kind(), USE))
125+
}
126+
None => true,
127+
});
128+
}
129+
}
130+
115131
pub(crate) fn find_defs<'a>(
116132
sema: &'a Semantics<'_, RootDatabase>,
117133
syntax: &SyntaxNode,
@@ -1094,7 +1110,7 @@ impl Foo {
10941110

10951111
fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect: Expect) {
10961112
let (analysis, pos) = fixture::position(ra_fixture);
1097-
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
1113+
let refs = analysis.find_all_refs(pos, search_scope, false).unwrap().unwrap();
10981114

10991115
let mut actual = String::new();
11001116
for refs in refs {

crates/rust-analyzer/src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ config_data! {
219219
files_excludeDirs: Vec<PathBuf> = "[]",
220220
/// Controls file watching implementation.
221221
files_watcher: FilesWatcherDef = "\"client\"",
222-
223222
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
224223
highlightRelated_breakPoints_enable: bool = "true",
225224
/// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`).
@@ -361,6 +360,9 @@ config_data! {
361360
/// this is rust-analyzer itself, but we override this in tests).
362361
procMacro_server: Option<PathBuf> = "null",
363362

363+
/// Exclude imports from find-all-references.
364+
references_excludeImports: bool = "false",
365+
364366
/// Command to be executed instead of 'cargo' for runnables.
365367
runnables_command: Option<String> = "null",
366368
/// Additional arguments to be passed to cargo for runnables such as
@@ -1151,6 +1153,10 @@ impl Config {
11511153
}
11521154
}
11531155

1156+
pub fn find_all_refs_exclude_imports(&self) -> bool {
1157+
self.data.references_excludeImports
1158+
}
1159+
11541160
pub fn snippet_cap(&self) -> bool {
11551161
self.experimental("snippetTextEdit")
11561162
}

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);

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ This config takes a map of crate names with the exported proc-macro names to ign
551551
Internal config, path to proc-macro server executable (typically,
552552
this is rust-analyzer itself, but we override this in tests).
553553
--
554+
[[rust-analyzer.references.excludeImports]]rust-analyzer.references.excludeImports (default: `false`)::
555+
+
556+
--
557+
Exclude imports from find-all-references.
558+
--
554559
[[rust-analyzer.runnables.command]]rust-analyzer.runnables.command (default: `null`)::
555560
+
556561
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,11 @@
10411041
"string"
10421042
]
10431043
},
1044+
"rust-analyzer.references.excludeImports": {
1045+
"markdownDescription": "Exclude imports from find-all-references.",
1046+
"default": false,
1047+
"type": "boolean"
1048+
},
10441049
"rust-analyzer.runnables.command": {
10451050
"markdownDescription": "Command to be executed instead of 'cargo' for runnables.",
10461051
"default": null,

0 commit comments

Comments
 (0)