mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Move module to SourceBinder
This commit is contained in:
parent
16cfc8d50c
commit
9a6c26e348
5 changed files with 69 additions and 55 deletions
|
@ -2,46 +2,22 @@
|
|||
//! file.
|
||||
|
||||
use hir_def::{nameres::ModuleSource, ModuleId};
|
||||
use hir_expand::name::AsName;
|
||||
use ra_db::FileId;
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::ast::{self, AstNode, NameOwner};
|
||||
|
||||
use crate::{db::DefDatabase, InFile, Module};
|
||||
use crate::{
|
||||
db::{DefDatabase, HirDatabase},
|
||||
InFile, Module,
|
||||
};
|
||||
|
||||
impl Module {
|
||||
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
|
||||
let _p = profile("Module::from_declaration");
|
||||
let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
|
||||
|
||||
let parent_module = match parent_declaration {
|
||||
Some(parent_declaration) => {
|
||||
let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
|
||||
Module::from_declaration(db, src_parent)
|
||||
}
|
||||
None => {
|
||||
let source_file = db.parse(src.file_id.original_file(db)).tree();
|
||||
let src_parent =
|
||||
InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
|
||||
Module::from_definition(db, src_parent)
|
||||
}
|
||||
}?;
|
||||
|
||||
let child_name = src.value.name()?.as_name();
|
||||
let def_map = db.crate_def_map(parent_module.id.krate);
|
||||
let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?;
|
||||
Some(parent_module.with_module_id(*child_id))
|
||||
}
|
||||
|
||||
pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> {
|
||||
pub fn from_definition(db: &impl HirDatabase, src: InFile<ModuleSource>) -> Option<Self> {
|
||||
let _p = profile("Module::from_definition");
|
||||
let mut sb = crate::SourceBinder::new(db);
|
||||
match src.value {
|
||||
ModuleSource::Module(ref module) => {
|
||||
assert!(!module.has_semi());
|
||||
return Module::from_declaration(
|
||||
db,
|
||||
InFile { file_id: src.file_id, value: module.clone() },
|
||||
);
|
||||
return sb.to_def(InFile { file_id: src.file_id, value: module.clone() });
|
||||
}
|
||||
ModuleSource::SourceFile(_) => (),
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue