Skip to content

Commit 6f303f4

Browse files
committed
feat: enable excluding refs search results in test
1 parent d410d4a commit 6f303f4

File tree

8 files changed

+60
-3
lines changed

8 files changed

+60
-3
lines changed

crates/ide-db/src/search.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use memchr::memmem::Finder;
1515
use nohash_hasher::IntMap;
1616
use once_cell::unsync::Lazy;
1717
use parser::SyntaxKind;
18-
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxElement, TextRange, TextSize};
18+
use syntax::{
19+
ast::{self, HasAttrs as _},
20+
match_ast, AstNode, AstToken, SyntaxElement, TextRange, TextSize,
21+
};
1922
use triomphe::Arc;
2023

2124
use crate::{
@@ -134,6 +137,7 @@ pub enum ReferenceCategory {
134137
// FIXME: Some day should be able to search in doc comments. Would probably
135138
// need to switch from enum to bitflags then?
136139
// DocComment
140+
Test,
137141
}
138142

139143
/// Generally, `search_scope` returns files that might contain references for the element.
@@ -872,6 +876,10 @@ fn def_to_ty(sema: &Semantics<'_, RootDatabase>, def: &Definition) -> Option<hir
872876

873877
impl ReferenceCategory {
874878
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
879+
if is_name_ref_in_test(r) {
880+
return Some(ReferenceCategory::Test);
881+
}
882+
875883
// Only Locals and Fields have accesses for now.
876884
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
877885
return is_name_ref_in_import(r).then_some(ReferenceCategory::Import);
@@ -910,3 +918,30 @@ fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool {
910918
.and_then(|it| it.parent_path().top_path().syntax().parent())
911919
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
912920
}
921+
922+
fn is_name_ref_in_test(name_ref: &ast::NameRef) -> bool {
923+
let mode = name_ref.syntax().ancestors().find_map(|node| {
924+
match_ast! {
925+
match node {
926+
ast::Fn(f) => {
927+
let attrs = f.attrs();
928+
let mut is_test = false;
929+
for attr in attrs {
930+
if attr.to_string() == "#[test]" {
931+
is_test = true;
932+
break;
933+
}
934+
}
935+
if is_test {
936+
Some(ReferenceCategory::Test)
937+
}
938+
else {
939+
None
940+
}
941+
},
942+
_ => None
943+
}
944+
}
945+
});
946+
mode.is_some()
947+
}

crates/ide/src/highlight_related.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ mod tests {
519519
ReferenceCategory::Read => "read",
520520
ReferenceCategory::Write => "write",
521521
ReferenceCategory::Import => "import",
522+
ReferenceCategory::Test => "test",
522523
}
523524
.to_string()
524525
}),

crates/ide/src/references.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ fn main() {
454454
"#]],
455455
);
456456
}
457+
457458
#[test]
458459
fn test_variant_tuple_before_paren() {
459460
check(
@@ -1435,7 +1436,7 @@ fn test$0() {
14351436
expect![[r#"
14361437
test Function FileId(0) 0..33 11..15
14371438
1438-
FileId(0) 24..28
1439+
FileId(0) 24..28 Test
14391440
"#]],
14401441
);
14411442
}

crates/rust-analyzer/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ config_data! {
494494
/// Exclude imports from find-all-references.
495495
references_excludeImports: bool = "false",
496496

497+
/// Exclude tests from find-all-references.
498+
references_excludeTests: bool = "false",
499+
497500
/// Allow renaming of items not belonging to the loaded workspaces.
498501
rename_allowExternalItems: bool = "false",
499502

@@ -1545,6 +1548,10 @@ impl Config {
15451548
self.data.references_excludeImports
15461549
}
15471550

1551+
pub fn find_all_refs_exclude_tests(&self) -> bool {
1552+
self.data.references_excludeTests
1553+
}
1554+
15481555
pub fn snippet_cap(&self) -> bool {
15491556
self.experimental("snippetTextEdit")
15501557
}

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ pub(crate) fn handle_references(
10551055
let position = from_proto::file_position(&snap, params.text_document_position)?;
10561056

10571057
let exclude_imports = snap.config.find_all_refs_exclude_imports();
1058+
let exclude_tests = snap.config.find_all_refs_exclude_tests();
10581059

10591060
let refs = match snap.analysis.find_all_refs(position, None)? {
10601061
None => return Ok(None),
@@ -1078,7 +1079,8 @@ pub(crate) fn handle_references(
10781079
.flat_map(|(file_id, refs)| {
10791080
refs.into_iter()
10801081
.filter(|&(_, category)| {
1081-
!exclude_imports || category != Some(ReferenceCategory::Import)
1082+
(!exclude_imports || category != Some(ReferenceCategory::Import))
1083+
&& (!exclude_tests || category != Some(ReferenceCategory::Test))
10821084
})
10831085
.map(move |(range, _)| FileRange { file_id, range })
10841086
})

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub(crate) fn document_highlight_kind(
9292
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
9393
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
9494
ReferenceCategory::Import => None,
95+
ReferenceCategory::Test => None,
9596
}
9697
}
9798

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ Internal config, path to proc-macro server executable.
777777
--
778778
Exclude imports from find-all-references.
779779
--
780+
[[rust-analyzer.references.excludeTests]]rust-analyzer.references.excludeTests (default: `false`)::
781+
+
782+
--
783+
Exclude tests from find-all-references.
784+
--
780785
[[rust-analyzer.rename.allowExternalItems]]rust-analyzer.rename.allowExternalItems (default: `false`)::
781786
+
782787
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,11 @@
15051505
"default": false,
15061506
"type": "boolean"
15071507
},
1508+
"rust-analyzer.references.excludeTests": {
1509+
"markdownDescription": "Exclude tests from find-all-references.",
1510+
"default": false,
1511+
"type": "boolean"
1512+
},
15081513
"rust-analyzer.rename.allowExternalItems": {
15091514
"markdownDescription": "Allow renaming of items not belonging to the loaded workspaces.",
15101515
"default": false,

0 commit comments

Comments
 (0)