9275: feat: Support goto type for field expressions and patterns r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-06-14 19:08:05 +00:00 committed by GitHub
commit 27a70492f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,7 @@ pub(crate) fn goto_type_definition(
let token: SyntaxToken = pick_best(file.syntax().token_at_offset(position.offset))?; let token: SyntaxToken = pick_best(file.syntax().token_at_offset(position.offset))?;
let token: SyntaxToken = sema.descend_into_macros(token); let token: SyntaxToken = sema.descend_into_macros(token);
let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| { let (ty, node) = sema.token_ancestors_with_macros(token.clone()).find_map(|node| {
let ty = match_ast! { let ty = match_ast! {
match node { match node {
ast::Expr(it) => sema.type_of_expr(&it)?, ast::Expr(it) => sema.type_of_expr(&it)?,
@ -33,13 +33,23 @@ pub(crate) fn goto_type_definition(
ast::SelfParam(it) => sema.type_of_self(&it)?, ast::SelfParam(it) => sema.type_of_self(&it)?,
ast::Type(it) => sema.resolve_type(&it)?, ast::Type(it) => sema.resolve_type(&it)?,
ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?, ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?,
ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?,
// can't match on RecordExprField directly as `ast::Expr` will match an iteration too early otherwise
ast::NameRef(it) => {
if let Some(record_field) = ast::RecordExprField::for_name_ref(&it) {
let (_, _, ty) = sema.resolve_record_field(&record_field)?;
ty
} else {
let record_field = ast::RecordPatField::for_field_name_ref(&it)?;
sema.resolve_record_pat_field(&record_field)?.ty(db)
}
},
_ => return None, _ => return None,
} }
}; };
Some((ty, node)) Some((ty, node))
})?; })?;
let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?; let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?;
let nav = adt_def.try_to_nav(db)?; let nav = adt_def.try_to_nav(db)?;
@ -87,6 +97,54 @@ fn foo() {
); );
} }
#[test]
fn goto_type_definition_record_expr_field() {
check(
r#"
struct Bar;
// ^^^
struct Foo { foo: Bar }
fn foo() {
Foo { foo$0 }
}
"#,
);
check(
r#"
struct Bar;
// ^^^
struct Foo { foo: Bar }
fn foo() {
Foo { foo$0: Bar }
}
"#,
);
}
#[test]
fn goto_type_definition_record_pat_field() {
check(
r#"
struct Bar;
// ^^^
struct Foo { foo: Bar }
fn foo() {
let Foo { foo$0 };
}
"#,
);
check(
r#"
struct Bar;
// ^^^
struct Foo { foo: Bar }
fn foo() {
let Foo { foo$0: bar };
}
"#,
);
}
#[test] #[test]
fn goto_type_definition_works_simple_ref() { fn goto_type_definition_works_simple_ref() {
check( check(