mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
introduce FromSource trait
This commit is contained in:
parent
c35ef7e1ed
commit
2867c40925
13 changed files with 294 additions and 138 deletions
|
@ -1,4 +1,4 @@
|
|||
use hir::{db::HirDatabase, source_binder, ApplicationTy, Ty, TypeCtor};
|
||||
use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
||||
|
||||
|
@ -11,17 +11,21 @@ pub(crate) fn goto_implementation(
|
|||
let parse = db.parse(position.file_id);
|
||||
let syntax = parse.tree().syntax().clone();
|
||||
|
||||
let module = source_binder::module_from_position(db, position)?;
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
db,
|
||||
hir::Source { file_id: position.file_id.into(), ast: src },
|
||||
)?;
|
||||
|
||||
if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) {
|
||||
return Some(RangeInfo::new(
|
||||
nominal_def.syntax().text_range(),
|
||||
impls_for_def(db, &nominal_def, module)?,
|
||||
impls_for_def(db, position, &nominal_def, module)?,
|
||||
));
|
||||
} else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) {
|
||||
return Some(RangeInfo::new(
|
||||
trait_def.syntax().text_range(),
|
||||
impls_for_trait(db, &trait_def, module)?,
|
||||
impls_for_trait(db, position, &trait_def, module)?,
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -30,14 +34,19 @@ pub(crate) fn goto_implementation(
|
|||
|
||||
fn impls_for_def(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
node: &ast::NominalDef,
|
||||
module: hir::Module,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let ty = match node {
|
||||
ast::NominalDef::StructDef(def) => {
|
||||
source_binder::struct_from_module(db, module, &def).ty(db)
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() };
|
||||
hir::Struct::from_source(db, src)?.ty(db)
|
||||
}
|
||||
ast::NominalDef::EnumDef(def) => {
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() };
|
||||
hir::Enum::from_source(db, src)?.ty(db)
|
||||
}
|
||||
ast::NominalDef::EnumDef(def) => source_binder::enum_from_module(db, module, &def).ty(db),
|
||||
};
|
||||
|
||||
let krate = module.krate(db)?;
|
||||
|
@ -54,10 +63,12 @@ fn impls_for_def(
|
|||
|
||||
fn impls_for_trait(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
node: &ast::TraitDef,
|
||||
module: hir::Module,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let tr = source_binder::trait_from_module(db, module, node);
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() };
|
||||
let tr = hir::Trait::from_source(db, src)?;
|
||||
|
||||
let krate = module.krate(db)?;
|
||||
let impls = db.impls_in_crate(krate);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue