mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
feat: enable excluding refs search results in test
This commit is contained in:
parent
d410d4a2ba
commit
6f303f49fe
8 changed files with 60 additions and 3 deletions
|
@ -15,7 +15,10 @@ use memchr::memmem::Finder;
|
||||||
use nohash_hasher::IntMap;
|
use nohash_hasher::IntMap;
|
||||||
use once_cell::unsync::Lazy;
|
use once_cell::unsync::Lazy;
|
||||||
use parser::SyntaxKind;
|
use parser::SyntaxKind;
|
||||||
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxElement, TextRange, TextSize};
|
use syntax::{
|
||||||
|
ast::{self, HasAttrs as _},
|
||||||
|
match_ast, AstNode, AstToken, SyntaxElement, TextRange, TextSize,
|
||||||
|
};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -134,6 +137,7 @@ pub enum ReferenceCategory {
|
||||||
// FIXME: Some day should be able to search in doc comments. Would probably
|
// FIXME: Some day should be able to search in doc comments. Would probably
|
||||||
// need to switch from enum to bitflags then?
|
// need to switch from enum to bitflags then?
|
||||||
// DocComment
|
// DocComment
|
||||||
|
Test,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generally, `search_scope` returns files that might contain references for the element.
|
/// 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
|
||||||
|
|
||||||
impl ReferenceCategory {
|
impl ReferenceCategory {
|
||||||
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
|
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
|
||||||
|
if is_name_ref_in_test(r) {
|
||||||
|
return Some(ReferenceCategory::Test);
|
||||||
|
}
|
||||||
|
|
||||||
// Only Locals and Fields have accesses for now.
|
// Only Locals and Fields have accesses for now.
|
||||||
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
|
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
|
||||||
return is_name_ref_in_import(r).then_some(ReferenceCategory::Import);
|
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 {
|
||||||
.and_then(|it| it.parent_path().top_path().syntax().parent())
|
.and_then(|it| it.parent_path().top_path().syntax().parent())
|
||||||
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
|
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_name_ref_in_test(name_ref: &ast::NameRef) -> bool {
|
||||||
|
let mode = name_ref.syntax().ancestors().find_map(|node| {
|
||||||
|
match_ast! {
|
||||||
|
match node {
|
||||||
|
ast::Fn(f) => {
|
||||||
|
let attrs = f.attrs();
|
||||||
|
let mut is_test = false;
|
||||||
|
for attr in attrs {
|
||||||
|
if attr.to_string() == "#[test]" {
|
||||||
|
is_test = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if is_test {
|
||||||
|
Some(ReferenceCategory::Test)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mode.is_some()
|
||||||
|
}
|
||||||
|
|
|
@ -519,6 +519,7 @@ mod tests {
|
||||||
ReferenceCategory::Read => "read",
|
ReferenceCategory::Read => "read",
|
||||||
ReferenceCategory::Write => "write",
|
ReferenceCategory::Write => "write",
|
||||||
ReferenceCategory::Import => "import",
|
ReferenceCategory::Import => "import",
|
||||||
|
ReferenceCategory::Test => "test",
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -454,6 +454,7 @@ fn main() {
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_variant_tuple_before_paren() {
|
fn test_variant_tuple_before_paren() {
|
||||||
check(
|
check(
|
||||||
|
@ -1435,7 +1436,7 @@ fn test$0() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
test Function FileId(0) 0..33 11..15
|
test Function FileId(0) 0..33 11..15
|
||||||
|
|
||||||
FileId(0) 24..28
|
FileId(0) 24..28 Test
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -494,6 +494,9 @@ config_data! {
|
||||||
/// Exclude imports from find-all-references.
|
/// Exclude imports from find-all-references.
|
||||||
references_excludeImports: bool = "false",
|
references_excludeImports: bool = "false",
|
||||||
|
|
||||||
|
/// Exclude tests from find-all-references.
|
||||||
|
references_excludeTests: bool = "false",
|
||||||
|
|
||||||
/// Allow renaming of items not belonging to the loaded workspaces.
|
/// Allow renaming of items not belonging to the loaded workspaces.
|
||||||
rename_allowExternalItems: bool = "false",
|
rename_allowExternalItems: bool = "false",
|
||||||
|
|
||||||
|
@ -1545,6 +1548,10 @@ impl Config {
|
||||||
self.data.references_excludeImports
|
self.data.references_excludeImports
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_all_refs_exclude_tests(&self) -> bool {
|
||||||
|
self.data.references_excludeTests
|
||||||
|
}
|
||||||
|
|
||||||
pub fn snippet_cap(&self) -> bool {
|
pub fn snippet_cap(&self) -> bool {
|
||||||
self.experimental("snippetTextEdit")
|
self.experimental("snippetTextEdit")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1055,6 +1055,7 @@ pub(crate) fn handle_references(
|
||||||
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
||||||
|
|
||||||
let exclude_imports = snap.config.find_all_refs_exclude_imports();
|
let exclude_imports = snap.config.find_all_refs_exclude_imports();
|
||||||
|
let exclude_tests = snap.config.find_all_refs_exclude_tests();
|
||||||
|
|
||||||
let refs = match snap.analysis.find_all_refs(position, None)? {
|
let refs = match snap.analysis.find_all_refs(position, None)? {
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
|
@ -1078,7 +1079,8 @@ pub(crate) fn handle_references(
|
||||||
.flat_map(|(file_id, refs)| {
|
.flat_map(|(file_id, refs)| {
|
||||||
refs.into_iter()
|
refs.into_iter()
|
||||||
.filter(|&(_, category)| {
|
.filter(|&(_, category)| {
|
||||||
!exclude_imports || category != Some(ReferenceCategory::Import)
|
(!exclude_imports || category != Some(ReferenceCategory::Import))
|
||||||
|
&& (!exclude_tests || category != Some(ReferenceCategory::Test))
|
||||||
})
|
})
|
||||||
.map(move |(range, _)| FileRange { file_id, range })
|
.map(move |(range, _)| FileRange { file_id, range })
|
||||||
})
|
})
|
||||||
|
|
|
@ -92,6 +92,7 @@ pub(crate) fn document_highlight_kind(
|
||||||
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
|
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
|
||||||
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
|
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
|
||||||
ReferenceCategory::Import => None,
|
ReferenceCategory::Import => None,
|
||||||
|
ReferenceCategory::Test => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -777,6 +777,11 @@ Internal config, path to proc-macro server executable.
|
||||||
--
|
--
|
||||||
Exclude imports from find-all-references.
|
Exclude imports from find-all-references.
|
||||||
--
|
--
|
||||||
|
[[rust-analyzer.references.excludeTests]]rust-analyzer.references.excludeTests (default: `false`)::
|
||||||
|
+
|
||||||
|
--
|
||||||
|
Exclude tests from find-all-references.
|
||||||
|
--
|
||||||
[[rust-analyzer.rename.allowExternalItems]]rust-analyzer.rename.allowExternalItems (default: `false`)::
|
[[rust-analyzer.rename.allowExternalItems]]rust-analyzer.rename.allowExternalItems (default: `false`)::
|
||||||
+
|
+
|
||||||
--
|
--
|
||||||
|
|
|
@ -1505,6 +1505,11 @@
|
||||||
"default": false,
|
"default": false,
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
|
"rust-analyzer.references.excludeTests": {
|
||||||
|
"markdownDescription": "Exclude tests from find-all-references.",
|
||||||
|
"default": false,
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"rust-analyzer.rename.allowExternalItems": {
|
"rust-analyzer.rename.allowExternalItems": {
|
||||||
"markdownDescription": "Allow renaming of items not belonging to the loaded workspaces.",
|
"markdownDescription": "Allow renaming of items not belonging to the loaded workspaces.",
|
||||||
"default": false,
|
"default": false,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue