Auto merge of #18291 - roife:fix-issue-18212, r=Veykril

feat: respect references.exclude_tests in call-hierarchy

close #18212

### Changes

1. feat: respect `references.exclude_tests` in call-hierarchy
2. Modified the description of `references.exclude_tests`
This commit is contained in:
bors 2024-10-14 12:50:40 +00:00
commit 7b2548bd8d
6 changed files with 141 additions and 20 deletions

View file

@ -19,6 +19,12 @@ pub struct CallItem {
pub ranges: Vec<FileRange>, pub ranges: Vec<FileRange>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CallHierarchyConfig {
/// Whether to exclude tests from the call hierarchy
pub exclude_tests: bool,
}
pub(crate) fn call_hierarchy( pub(crate) fn call_hierarchy(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,
@ -28,6 +34,7 @@ pub(crate) fn call_hierarchy(
pub(crate) fn incoming_calls( pub(crate) fn incoming_calls(
db: &RootDatabase, db: &RootDatabase,
CallHierarchyConfig { exclude_tests }: CallHierarchyConfig,
FilePosition { file_id, offset }: FilePosition, FilePosition { file_id, offset }: FilePosition,
) -> Option<Vec<CallItem>> { ) -> Option<Vec<CallItem>> {
let sema = &Semantics::new(db); let sema = &Semantics::new(db);
@ -56,11 +63,18 @@ pub(crate) fn incoming_calls(
references.iter().filter_map(|FileReference { name, .. }| name.as_name_ref()); references.iter().filter_map(|FileReference { name, .. }| name.as_name_ref());
for name in references { for name in references {
// This target is the containing function // This target is the containing function
let nav = sema.ancestors_with_macros(name.syntax().clone()).find_map(|node| { let def_nav = sema.ancestors_with_macros(name.syntax().clone()).find_map(|node| {
let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?; let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
def.try_to_nav(sema.db) // We should return def before check if it is a test, so that we
// will not continue to search for outer fn in nested fns
def.try_to_nav(sema.db).map(|nav| (def, nav))
}); });
if let Some(nav) = nav {
if let Some((def, nav)) = def_nav {
if exclude_tests && def.is_test(db) {
continue;
}
let range = sema.original_range(name.syntax()); let range = sema.original_range(name.syntax());
calls.add(nav.call_site, range.into()); calls.add(nav.call_site, range.into());
if let Some(other) = nav.def_site { if let Some(other) = nav.def_site {
@ -75,6 +89,7 @@ pub(crate) fn incoming_calls(
pub(crate) fn outgoing_calls( pub(crate) fn outgoing_calls(
db: &RootDatabase, db: &RootDatabase,
CallHierarchyConfig { exclude_tests }: CallHierarchyConfig,
FilePosition { file_id, offset }: FilePosition, FilePosition { file_id, offset }: FilePosition,
) -> Option<Vec<CallItem>> { ) -> Option<Vec<CallItem>> {
let sema = Semantics::new(db); let sema = Semantics::new(db);
@ -103,7 +118,12 @@ pub(crate) fn outgoing_calls(
let expr = call.expr()?; let expr = call.expr()?;
let callable = sema.type_of_expr(&expr)?.original.as_callable(db)?; let callable = sema.type_of_expr(&expr)?.original.as_callable(db)?;
match callable.kind() { match callable.kind() {
hir::CallableKind::Function(it) => it.try_to_nav(db), hir::CallableKind::Function(it) => {
if exclude_tests && it.is_test(db) {
return None;
}
it.try_to_nav(db)
}
hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(db), hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(db),
hir::CallableKind::TupleStruct(it) => it.try_to_nav(db), hir::CallableKind::TupleStruct(it) => it.try_to_nav(db),
_ => None, _ => None,
@ -112,6 +132,9 @@ pub(crate) fn outgoing_calls(
} }
ast::CallableExpr::MethodCall(expr) => { ast::CallableExpr::MethodCall(expr) => {
let function = sema.resolve_method_call(&expr)?; let function = sema.resolve_method_call(&expr)?;
if exclude_tests && function.is_test(db) {
return None;
}
function function
.try_to_nav(db) .try_to_nav(db)
.zip(Some(sema.original_range(expr.name_ref()?.syntax()))) .zip(Some(sema.original_range(expr.name_ref()?.syntax())))
@ -149,6 +172,7 @@ mod tests {
use crate::fixture; use crate::fixture;
fn check_hierarchy( fn check_hierarchy(
exclude_tests: bool,
ra_fixture: &str, ra_fixture: &str,
expected_nav: Expect, expected_nav: Expect,
expected_incoming: Expect, expected_incoming: Expect,
@ -172,18 +196,21 @@ mod tests {
let nav = navs.pop().unwrap(); let nav = navs.pop().unwrap();
expected_nav.assert_eq(&nav.debug_render()); expected_nav.assert_eq(&nav.debug_render());
let config = crate::CallHierarchyConfig { exclude_tests };
let item_pos = let item_pos =
FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() }; FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() };
let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap(); let incoming_calls = analysis.incoming_calls(config, item_pos).unwrap().unwrap();
expected_incoming.assert_eq(&incoming_calls.into_iter().map(debug_render).join("\n")); expected_incoming.assert_eq(&incoming_calls.into_iter().map(debug_render).join("\n"));
let outgoing_calls = analysis.outgoing_calls(item_pos).unwrap().unwrap(); let outgoing_calls = analysis.outgoing_calls(config, item_pos).unwrap().unwrap();
expected_outgoing.assert_eq(&outgoing_calls.into_iter().map(debug_render).join("\n")); expected_outgoing.assert_eq(&outgoing_calls.into_iter().map(debug_render).join("\n"));
} }
#[test] #[test]
fn test_call_hierarchy_on_ref() { fn test_call_hierarchy_on_ref() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn callee() {} fn callee() {}
@ -200,6 +227,7 @@ fn caller() {
#[test] #[test]
fn test_call_hierarchy_on_def() { fn test_call_hierarchy_on_def() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn call$0ee() {} fn call$0ee() {}
@ -216,6 +244,7 @@ fn caller() {
#[test] #[test]
fn test_call_hierarchy_in_same_fn() { fn test_call_hierarchy_in_same_fn() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn callee() {} fn callee() {}
@ -233,6 +262,7 @@ fn caller() {
#[test] #[test]
fn test_call_hierarchy_in_different_fn() { fn test_call_hierarchy_in_different_fn() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn callee() {} fn callee() {}
@ -255,6 +285,7 @@ fn caller2() {
#[test] #[test]
fn test_call_hierarchy_in_tests_mod() { fn test_call_hierarchy_in_tests_mod() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs cfg:test //- /lib.rs cfg:test
fn callee() {} fn callee() {}
@ -283,6 +314,7 @@ mod tests {
#[test] #[test]
fn test_call_hierarchy_in_different_files() { fn test_call_hierarchy_in_different_files() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
mod foo; mod foo;
@ -304,6 +336,7 @@ pub fn callee() {}
#[test] #[test]
fn test_call_hierarchy_outgoing() { fn test_call_hierarchy_outgoing() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn callee() {} fn callee() {}
@ -321,6 +354,7 @@ fn call$0er() {
#[test] #[test]
fn test_call_hierarchy_outgoing_in_different_files() { fn test_call_hierarchy_outgoing_in_different_files() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
mod foo; mod foo;
@ -342,6 +376,7 @@ pub fn callee() {}
#[test] #[test]
fn test_call_hierarchy_incoming_outgoing() { fn test_call_hierarchy_incoming_outgoing() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
fn caller1() { fn caller1() {
@ -365,6 +400,7 @@ fn caller3() {
#[test] #[test]
fn test_call_hierarchy_issue_5103() { fn test_call_hierarchy_issue_5103() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
fn a() { fn a() {
b() b()
@ -382,6 +418,7 @@ fn main() {
); );
check_hierarchy( check_hierarchy(
false,
r#" r#"
fn a() { fn a() {
b$0() b$0()
@ -402,6 +439,7 @@ fn main() {
#[test] #[test]
fn test_call_hierarchy_in_macros_incoming() { fn test_call_hierarchy_in_macros_incoming() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
macro_rules! define { macro_rules! define {
($ident:ident) => { ($ident:ident) => {
@ -423,6 +461,7 @@ fn caller() {
expect![[]], expect![[]],
); );
check_hierarchy( check_hierarchy(
false,
r#" r#"
macro_rules! define { macro_rules! define {
($ident:ident) => { ($ident:ident) => {
@ -448,6 +487,7 @@ fn caller() {
#[test] #[test]
fn test_call_hierarchy_in_macros_outgoing() { fn test_call_hierarchy_in_macros_outgoing() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
macro_rules! define { macro_rules! define {
($ident:ident) => { ($ident:ident) => {
@ -473,6 +513,7 @@ fn caller$0() {
#[test] #[test]
fn test_call_hierarchy_in_macros_incoming_different_files() { fn test_call_hierarchy_in_macros_incoming_different_files() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
#[macro_use] #[macro_use]
@ -498,6 +539,7 @@ macro_rules! call {
expect![[]], expect![[]],
); );
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
#[macro_use] #[macro_use]
@ -523,6 +565,7 @@ macro_rules! call {
expect![[]], expect![[]],
); );
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
#[macro_use] #[macro_use]
@ -558,6 +601,7 @@ macro_rules! call {
#[test] #[test]
fn test_call_hierarchy_in_macros_outgoing_different_files() { fn test_call_hierarchy_in_macros_outgoing_different_files() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
#[macro_use] #[macro_use]
@ -585,6 +629,7 @@ macro_rules! call {
expect![[]], expect![[]],
); );
check_hierarchy( check_hierarchy(
false,
r#" r#"
//- /lib.rs //- /lib.rs
#[macro_use] #[macro_use]
@ -616,6 +661,7 @@ macro_rules! call {
#[test] #[test]
fn test_trait_method_call_hierarchy() { fn test_trait_method_call_hierarchy() {
check_hierarchy( check_hierarchy(
false,
r#" r#"
trait T1 { trait T1 {
fn call$0ee(); fn call$0ee();
@ -636,4 +682,64 @@ fn caller() {
expect![[]], expect![[]],
); );
} }
#[test]
fn test_call_hierarchy_excluding_tests() {
check_hierarchy(
false,
r#"
fn main() {
f1();
}
fn f1$0() {
f2(); f3();
}
fn f2() {
f1(); f3();
}
#[test]
fn f3() {
f1(); f2();
}
"#,
expect!["f1 Function FileId(0) 25..52 28..30"],
expect![[r#"
main Function FileId(0) 0..23 3..7 : FileId(0):16..18
f2 Function FileId(0) 54..81 57..59 : FileId(0):68..70
f3 Function FileId(0) 83..118 94..96 : FileId(0):105..107"#]],
expect![[r#"
f2 Function FileId(0) 54..81 57..59 : FileId(0):39..41
f3 Function FileId(0) 83..118 94..96 : FileId(0):45..47"#]],
);
check_hierarchy(
true,
r#"
fn main() {
f1();
}
fn f1$0() {
f2(); f3();
}
fn f2() {
f1(); f3();
}
#[test]
fn f3() {
f1(); f2();
}
"#,
expect!["f1 Function FileId(0) 25..52 28..30"],
expect![[r#"
main Function FileId(0) 0..23 3..7 : FileId(0):16..18
f2 Function FileId(0) 54..81 57..59 : FileId(0):68..70"#]],
expect!["f2 Function FileId(0) 54..81 57..59 : FileId(0):39..41"],
);
}
} }

View file

@ -79,7 +79,7 @@ use crate::navigation_target::ToNav;
pub use crate::{ pub use crate::{
annotations::{Annotation, AnnotationConfig, AnnotationKind, AnnotationLocation}, annotations::{Annotation, AnnotationConfig, AnnotationKind, AnnotationLocation},
call_hierarchy::CallItem, call_hierarchy::{CallHierarchyConfig, CallItem},
expand_macro::ExpandedMacro, expand_macro::ExpandedMacro,
file_structure::{StructureNode, StructureNodeKind}, file_structure::{StructureNode, StructureNodeKind},
folding_ranges::{Fold, FoldKind}, folding_ranges::{Fold, FoldKind},
@ -564,13 +564,21 @@ impl Analysis {
} }
/// Computes incoming calls for the given file position. /// Computes incoming calls for the given file position.
pub fn incoming_calls(&self, position: FilePosition) -> Cancellable<Option<Vec<CallItem>>> { pub fn incoming_calls(
self.with_db(|db| call_hierarchy::incoming_calls(db, position)) &self,
config: CallHierarchyConfig,
position: FilePosition,
) -> Cancellable<Option<Vec<CallItem>>> {
self.with_db(|db| call_hierarchy::incoming_calls(db, config, position))
} }
/// Computes outgoing calls for the given file position. /// Computes outgoing calls for the given file position.
pub fn outgoing_calls(&self, position: FilePosition) -> Cancellable<Option<Vec<CallItem>>> { pub fn outgoing_calls(
self.with_db(|db| call_hierarchy::outgoing_calls(db, position)) &self,
config: CallHierarchyConfig,
position: FilePosition,
) -> Cancellable<Option<Vec<CallItem>>> {
self.with_db(|db| call_hierarchy::outgoing_calls(db, config, position))
} }
/// Returns a `mod name;` declaration which created the current module. /// Returns a `mod name;` declaration which created the current module.

View file

@ -12,10 +12,11 @@ use std::{
use cfg::{CfgAtom, CfgDiff}; use cfg::{CfgAtom, CfgDiff};
use hir::Symbol; use hir::Symbol;
use ide::{ use ide::{
AssistConfig, CallableSnippets, CompletionConfig, CompletionFieldsToResolve, DiagnosticsConfig, AssistConfig, CallHierarchyConfig, CallableSnippets, CompletionConfig,
ExprFillDefaultMode, GenericParameterHints, HighlightConfig, HighlightRelatedConfig, CompletionFieldsToResolve, DiagnosticsConfig, ExprFillDefaultMode, GenericParameterHints,
HoverConfig, HoverDocFormat, InlayFieldsToResolve, InlayHintsConfig, JoinLinesConfig, HighlightConfig, HighlightRelatedConfig, HoverConfig, HoverDocFormat, InlayFieldsToResolve,
MemoryLayoutHoverConfig, MemoryLayoutHoverRenderKind, Snippet, SnippetScope, SourceRootId, InlayHintsConfig, JoinLinesConfig, MemoryLayoutHoverConfig, MemoryLayoutHoverRenderKind,
Snippet, SnippetScope, SourceRootId,
}; };
use ide_db::{ use ide_db::{
imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind}, imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@ -262,7 +263,7 @@ 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. /// Exclude tests from find-all-references and call-hierarchy.
references_excludeTests: bool = false, references_excludeTests: bool = false,
/// Inject additional highlighting into doc comments. /// Inject additional highlighting into doc comments.
@ -1392,6 +1393,10 @@ impl Config {
} }
} }
pub fn call_hierarchy(&self) -> CallHierarchyConfig {
CallHierarchyConfig { exclude_tests: self.references_excludeTests().to_owned() }
}
pub fn completion(&self, source_root: Option<SourceRootId>) -> CompletionConfig { pub fn completion(&self, source_root: Option<SourceRootId>) -> CompletionConfig {
let client_capability_fields = self.completion_resolve_support_properties(); let client_capability_fields = self.completion_resolve_support_properties();
CompletionConfig { CompletionConfig {

View file

@ -1708,7 +1708,8 @@ pub(crate) fn handle_call_hierarchy_incoming(
let frange = from_proto::file_range(&snap, &doc, item.selection_range)?; let frange = from_proto::file_range(&snap, &doc, item.selection_range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() }; let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
let call_items = match snap.analysis.incoming_calls(fpos)? { let config = snap.config.call_hierarchy();
let call_items = match snap.analysis.incoming_calls(config, fpos)? {
None => return Ok(None), None => return Ok(None),
Some(it) => it, Some(it) => it,
}; };
@ -1745,7 +1746,8 @@ pub(crate) fn handle_call_hierarchy_outgoing(
let frange = from_proto::file_range(&snap, &doc, item.selection_range)?; let frange = from_proto::file_range(&snap, &doc, item.selection_range)?;
let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() }; let fpos = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
let call_items = match snap.analysis.outgoing_calls(fpos)? { let config = snap.config.call_hierarchy();
let call_items = match snap.analysis.outgoing_calls(config, fpos)? {
None => return Ok(None), None => return Ok(None),
Some(it) => it, Some(it) => it,
}; };

View file

@ -852,7 +852,7 @@ Exclude imports from find-all-references.
[[rust-analyzer.references.excludeTests]]rust-analyzer.references.excludeTests (default: `false`):: [[rust-analyzer.references.excludeTests]]rust-analyzer.references.excludeTests (default: `false`)::
+ +
-- --
Exclude tests from find-all-references. Exclude tests from find-all-references and call-hierarchy.
-- --
[[rust-analyzer.runnables.command]]rust-analyzer.runnables.command (default: `null`):: [[rust-analyzer.runnables.command]]rust-analyzer.runnables.command (default: `null`)::
+ +

View file

@ -2333,7 +2333,7 @@
"title": "references", "title": "references",
"properties": { "properties": {
"rust-analyzer.references.excludeTests": { "rust-analyzer.references.excludeTests": {
"markdownDescription": "Exclude tests from find-all-references.", "markdownDescription": "Exclude tests from find-all-references and call-hierarchy.",
"default": false, "default": false,
"type": "boolean" "type": "boolean"
} }