aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/goto_definition.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs34
1 files changed, 30 insertions, 4 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 46bdde00d..45b4c56ef 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -3,6 +3,7 @@ use ra_syntax::{
3 AstNode, ast, 3 AstNode, ast,
4 algo::find_node_at_offset, 4 algo::find_node_at_offset,
5}; 5};
6use test_utils::tested_by;
6 7
7use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo}; 8use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
8 9
@@ -60,6 +61,7 @@ pub(crate) fn reference_definition(
60 .parent() 61 .parent()
61 .and_then(ast::MethodCallExpr::cast) 62 .and_then(ast::MethodCallExpr::cast)
62 { 63 {
64 tested_by!(goto_definition_works_for_methods);
63 let infer_result = function.infer(db); 65 let infer_result = function.infer(db);
64 let syntax_mapping = function.body_syntax_mapping(db); 66 let syntax_mapping = function.body_syntax_mapping(db);
65 let expr = ast::Expr::cast(method_call.syntax()).unwrap(); 67 let expr = ast::Expr::cast(method_call.syntax()).unwrap();
@@ -70,6 +72,19 @@ pub(crate) fn reference_definition(
70 return Exact(NavigationTarget::from_function(db, func)); 72 return Exact(NavigationTarget::from_function(db, func));
71 }; 73 };
72 } 74 }
75 // It could also be a field access
76 if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::FieldExpr::cast) {
77 tested_by!(goto_definition_works_for_fields);
78 let infer_result = function.infer(db);
79 let syntax_mapping = function.body_syntax_mapping(db);
80 let expr = ast::Expr::cast(field_expr.syntax()).unwrap();
81 if let Some(field) = syntax_mapping
82 .node_expr(expr)
83 .and_then(|it| infer_result.field_resolution(it))
84 {
85 return Exact(NavigationTarget::from_field(db, field));
86 };
87 }
73 } 88 }
74 // Then try module name resolution 89 // Then try module name resolution
75 if let Some(module) = hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax()) 90 if let Some(module) = hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax())
@@ -117,6 +132,8 @@ fn name_definition(
117 132
118#[cfg(test)] 133#[cfg(test)]
119mod tests { 134mod tests {
135 use test_utils::covers;
136
120 use crate::mock_analysis::analysis_and_position; 137 use crate::mock_analysis::analysis_and_position;
121 138
122 fn check_goto(fixuture: &str, expected: &str) { 139 fn check_goto(fixuture: &str, expected: &str) {
@@ -183,6 +200,7 @@ mod tests {
183 200
184 #[test] 201 #[test]
185 fn goto_definition_works_for_methods() { 202 fn goto_definition_works_for_methods() {
203 covers!(goto_definition_works_for_methods);
186 check_goto( 204 check_goto(
187 " 205 "
188 //- /lib.rs 206 //- /lib.rs
@@ -197,15 +215,23 @@ mod tests {
197 ", 215 ",
198 "frobnicate FN_DEF FileId(1) [27; 52) [30; 40)", 216 "frobnicate FN_DEF FileId(1) [27; 52) [30; 40)",
199 ); 217 );
218 }
200 219
220 #[test]
221 fn goto_definition_works_for_fields() {
222 covers!(goto_definition_works_for_fields);
201 check_goto( 223 check_goto(
202 " 224 "
203 //- /lib.rs 225 //- /lib.rs
204 mod <|>foo; 226 struct Foo {
205 //- /foo/mod.rs 227 spam: u32,
206 // empty 228 }
229
230 fn bar(foo: &Foo) {
231 foo.spam<|>;
232 }
207 ", 233 ",
208 "foo SOURCE_FILE FileId(2) [0; 10)", 234 "spam NAMED_FIELD_DEF FileId(1) [17; 26) [17; 21)",
209 ); 235 );
210 } 236 }
211} 237}