mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Make FromSource private
This commit is contained in:
parent
f4eeff2c82
commit
81a45ca1b3
8 changed files with 84 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::{FromSource, ImplBlock};
|
||||
use hir::{ImplBlock, SourceBinder};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
||||
|
||||
|
@ -12,6 +12,7 @@ pub(crate) fn goto_implementation(
|
|||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let parse = db.parse(position.file_id);
|
||||
let syntax = parse.tree().syntax().clone();
|
||||
let mut sb = SourceBinder::new(db);
|
||||
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
|
@ -22,12 +23,12 @@ pub(crate) fn goto_implementation(
|
|||
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, position, &nominal_def, module)?,
|
||||
impls_for_def(&mut sb, 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, position, &trait_def, module)?,
|
||||
impls_for_trait(&mut sb, position, &trait_def, module)?,
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -35,7 +36,7 @@ pub(crate) fn goto_implementation(
|
|||
}
|
||||
|
||||
fn impls_for_def(
|
||||
db: &RootDatabase,
|
||||
sb: &mut SourceBinder<RootDatabase>,
|
||||
position: FilePosition,
|
||||
node: &ast::NominalDef,
|
||||
module: hir::Module,
|
||||
|
@ -43,43 +44,43 @@ fn impls_for_def(
|
|||
let ty = match node {
|
||||
ast::NominalDef::StructDef(def) => {
|
||||
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
|
||||
hir::Struct::from_source(db, src)?.ty(db)
|
||||
sb.to_def::<hir::Struct, _>(src)?.ty(sb.db)
|
||||
}
|
||||
ast::NominalDef::EnumDef(def) => {
|
||||
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
|
||||
hir::Enum::from_source(db, src)?.ty(db)
|
||||
sb.to_def::<hir::Enum, _>(src)?.ty(sb.db)
|
||||
}
|
||||
ast::NominalDef::UnionDef(def) => {
|
||||
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
|
||||
hir::Union::from_source(db, src)?.ty(db)
|
||||
sb.to_def::<hir::Union, _>(src)?.ty(sb.db)
|
||||
}
|
||||
};
|
||||
|
||||
let krate = module.krate();
|
||||
let impls = ImplBlock::all_in_crate(db, krate);
|
||||
let impls = ImplBlock::all_in_crate(sb.db, krate);
|
||||
|
||||
Some(
|
||||
impls
|
||||
.into_iter()
|
||||
.filter(|impl_block| ty.is_equal_for_find_impls(&impl_block.target_ty(db)))
|
||||
.map(|imp| imp.to_nav(db))
|
||||
.filter(|impl_block| ty.is_equal_for_find_impls(&impl_block.target_ty(sb.db)))
|
||||
.map(|imp| imp.to_nav(sb.db))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn impls_for_trait(
|
||||
db: &RootDatabase,
|
||||
sb: &mut SourceBinder<RootDatabase>,
|
||||
position: FilePosition,
|
||||
node: &ast::TraitDef,
|
||||
module: hir::Module,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() };
|
||||
let tr = hir::Trait::from_source(db, src)?;
|
||||
let tr = sb.to_def(src)?;
|
||||
|
||||
let krate = module.krate();
|
||||
let impls = ImplBlock::for_trait(db, krate, tr);
|
||||
let impls = ImplBlock::for_trait(sb.db, krate, tr);
|
||||
|
||||
Some(impls.into_iter().map(|imp| imp.to_nav(db)).collect())
|
||||
Some(impls.into_iter().map(|imp| imp.to_nav(sb.db)).collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -210,7 +211,7 @@ mod tests {
|
|||
"
|
||||
//- /lib.rs
|
||||
#[derive(Copy)]
|
||||
struct Foo<|>;
|
||||
struct Foo<|>;
|
||||
",
|
||||
&["impl IMPL_BLOCK FileId(1) [0; 15)"],
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue