1 //! FIXME: write short doc here
5 defs::{classify_name, classify_name_ref},
6 symbol_index, RootDatabase,
12 SyntaxToken, TokenAtOffset,
16 display::{ToNav, TryToNav},
17 FilePosition, NavigationTarget, RangeInfo,
20 pub(crate) fn goto_definition(
22 position: FilePosition,
23 ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
24 let sema = Semantics::new(db);
25 let file = sema.parse(position.file_id).syntax().clone();
26 let original_token = pick_best(file.token_at_offset(position.offset))?;
27 let token = sema.descend_into_macros(original_token.clone());
29 let nav_targets = match_ast! {
30 match (token.parent()) {
31 ast::NameRef(name_ref) => {
32 reference_definition(&sema, &name_ref).to_vec()
35 let def = classify_name(&sema, &name)?.definition();
36 let nav = def.try_to_nav(sema.db)?;
43 Some(RangeInfo::new(original_token.text_range(), nav_targets))
46 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
47 return tokens.max_by_key(priority);
48 fn priority(n: &SyntaxToken) -> usize {
50 IDENT | INT_NUMBER => 2,
51 kind if kind.is_trivia() => 0,
58 pub(crate) enum ReferenceResult {
59 Exact(NavigationTarget),
60 Approximate(Vec<NavigationTarget>),
63 impl ReferenceResult {
64 fn to_vec(self) -> Vec<NavigationTarget> {
66 ReferenceResult::Exact(target) => vec![target],
67 ReferenceResult::Approximate(vec) => vec,
72 pub(crate) fn reference_definition(
73 sema: &Semantics<RootDatabase>,
74 name_ref: &ast::NameRef,
75 ) -> ReferenceResult {
76 let name_kind = classify_name_ref(sema, name_ref);
77 if let Some(def) = name_kind {
78 let def = def.definition();
80 return match def.try_to_nav(sema.db) {
81 Some(nav) => ReferenceResult::Exact(nav),
82 None => ReferenceResult::Approximate(Vec::new()),
86 // Fallback index based approach:
87 let navs = symbol_index::index_resolve(sema.db, name_ref)
89 .map(|s| s.to_nav(sema.db))
91 ReferenceResult::Approximate(navs)
96 use test_utils::{assert_eq_text, covers};
98 use crate::mock_analysis::analysis_and_position;
100 fn check_goto(ra_fixture: &str, expected: &str, expected_range: &str) {
101 let (analysis, pos) = analysis_and_position(ra_fixture);
103 let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info;
105 panic!("unresolved reference")
107 assert_eq!(navs.len(), 1);
109 let nav = navs.pop().unwrap();
110 let file_text = analysis.file_text(nav.file_id()).unwrap();
112 let mut actual = file_text[nav.full_range()].to_string();
113 if let Some(focus) = nav.focus_range() {
115 actual += &file_text[focus];
118 if !expected_range.contains("...") {
119 test_utils::assert_eq_text!(&actual, expected_range);
121 let mut parts = expected_range.split("...");
122 let prefix = parts.next().unwrap();
123 let suffix = parts.next().unwrap();
125 actual.starts_with(prefix) && actual.ends_with(suffix),
126 "\nExpected: {}\n Actual: {}\n",
132 nav.assert_match(expected);
136 fn goto_def_in_items() {
143 "Foo STRUCT_DEF FileId(1) 0..11 7..10",
149 fn goto_def_at_start_of_item() {
156 "Foo STRUCT_DEF FileId(1) 0..11 7..10",
162 fn goto_definition_resolves_correct_name() {
177 "Foo STRUCT_DEF FileId(2) 0..11 7..10",
183 fn goto_def_for_module_declaration() {
192 "foo SOURCE_FILE FileId(2) 0..10",
204 "foo SOURCE_FILE FileId(2) 0..10",
210 fn goto_def_for_macros() {
211 covers!(ra_ide_db::goto_def_for_macros);
215 macro_rules! foo { () => { () } }
221 "foo MACRO_CALL FileId(1) 0..33 13..16",
222 "macro_rules! foo { () => { () } }|foo",
227 fn goto_def_for_macros_from_other_crates() {
228 covers!(ra_ide_db::goto_def_for_macros);
239 macro_rules! foo { () => { () } }
241 "foo MACRO_CALL FileId(2) 0..49 29..32",
242 "#[macro_export]\nmacro_rules! foo { () => { () } }|foo",
247 fn goto_def_for_use_alias() {
248 covers!(ra_ide_db::goto_def_for_use_alias);
257 macro_rules! foo { () => { () } }",
258 "SOURCE_FILE FileId(2) 0..50",
259 "#[macro_export]\nmacro_rules! foo { () => { () } }\n",
264 fn goto_def_for_use_alias_foo_macro() {
268 use foo::foo as bar<|>;
272 macro_rules! foo { () => { () } }
274 "foo MACRO_CALL FileId(2) 0..49 29..32",
275 "#[macro_export]\nmacro_rules! foo { () => { () } }|foo",
280 fn goto_def_for_macros_in_use_tree() {
288 macro_rules! foo { () => { () } }
290 "foo MACRO_CALL FileId(2) 0..49 29..32",
291 "#[macro_export]\nmacro_rules! foo { () => { () } }|foo",
296 fn goto_def_for_macro_defined_fn_with_arg() {
300 macro_rules! define_fn {
301 ($name:ident) => (fn $name() {})
310 "foo FN_DEF FileId(1) 64..80 75..78",
311 "define_fn!(foo);|foo",
316 fn goto_def_for_macro_defined_fn_no_arg() {
320 macro_rules! define_fn {
330 "foo FN_DEF FileId(1) 51..64 51..64",
331 "define_fn!();|define_fn!();",
336 fn goto_definition_works_for_macro_inside_pattern() {
340 macro_rules! foo {() => {0}}
348 "foo MACRO_CALL FileId(1) 0..28 13..16",
349 "macro_rules! foo {() => {0}}|foo",
354 fn goto_definition_works_for_macro_inside_match_arm_lhs() {
358 macro_rules! foo {() => {0}}
366 "foo MACRO_CALL FileId(1) 0..28 13..16",
367 "macro_rules! foo {() => {0}}|foo",
372 fn goto_def_for_methods() {
373 covers!(ra_ide_db::goto_def_for_methods);
379 fn frobnicate(&self) { }
386 "frobnicate FN_DEF FileId(1) 27..51 30..40",
387 "fn frobnicate(&self) { }|frobnicate",
392 fn goto_def_for_fields() {
393 covers!(ra_ide_db::goto_def_for_fields);
405 "spam RECORD_FIELD_DEF FileId(1) 17..26 17..21",
411 fn goto_def_for_record_fields() {
412 covers!(ra_ide_db::goto_def_for_record_fields);
426 "spam RECORD_FIELD_DEF FileId(1) 17..26 17..21",
432 fn goto_def_for_record_pat_fields() {
433 covers!(ra_ide_db::goto_def_for_record_field_pats);
441 fn bar(foo: Foo) -> Foo {
442 let Foo { spam<|>: _, } = foo
445 "spam RECORD_FIELD_DEF FileId(1) 17..26 17..21",
451 fn goto_def_for_record_fields_macros() {
455 macro_rules! m { () => { 92 };}
456 struct Foo { spam: u32 }
459 Foo { spam<|>: m!() }
462 "spam RECORD_FIELD_DEF FileId(1) 45..54 45..49",
468 fn goto_for_tuple_fields() {
479 "TUPLE_FIELD_DEF FileId(1) 11..14",
485 fn goto_def_for_ufcs_inherent_methods() {
495 Foo::frobnicate<|>();
498 "frobnicate FN_DEF FileId(1) 27..46 30..40",
499 "fn frobnicate() { }|frobnicate",
504 fn goto_def_for_ufcs_trait_methods_through_traits() {
513 Foo::frobnicate<|>();
516 "frobnicate FN_DEF FileId(1) 16..32 19..29",
517 "fn frobnicate();|frobnicate",
522 fn goto_def_for_ufcs_trait_methods_through_self() {
530 impl Trait for Foo {}
533 Foo::frobnicate<|>();
536 "frobnicate FN_DEF FileId(1) 30..46 33..43",
537 "fn frobnicate();|frobnicate",
542 fn goto_definition_on_self() {
548 pub fn new() -> Self {
553 "impl IMPL_DEF FileId(1) 12..73",
562 pub fn new() -> Self<|> {
567 "impl IMPL_DEF FileId(1) 12..73",
576 pub fn new() -> Self<|> {
581 "impl IMPL_DEF FileId(1) 15..75",
590 pub fn thing(a: &Self<|>) {
594 "impl IMPL_DEF FileId(1) 15..62",
600 fn goto_definition_on_self_in_trait_impl() {
614 "impl IMPL_DEF FileId(1) 49..115",
615 "impl Make for Foo {...}",
626 fn new() -> Self<|> {
631 "impl IMPL_DEF FileId(1) 49..115",
632 "impl Make for Foo {...}",
637 fn goto_def_when_used_on_definition_name_itself() {
641 struct Foo<|> { value: u32 }
643 "Foo STRUCT_DEF FileId(1) 0..25 7..10",
644 "struct Foo { value: u32 }|Foo",
654 "field RECORD_FIELD_DEF FileId(1) 17..30 17..22",
655 "field: string|field",
663 "foo_test FN_DEF FileId(1) 0..17 3..11",
664 "fn foo_test() { }|foo_test",
674 "Foo ENUM_DEF FileId(1) 0..25 5..8",
675 "enum Foo {...}|Foo",
687 "Variant2 ENUM_VARIANT FileId(1) 29..37 29..37",
694 static INNER<|>: &str = "";
696 "INNER STATIC_DEF FileId(1) 0..24 7..12",
697 "static INNER: &str = \"\";|INNER",
703 const INNER<|>: &str = "";
705 "INNER CONST_DEF FileId(1) 0..23 6..11",
706 "const INNER: &str = \"\";|INNER",
712 type Thing<|> = Option<()>;
714 "Thing TYPE_ALIAS_DEF FileId(1) 0..24 5..10",
715 "type Thing = Option<()>;|Thing",
723 "Foo TRAIT_DEF FileId(1) 0..13 6..9",
732 "bar MODULE FileId(1) 0..11 4..7",
738 fn goto_from_macro() {
743 ($($tt:tt)*) => { $($tt)* }
751 mod confuse_index { fn foo(); }
753 "foo FN_DEF FileId(1) 52..63 55..58",
759 fn goto_through_format() {
764 macro_rules! format {
765 ($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
767 #[rustc_builtin_macro]
769 macro_rules! format_args {
770 ($fmt:expr) => ({ /* compiler built-in */ });
771 ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
774 pub use crate::format_args;
775 fn foo() {} // for index confusion
779 format!(\"{}\", fo<|>o())
782 "foo FN_DEF FileId(1) 398..415 401..404",
783 "fn foo() -> i8 {}|foo",
788 fn goto_for_type_param() {
796 "T TYPE_PARAM FileId(1) 11..12",
802 fn goto_within_macro() {
807 ($($tt:tt)*) => ($($tt)*)
818 "x BIND_PAT FileId(1) 69..70",
826 ($($tt:tt)*) => ($($tt)*)
837 "y BIND_PAT FileId(1) 98..99",
843 fn goto_def_in_local_fn() {
854 "x BIND_PAT FileId(1) 39..40",
860 fn goto_def_in_local_macro() {
865 macro_rules! foo { () => { () } }
869 "foo MACRO_CALL FileId(1) 15..48 28..31",
870 "macro_rules! foo { () => { () } }|foo",
875 fn goto_def_for_field_init_shorthand() {
876 covers!(ra_ide_db::goto_def_for_field_init_shorthand);
880 struct Foo { x: i32 }
886 "x BIND_PAT FileId(1) 42..43",