Skip to content

Commit d97a4b8

Browse files
committed
Support goto_type_definition for types
1 parent 5b663f1 commit d97a4b8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

crates/hir/src/semantics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
196196
self.imp.resolve_label(lifetime)
197197
}
198198

199+
pub fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
200+
self.imp.resolve_type(ty)
201+
}
202+
199203
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
200204
self.imp.type_of_expr(expr)
201205
}
@@ -476,6 +480,14 @@ impl<'db> SemanticsImpl<'db> {
476480
ToDef::to_def(self, src)
477481
}
478482

483+
fn resolve_type(&self, ty: &ast::Type) -> Option<Type> {
484+
let scope = self.scope(ty.syntax());
485+
let ctx = body::LowerCtx::new(self.db.upcast(), scope.file_id);
486+
let ty = hir_ty::TyLoweringContext::new(self.db, &scope.resolver)
487+
.lower_ty(&crate::TypeRef::from_ast(&ctx, ty.clone()));
488+
Type::new_with_resolver(self.db, &scope.resolver, ty)
489+
}
490+
479491
fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
480492
self.analyze(expr.syntax()).type_of_expr(self.db, expr)
481493
}

crates/ide/src/goto_type_definition.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) fn goto_type_definition(
3030
ast::Expr(it) => sema.type_of_expr(&it)?,
3131
ast::Pat(it) => sema.type_of_pat(&it)?,
3232
ast::SelfParam(it) => sema.type_of_self(&it)?,
33+
ast::Type(it) => sema.resolve_type(&it)?,
3334
_ => return None,
3435
}
3536
};
@@ -146,6 +147,17 @@ struct Foo;
146147
impl Foo {
147148
fn f(&self$0) {}
148149
}
150+
"#,
151+
)
152+
}
153+
154+
#[test]
155+
fn goto_def_for_type_fallback() {
156+
check(
157+
r#"
158+
struct Foo;
159+
//^^^
160+
impl Foo$0 {}
149161
"#,
150162
)
151163
}

0 commit comments

Comments
 (0)