rust/crates/ra_ide/src/goto_definition.rs

870 lines
14 KiB
Rust
Raw Normal View History

use hir::Semantics;
2020-03-03 11:54:39 -06:00
use ra_ide_db::{
defs::{classify_name, classify_name_ref, NameClass},
2020-03-03 11:54:39 -06:00
symbol_index, RootDatabase,
};
2019-01-08 13:33:36 -06:00
use ra_syntax::{
2020-02-22 09:57:29 -06:00
ast::{self},
match_ast, AstNode,
SyntaxKind::*,
2020-07-10 07:08:35 -05:00
SyntaxToken, TokenAtOffset, T,
2019-01-08 13:33:36 -06:00
};
use crate::{
2020-02-22 09:57:29 -06:00
display::{ToNav, TryToNav},
FilePosition, NavigationTarget, RangeInfo,
};
2019-01-08 13:33:36 -06:00
2020-05-31 02:45:41 -05:00
// Feature: Go to Definition
2020-05-30 18:54:54 -05:00
//
// Navigates to the definition of an identifier.
//
// |===
// | Editor | Shortcut
//
// | VS Code | kbd:[F12]
// |===
2019-01-08 16:38:51 -06:00
pub(crate) fn goto_definition(
2019-01-08 13:33:36 -06:00
db: &RootDatabase,
position: FilePosition,
2019-01-15 12:02:42 -06:00
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let sema = Semantics::new(db);
let file = sema.parse(position.file_id).syntax().clone();
let original_token = pick_best(file.token_at_offset(position.offset))?;
let token = sema.descend_into_macros(original_token.clone());
2020-07-10 07:08:35 -05:00
let parent = token.parent();
2019-11-16 07:49:26 -06:00
let nav_targets = match_ast! {
2020-07-10 07:08:35 -05:00
match parent {
ast::NameRef(name_ref) => {
reference_definition(&sema, &name_ref).to_vec()
},
ast::Name(name) => {
let def = match classify_name(&sema, &name)? {
NameClass::Definition(def) | NameClass::ConstReference(def) => def,
NameClass::FieldShorthand { local: _, field } => field,
};
let nav = def.try_to_nav(sema.db)?;
vec![nav]
},
_ => return None,
}
};
Some(RangeInfo::new(original_token.text_range(), nav_targets))
}
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
return tokens.max_by_key(priority);
fn priority(n: &SyntaxToken) -> usize {
match n.kind() {
2020-07-10 07:08:35 -05:00
IDENT | INT_NUMBER | T![self] => 2,
kind if kind.is_trivia() => 0,
_ => 1,
}
}
}
#[derive(Debug)]
pub(crate) enum ReferenceResult {
Exact(NavigationTarget),
Approximate(Vec<NavigationTarget>),
}
impl ReferenceResult {
fn to_vec(self) -> Vec<NavigationTarget> {
match self {
ReferenceResult::Exact(target) => vec![target],
ReferenceResult::Approximate(vec) => vec,
}
}
}
2019-01-08 16:38:51 -06:00
pub(crate) fn reference_definition(
sema: &Semantics<RootDatabase>,
name_ref: &ast::NameRef,
2019-01-15 12:02:42 -06:00
) -> ReferenceResult {
let name_kind = classify_name_ref(sema, name_ref);
2020-02-22 09:57:29 -06:00
if let Some(def) = name_kind {
let def = def.definition();
return match def.try_to_nav(sema.db) {
2020-02-22 09:57:29 -06:00
Some(nav) => ReferenceResult::Exact(nav),
None => ReferenceResult::Approximate(Vec::new()),
};
}
2019-04-10 03:15:55 -05:00
// Fallback index based approach:
let navs = symbol_index::index_resolve(sema.db, name_ref)
2019-01-08 13:33:36 -06:00
.into_iter()
.map(|s| s.to_nav(sema.db))
2019-01-08 13:33:36 -06:00
.collect();
ReferenceResult::Approximate(navs)
2019-01-08 13:33:36 -06:00
}
#[cfg(test)]
mod tests {
2020-06-30 06:03:08 -05:00
use ra_db::FileRange;
use ra_syntax::{TextRange, TextSize};
use crate::mock_analysis::MockAnalysis;
fn check(ra_fixture: &str) {
let (mock, position) = MockAnalysis::with_files_and_position(ra_fixture);
let (mut expected, data) = mock.annotation();
let analysis = mock.analysis();
match data.as_str() {
"" => (),
"file" => {
expected.range =
TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
}
data => panic!("bad data: {}", data),
}
2019-12-14 11:20:07 -06:00
2020-07-10 07:08:35 -05:00
let mut navs =
analysis.goto_definition(position).unwrap().expect("no definition found").info;
if navs.len() == 0 {
panic!("unresolved reference")
}
assert_eq!(navs.len(), 1);
let nav = navs.pop().unwrap();
2020-06-30 06:03:08 -05:00
assert_eq!(expected, FileRange { file_id: nav.file_id(), range: nav.range() });
}
2019-01-08 13:33:36 -06:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_in_items() {
check(
r#"
struct Foo;
2020-06-30 06:03:08 -05:00
//^^^
enum E { X(Foo<|>) }
"#,
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_at_start_of_item() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
//^^^
enum E { X(<|>Foo) }
"#,
);
}
#[test]
fn goto_definition_resolves_correct_name() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
use a::Foo;
mod a;
mod b;
enum E { X(Foo<|>) }
2019-12-18 07:52:58 -06:00
2020-06-30 06:03:08 -05:00
//- /a.rs
struct Foo;
//^^^
//- /b.rs
struct Foo;
"#,
2019-01-08 13:33:36 -06:00
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_module_declaration() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
mod <|>foo;
2019-12-18 07:52:58 -06:00
//- /foo.rs
// empty
2020-06-30 06:03:08 -05:00
//^ file
"#,
2019-01-08 13:33:36 -06:00
);
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
mod <|>foo;
2019-12-18 07:52:58 -06:00
//- /foo/mod.rs
// empty
2020-06-30 06:03:08 -05:00
//^ file
"#,
2019-01-08 13:33:36 -06:00
);
}
2019-04-24 15:16:50 -05:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_macros() {
2020-06-30 06:03:08 -05:00
check(
r#"
macro_rules! foo { () => { () } }
//^^^
fn bar() {
<|>foo!();
}
"#,
2019-04-24 15:16:50 -05:00
);
}
2019-06-01 06:34:19 -05:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_macros_from_other_crates() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
2020-06-30 06:03:08 -05:00
use foo::foo;
fn bar() {
<|>foo!();
}
//- /foo/lib.rs
#[macro_export]
macro_rules! foo { () => { () } }
2020-06-30 06:03:08 -05:00
//^^^
"#,
2019-06-01 06:34:19 -05:00
);
}
2019-10-31 12:29:56 -05:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_macros_in_use_tree() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
use foo::foo<|>;
2019-10-31 12:29:56 -05:00
2020-06-30 06:03:08 -05:00
//- /foo/lib.rs
#[macro_export]
macro_rules! foo { () => { () } }
//^^^
"#,
2019-10-31 12:29:56 -05:00
);
}
2019-11-03 11:49:49 -06:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_macro_defined_fn_with_arg() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
macro_rules! define_fn {
($name:ident) => (fn $name() {})
}
2019-11-03 11:49:49 -06:00
define_fn!(foo);
2020-06-30 06:03:08 -05:00
//^^^
2019-11-03 11:49:49 -06:00
fn bar() {
<|>foo();
}
"#,
2019-11-03 11:49:49 -06:00
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_macro_defined_fn_no_arg() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
macro_rules! define_fn {
() => (fn foo() {})
}
2019-11-03 11:49:49 -06:00
2020-06-30 06:03:08 -05:00
define_fn!();
//^^^^^^^^^^^^^
2019-11-03 11:49:49 -06:00
fn bar() {
<|>foo();
}
"#,
2019-11-03 11:49:49 -06:00
);
}
#[test]
fn goto_definition_works_for_macro_inside_pattern() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
macro_rules! foo {() => {0}}
//^^^
fn bar() {
match (0,1) {
(<|>foo!(), _) => {}
}
}
"#,
);
}
#[test]
fn goto_definition_works_for_macro_inside_match_arm_lhs() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
macro_rules! foo {() => {0}}
//^^^
fn bar() {
match 0 {
<|>foo!() => {}
}
}
"#,
);
}
#[test]
fn goto_def_for_use_alias() {
check(
r#"
//- /lib.rs
use foo as bar<|>;
//- /foo/lib.rs
// empty
//^ file
"#,
);
}
#[test]
fn goto_def_for_use_alias_foo_macro() {
check(
r#"
//- /lib.rs
use foo::foo as bar<|>;
//- /foo/lib.rs
#[macro_export]
macro_rules! foo { () => { () } }
//^^^
"#,
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_methods() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
struct Foo;
impl Foo {
fn frobnicate(&self) { }
//^^^^^^^^^^
}
2020-06-30 06:03:08 -05:00
fn bar(foo: &Foo) {
foo.frobnicate<|>();
}
"#,
);
2019-01-25 11:32:34 -06:00
}
2019-01-25 11:32:34 -06:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_fields() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo {
spam: u32,
} //^^^^
2019-01-25 11:32:34 -06:00
2020-06-30 06:03:08 -05:00
fn bar(foo: &Foo) {
foo.spam<|>;
}
"#,
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_record_fields() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
struct Foo {
spam: u32,
} //^^^^
2020-06-30 06:03:08 -05:00
fn bar() -> Foo {
Foo {
spam<|>: 0,
}
}
"#,
);
}
#[test]
fn goto_def_for_record_pat_fields() {
2020-06-30 06:03:08 -05:00
check(
r#"
//- /lib.rs
struct Foo {
spam: u32,
} //^^^^
2020-06-30 06:03:08 -05:00
fn bar(foo: Foo) -> Foo {
let Foo { spam<|>: _, } = foo
}
"#,
);
}
2019-10-31 09:13:52 -05:00
2020-03-25 07:53:15 -05:00
#[test]
fn goto_def_for_record_fields_macros() {
2020-06-30 06:03:08 -05:00
check(
2020-03-25 07:53:15 -05:00
r"
2020-06-30 06:03:08 -05:00
macro_rules! m { () => { 92 };}
struct Foo { spam: u32 }
//^^^^
2020-03-25 07:53:15 -05:00
2020-06-30 06:03:08 -05:00
fn bar() -> Foo {
Foo { spam<|>: m!() }
}
",
2020-03-25 07:53:15 -05:00
);
}
2019-12-13 14:59:25 -06:00
#[test]
fn goto_for_tuple_fields() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo(u32);
//^^^
fn bar() {
let foo = Foo(0);
foo.<|>0;
}
"#,
2019-12-13 14:59:25 -06:00
);
}
2019-10-31 09:13:52 -05:00
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_ufcs_inherent_methods() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
impl Foo {
fn frobnicate() { }
} //^^^^^^^^^^
2019-10-31 09:13:52 -05:00
2020-06-30 06:03:08 -05:00
fn bar(foo: &Foo) {
Foo::frobnicate<|>();
}
"#,
2019-10-31 09:13:52 -05:00
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_ufcs_trait_methods_through_traits() {
2020-06-30 06:03:08 -05:00
check(
r#"
trait Foo {
fn frobnicate();
} //^^^^^^^^^^
2019-10-31 09:13:52 -05:00
2020-06-30 06:03:08 -05:00
fn bar() {
Foo::frobnicate<|>();
}
"#,
2019-10-31 09:13:52 -05:00
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_for_ufcs_trait_methods_through_self() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
trait Trait {
fn frobnicate();
} //^^^^^^^^^^
impl Trait for Foo {}
2019-10-31 09:13:52 -05:00
2020-06-30 06:03:08 -05:00
fn bar() {
Foo::frobnicate<|>();
}
"#,
2019-10-31 09:13:52 -05:00
);
}
#[test]
fn goto_definition_on_self() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
impl Foo {
//^^^
pub fn new() -> Self {
Self<|> {}
}
}
"#,
);
check(
r#"
struct Foo;
impl Foo {
//^^^
pub fn new() -> Self<|> {
Self {}
}
}
"#,
);
check(
r#"
enum Foo { A }
impl Foo {
//^^^
pub fn new() -> Self<|> {
Foo::A
}
}
"#,
);
check(
r#"
enum Foo { A }
impl Foo {
//^^^
pub fn thing(a: &Self<|>) {
}
}
"#,
);
}
#[test]
fn goto_definition_on_self_in_trait_impl() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
trait Make {
fn new() -> Self;
}
impl Make for Foo {
//^^^
fn new() -> Self {
Self<|> {}
}
}
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo;
trait Make {
fn new() -> Self;
}
impl Make for Foo {
//^^^
fn new() -> Self<|> {
Self {}
}
}
"#,
);
}
#[test]
2019-12-20 07:47:01 -06:00
fn goto_def_when_used_on_definition_name_itself() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo<|> { value: u32 }
//^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
struct Foo {
field<|>: string,
} //^^^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
fn foo_test<|>() { }
//^^^^^^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo<|> { Variant }
//^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
enum Foo {
Variant1,
Variant2<|>,
//^^^^^^^^
Variant3,
}
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
static INNER<|>: &str = "";
//^^^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
const INNER<|>: &str = "";
//^^^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
type Thing<|> = Option<()>;
//^^^^^
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
trait Foo<|> { }
//^^^
"#,
);
check(
r#"
mod bar<|> { }
//^^^
"#,
);
}
2019-11-16 07:49:26 -06:00
#[test]
fn goto_from_macro() {
2020-06-30 06:03:08 -05:00
check(
r#"
macro_rules! id {
($($tt:tt)*) => { $($tt)* }
}
fn foo() {}
//^^^
id! {
fn bar() {
fo<|>o();
}
}
mod confuse_index { fn foo(); }
"#,
2019-11-16 07:49:26 -06:00
);
}
#[test]
fn goto_through_format() {
2020-06-30 06:03:08 -05:00
check(
r#"
#[macro_export]
macro_rules! format {
($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
}
#[rustc_builtin_macro]
#[macro_export]
macro_rules! format_args {
($fmt:expr) => ({ /* compiler built-in */ });
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
}
pub mod __export {
pub use crate::format_args;
fn foo() {} // for index confusion
}
fn foo() -> i8 {}
//^^^
fn test() {
format!("{}", fo<|>o())
}
"#,
);
}
2019-12-07 11:54:18 -06:00
#[test]
fn goto_for_type_param() {
2020-06-30 06:03:08 -05:00
check(
r#"
2020-06-30 06:03:08 -05:00
struct Foo<T: Clone> { t: <|>T }
//^
"#,
2019-12-07 11:54:18 -06:00
);
}
#[test]
fn goto_within_macro() {
2020-06-30 06:03:08 -05:00
check(
r#"
macro_rules! id {
($($tt:tt)*) => ($($tt)*)
}
fn foo() {
let x = 1;
2020-06-30 06:03:08 -05:00
//^
id!({
let y = <|>x;
let z = y;
});
}
"#,
);
2020-06-30 06:03:08 -05:00
check(
r#"
macro_rules! id {
($($tt:tt)*) => ($($tt)*)
}
fn foo() {
let x = 1;
id!({
let y = x;
2020-06-30 06:03:08 -05:00
//^
let z = <|>y;
});
}
"#,
);
}
2019-12-20 04:19:09 -06:00
#[test]
fn goto_def_in_local_fn() {
2020-06-30 06:03:08 -05:00
check(
r#"
fn main() {
fn foo() {
let x = 92;
//^
<|>x;
}
}
"#,
2019-12-20 04:19:09 -06:00
);
}
2019-12-20 07:47:01 -06:00
2020-03-14 01:25:30 -05:00
#[test]
fn goto_def_in_local_macro() {
2020-06-30 06:03:08 -05:00
check(
r#"
fn bar() {
macro_rules! foo { () => { () } }
//^^^
<|>foo!();
}
"#,
2020-03-14 01:25:30 -05:00
);
}
2019-12-20 07:47:01 -06:00
#[test]
fn goto_def_for_field_init_shorthand() {
2020-06-30 06:03:08 -05:00
check(
r#"
struct Foo { x: i32 }
fn main() {
let x = 92;
//^
Foo { x<|> };
}
"#,
2019-12-20 07:47:01 -06:00
)
}
2020-06-06 14:16:59 -05:00
#[test]
fn goto_def_for_enum_variant_field() {
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo {
Bar { x: i32 }
} //^
fn baz(foo: Foo) {
match foo {
Foo::Bar { x<|> } => x
};
}
"#,
2020-06-06 14:16:59 -05:00
);
}
2020-06-10 12:11:47 -05:00
#[test]
fn goto_def_for_enum_variant_self_pattern_const() {
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo { Bar }
//^^^
impl Foo {
fn baz(self) {
match self { Self::Bar<|> => {} }
}
}
"#,
2020-06-10 12:11:47 -05:00
);
}
#[test]
fn goto_def_for_enum_variant_self_pattern_record() {
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo { Bar { val: i32 } }
//^^^
impl Foo {
fn baz(self) -> i32 {
match self { Self::Bar<|> { val } => {} }
}
}
"#,
2020-06-10 12:11:47 -05:00
);
}
#[test]
fn goto_def_for_enum_variant_self_expr_const() {
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo { Bar }
//^^^
impl Foo {
fn baz(self) { Self::Bar<|>; }
}
"#,
2020-06-10 12:11:47 -05:00
);
}
#[test]
fn goto_def_for_enum_variant_self_expr_record() {
2020-06-30 06:03:08 -05:00
check(
r#"
enum Foo { Bar { val: i32 } }
//^^^
impl Foo {
fn baz(self) { Self::Bar<|> {val: 4}; }
}
"#,
2020-06-10 12:11:47 -05:00
);
}
#[test]
fn goto_def_for_type_alias_generic_parameter() {
check(
r#"
type Alias<T> = T<|>;
//^
"#,
)
}
2019-01-08 13:33:36 -06:00
}