1 //! Entry point for call-hierarchy
3 use indexmap::IndexMap;
6 use ide_db::{call_info::FnCallNode, RootDatabase};
7 use syntax::{ast, AstNode, TextRange};
10 display::TryToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo,
13 #[derive(Debug, Clone)]
15 pub target: NavigationTarget,
16 pub ranges: Vec<TextRange>,
21 pub(crate) fn assert_match(&self, expected: &str) {
22 let actual = self.debug_render();
23 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
27 pub(crate) fn debug_render(&self) -> String {
28 format!("{} : {:?}", self.target.debug_render(), self.ranges)
32 pub(crate) fn call_hierarchy(
34 position: FilePosition,
35 ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
36 goto_definition::goto_definition(db, position)
39 pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
40 let sema = Semantics::new(db);
43 // 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply.
44 // 3. Add ranges relative to the start of the fndef.
45 let refs = references::find_all_refs(&sema, position, None)?;
47 let mut calls = CallLocations::default();
49 for (file_id, references) in refs.references {
50 let file = sema.parse(file_id);
51 let file = file.syntax();
52 for (relative_range, token) in references
54 .filter_map(|(range, _)| Some(range).zip(file.token_at_offset(range.start()).next()))
56 let token = sema.descend_into_macros(token);
57 // This target is the containing function
58 if let Some(nav) = token.ancestors().find_map(|node| {
59 let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
60 def.try_to_nav(sema.db)
62 calls.add(&nav, relative_range);
67 Some(calls.into_items())
70 pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
71 let sema = Semantics::new(db);
72 let file_id = position.file_id;
73 let file = sema.parse(file_id);
74 let file = file.syntax();
75 let token = file.token_at_offset(position.offset).next()?;
76 let token = sema.descend_into_macros(token);
78 let mut calls = CallLocations::default();
83 .flat_map(|it| it.descendants())
84 .filter_map(|node| FnCallNode::with_node_exact(&node))
85 .filter_map(|call_node| {
86 let name_ref = call_node.name_ref()?;
87 let func_target = match call_node {
88 FnCallNode::CallExpr(expr) => {
89 let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?;
90 match callable.kind() {
91 hir::CallableKind::Function(it) => it.try_to_nav(db),
95 FnCallNode::MethodCallExpr(expr) => {
96 let function = sema.resolve_method_call(&expr)?;
97 function.try_to_nav(db)
100 Some((func_target, name_ref.syntax().text_range()))
102 .for_each(|(nav, range)| calls.add(&nav, range));
104 Some(calls.into_items())
108 struct CallLocations {
109 funcs: IndexMap<NavigationTarget, Vec<TextRange>>,
113 fn add(&mut self, target: &NavigationTarget, range: TextRange) {
114 self.funcs.entry(target.clone()).or_default().push(range);
117 fn into_items(self) -> Vec<CallItem> {
118 self.funcs.into_iter().map(|(target, ranges)| CallItem { target, ranges }).collect()
124 use ide_db::base_db::FilePosition;
131 expected_incoming: &[&str],
132 expected_outgoing: &[&str],
134 let (analysis, pos) = fixture::position(ra_fixture);
136 let mut navs = analysis.call_hierarchy(pos).unwrap().unwrap().info;
137 assert_eq!(navs.len(), 1);
138 let nav = navs.pop().unwrap();
139 nav.assert_match(expected);
142 FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() };
143 let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap();
144 assert_eq!(incoming_calls.len(), expected_incoming.len());
146 for call in 0..incoming_calls.len() {
147 incoming_calls[call].assert_match(expected_incoming[call]);
150 let outgoing_calls = analysis.outgoing_calls(item_pos).unwrap().unwrap();
151 assert_eq!(outgoing_calls.len(), expected_outgoing.len());
153 for call in 0..outgoing_calls.len() {
154 outgoing_calls[call].assert_match(expected_outgoing[call]);
159 fn test_call_hierarchy_on_ref() {
168 "callee Function FileId(0) 0..14 3..9",
169 &["caller Function FileId(0) 15..44 18..24 : [33..39]"],
175 fn test_call_hierarchy_on_def() {
184 "callee Function FileId(0) 0..14 3..9",
185 &["caller Function FileId(0) 15..44 18..24 : [33..39]"],
191 fn test_call_hierarchy_in_same_fn() {
201 "callee Function FileId(0) 0..14 3..9",
202 &["caller Function FileId(0) 15..58 18..24 : [33..39, 47..53]"],
208 fn test_call_hierarchy_in_different_fn() {
221 "callee Function FileId(0) 0..14 3..9",
223 "caller1 Function FileId(0) 15..45 18..25 : [34..40]",
224 "caller2 Function FileId(0) 47..77 50..57 : [66..72]",
231 fn test_call_hierarchy_in_tests_mod() {
250 "callee Function FileId(0) 0..14 3..9",
252 "caller1 Function FileId(0) 15..45 18..25 : [34..40]",
253 "test_caller Function FileId(0) 95..149 110..121 : [134..140]",
260 fn test_call_hierarchy_in_different_files() {
274 "callee Function FileId(1) 0..18 7..13",
275 &["caller Function FileId(0) 27..56 30..36 : [45..51]"],
281 fn test_call_hierarchy_outgoing() {
291 "caller Function FileId(0) 15..58 18..24",
293 &["callee Function FileId(0) 0..14 3..9 : [33..39, 47..53]"],
298 fn test_call_hierarchy_outgoing_in_different_files() {
312 "caller Function FileId(0) 27..56 30..36",
314 &["callee Function FileId(1) 0..18 7..13 : [45..51]"],
319 fn test_call_hierarchy_incoming_outgoing() {
335 "caller2 Function FileId(0) 33..64 36..43",
336 &["caller1 Function FileId(0) 0..31 3..10 : [19..26]"],
337 &["caller3 Function FileId(0) 66..83 69..76 : [52..59]"],
342 fn test_call_hierarchy_issue_5103() {
355 "a Function FileId(0) 0..18 3..4",
356 &["main Function FileId(0) 31..52 34..38 : [47..48]"],
357 &["b Function FileId(0) 20..29 23..24 : [13..14]"],
372 "b Function FileId(0) 20..29 23..24",
373 &["a Function FileId(0) 0..18 3..4 : [13..14]"],