2019-12-30 08:12:06 -06:00
|
|
|
//! Entry point for call-hierarchy
|
|
|
|
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::Semantics;
|
2021-06-22 10:28:07 -05:00
|
|
|
use ide_db::{call_info::FnCallNode, RootDatabase};
|
2021-01-08 17:17:34 -06:00
|
|
|
use syntax::{ast, AstNode, TextRange};
|
2019-12-30 08:12:06 -06:00
|
|
|
|
|
|
|
use crate::{
|
2021-01-01 00:13:15 -06:00
|
|
|
display::TryToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo,
|
2019-12-30 08:12:06 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CallItem {
|
|
|
|
pub target: NavigationTarget,
|
|
|
|
pub ranges: Vec<TextRange>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallItem {
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn assert_match(&self, expected: &str) {
|
|
|
|
let actual = self.debug_render();
|
|
|
|
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn debug_render(&self) -> String {
|
|
|
|
format!("{} : {:?}", self.target.debug_render(), self.ranges)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn call_hierarchy(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
|
|
|
goto_definition::goto_definition(db, position)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
2020-07-01 07:11:34 -05:00
|
|
|
|
2019-12-30 08:12:06 -06:00
|
|
|
// 1. Find all refs
|
|
|
|
// 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply.
|
|
|
|
// 3. Add ranges relative to the start of the fndef.
|
2020-07-01 07:11:34 -05:00
|
|
|
let refs = references::find_all_refs(&sema, position, None)?;
|
2019-12-30 08:12:06 -06:00
|
|
|
|
|
|
|
let mut calls = CallLocations::default();
|
|
|
|
|
2021-02-07 11:38:12 -06:00
|
|
|
for (file_id, references) in refs.references {
|
2020-02-18 11:35:10 -06:00
|
|
|
let file = sema.parse(file_id);
|
|
|
|
let file = file.syntax();
|
2021-03-23 13:19:44 -05:00
|
|
|
for (relative_range, token) in references
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(range, _)| Some(range).zip(file.token_at_offset(range.start()).next()))
|
|
|
|
{
|
2021-01-11 17:05:07 -06:00
|
|
|
let token = sema.descend_into_macros(token);
|
|
|
|
// This target is the containing function
|
2021-01-30 09:19:21 -06:00
|
|
|
if let Some(nav) = token.ancestors().find_map(|node| {
|
2021-03-23 13:19:44 -05:00
|
|
|
let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
|
2021-01-11 17:05:07 -06:00
|
|
|
def.try_to_nav(sema.db)
|
|
|
|
}) {
|
|
|
|
calls.add(&nav, relative_range);
|
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(calls.into_items())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
2019-12-30 08:12:06 -06:00
|
|
|
let file_id = position.file_id;
|
2020-02-18 11:35:10 -06:00
|
|
|
let file = sema.parse(file_id);
|
|
|
|
let file = file.syntax();
|
2019-12-30 08:12:06 -06:00
|
|
|
let token = file.token_at_offset(position.offset).next()?;
|
2020-02-18 11:35:10 -06:00
|
|
|
let token = sema.descend_into_macros(token);
|
2019-12-30 08:12:06 -06:00
|
|
|
|
|
|
|
let mut calls = CallLocations::default();
|
|
|
|
|
2021-01-30 09:19:21 -06:00
|
|
|
token
|
|
|
|
.parent()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|it| it.descendants())
|
2019-12-30 08:12:06 -06:00
|
|
|
.filter_map(|node| FnCallNode::with_node_exact(&node))
|
|
|
|
.filter_map(|call_node| {
|
|
|
|
let name_ref = call_node.name_ref()?;
|
2021-01-08 17:17:34 -06:00
|
|
|
let func_target = match call_node {
|
2019-12-30 08:12:06 -06:00
|
|
|
FnCallNode::CallExpr(expr) => {
|
2021-08-02 13:42:25 -05:00
|
|
|
let callable = sema.type_of_expr(&expr.expr()?)?.ty.as_callable(db)?;
|
2020-07-16 06:00:56 -05:00
|
|
|
match callable.kind() {
|
2021-01-08 17:17:34 -06:00
|
|
|
hir::CallableKind::Function(it) => it.try_to_nav(db),
|
2019-12-30 08:12:06 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FnCallNode::MethodCallExpr(expr) => {
|
2020-02-18 11:35:10 -06:00
|
|
|
let function = sema.resolve_method_call(&expr)?;
|
2021-01-01 00:13:15 -06:00
|
|
|
function.try_to_nav(db)
|
2019-12-30 08:12:06 -06:00
|
|
|
}
|
2021-01-08 17:17:34 -06:00
|
|
|
}?;
|
|
|
|
Some((func_target, name_ref.syntax().text_range()))
|
2019-12-30 08:12:06 -06:00
|
|
|
})
|
|
|
|
.for_each(|(nav, range)| calls.add(&nav, range));
|
|
|
|
|
|
|
|
Some(calls.into_items())
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:33:04 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
struct CallLocations {
|
|
|
|
funcs: IndexMap<NavigationTarget, Vec<TextRange>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallLocations {
|
|
|
|
fn add(&mut self, target: &NavigationTarget, range: TextRange) {
|
|
|
|
self.funcs.entry(target.clone()).or_default().push(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_items(self) -> Vec<CallItem> {
|
|
|
|
self.funcs.into_iter().map(|(target, ranges)| CallItem { target, ranges }).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 08:12:06 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::FilePosition;
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::fixture;
|
2019-12-30 08:12:06 -06:00
|
|
|
|
|
|
|
fn check_hierarchy(
|
2020-06-23 15:27:24 -05:00
|
|
|
ra_fixture: &str,
|
2019-12-30 08:12:06 -06:00
|
|
|
expected: &str,
|
|
|
|
expected_incoming: &[&str],
|
|
|
|
expected_outgoing: &[&str],
|
|
|
|
) {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, pos) = fixture::position(ra_fixture);
|
2019-12-30 08:12:06 -06:00
|
|
|
|
|
|
|
let mut navs = analysis.call_hierarchy(pos).unwrap().unwrap().info;
|
|
|
|
assert_eq!(navs.len(), 1);
|
|
|
|
let nav = navs.pop().unwrap();
|
|
|
|
nav.assert_match(expected);
|
|
|
|
|
2020-07-17 05:42:48 -05:00
|
|
|
let item_pos =
|
|
|
|
FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() };
|
2019-12-30 08:12:06 -06:00
|
|
|
let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap();
|
|
|
|
assert_eq!(incoming_calls.len(), expected_incoming.len());
|
|
|
|
|
|
|
|
for call in 0..incoming_calls.len() {
|
|
|
|
incoming_calls[call].assert_match(expected_incoming[call]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let outgoing_calls = analysis.outgoing_calls(item_pos).unwrap().unwrap();
|
|
|
|
assert_eq!(outgoing_calls.len(), expected_outgoing.len());
|
|
|
|
|
|
|
|
for call in 0..outgoing_calls.len() {
|
|
|
|
outgoing_calls[call].assert_match(expected_outgoing[call]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_on_ref() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
fn callee() {}
|
|
|
|
fn caller() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0ee();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(0) 0..14 3..9",
|
|
|
|
&["caller Function FileId(0) 15..44 18..24 : [33..39]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_on_def() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
fn call$0ee() {}
|
2020-06-23 15:27:24 -05:00
|
|
|
fn caller() {
|
|
|
|
callee();
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(0) 0..14 3..9",
|
|
|
|
&["caller Function FileId(0) 15..44 18..24 : [33..39]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_in_same_fn() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
fn callee() {}
|
|
|
|
fn caller() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0ee();
|
2020-06-23 15:27:24 -05:00
|
|
|
callee();
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(0) 0..14 3..9",
|
|
|
|
&["caller Function FileId(0) 15..58 18..24 : [33..39, 47..53]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_in_different_fn() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
fn callee() {}
|
|
|
|
fn caller1() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0ee();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn caller2() {
|
|
|
|
callee();
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(0) 0..14 3..9",
|
2019-12-30 08:12:06 -06:00
|
|
|
&[
|
2020-12-17 05:29:05 -06:00
|
|
|
"caller1 Function FileId(0) 15..45 18..25 : [34..40]",
|
|
|
|
"caller2 Function FileId(0) 47..77 50..57 : [66..72]",
|
2019-12-30 08:12:06 -06:00
|
|
|
],
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:23:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_in_tests_mod() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs cfg:test
|
|
|
|
fn callee() {}
|
|
|
|
fn caller1() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0ee();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
2020-05-16 07:23:43 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-05-16 07:23:43 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
#[test]
|
|
|
|
fn test_caller() {
|
|
|
|
callee();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(0) 0..14 3..9",
|
2020-05-16 07:23:43 -05:00
|
|
|
&[
|
2020-12-17 05:29:05 -06:00
|
|
|
"caller1 Function FileId(0) 15..45 18..25 : [34..40]",
|
|
|
|
"test_caller Function FileId(0) 95..149 110..121 : [134..140]",
|
2020-05-16 07:23:43 -05:00
|
|
|
],
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-30 08:12:06 -06:00
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_in_different_files() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
use foo::callee;
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn caller() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0ee();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /foo/mod.rs
|
|
|
|
pub fn callee() {}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"callee Function FileId(1) 0..18 7..13",
|
|
|
|
&["caller Function FileId(0) 27..56 30..36 : [45..51]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_outgoing() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
fn callee() {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn call$0er() {
|
2020-06-23 15:27:24 -05:00
|
|
|
callee();
|
|
|
|
callee();
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"caller Function FileId(0) 15..58 18..24",
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
2020-12-17 05:29:05 -06:00
|
|
|
&["callee Function FileId(0) 0..14 3..9 : [33..39, 47..53]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_outgoing_in_different_files() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
use foo::callee;
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn call$0er() {
|
2020-06-23 15:27:24 -05:00
|
|
|
callee();
|
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /foo/mod.rs
|
|
|
|
pub fn callee() {}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"caller Function FileId(0) 27..56 30..36",
|
2019-12-30 08:12:06 -06:00
|
|
|
&[],
|
2020-12-17 05:29:05 -06:00
|
|
|
&["callee Function FileId(1) 0..18 7..13 : [45..51]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_incoming_outgoing() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /lib.rs
|
|
|
|
fn caller1() {
|
2021-01-06 14:15:48 -06:00
|
|
|
call$0er2();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn caller2() {
|
|
|
|
caller3();
|
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn caller3() {
|
2019-12-30 08:12:06 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"caller2 Function FileId(0) 33..64 36..43",
|
|
|
|
&["caller1 Function FileId(0) 0..31 3..10 : [19..26]"],
|
|
|
|
&["caller3 Function FileId(0) 66..83 69..76 : [52..59]"],
|
2019-12-30 08:12:06 -06:00
|
|
|
);
|
|
|
|
}
|
2020-06-28 13:43:02 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call_hierarchy_issue_5103() {
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
|
|
|
fn a() {
|
|
|
|
b()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
a$0()
|
2020-06-28 13:43:02 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"a Function FileId(0) 0..18 3..4",
|
|
|
|
&["main Function FileId(0) 31..52 34..38 : [47..48]"],
|
|
|
|
&["b Function FileId(0) 20..29 23..24 : [13..14]"],
|
2020-06-28 13:43:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
check_hierarchy(
|
|
|
|
r#"
|
|
|
|
fn a() {
|
2021-01-06 14:15:48 -06:00
|
|
|
b$0()
|
2020-06-28 13:43:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
a()
|
|
|
|
}
|
|
|
|
"#,
|
2020-12-17 05:29:05 -06:00
|
|
|
"b Function FileId(0) 20..29 23..24",
|
|
|
|
&["a Function FileId(0) 0..18 3..4 : [13..14]"],
|
2020-06-28 13:43:02 -05:00
|
|
|
&[],
|
|
|
|
);
|
|
|
|
}
|
2019-12-30 08:12:06 -06:00
|
|
|
}
|