use Source for module, part 1
This commit is contained in:
parent
178d8e96b5
commit
0145d06515
@ -196,16 +196,13 @@ impl Module {
|
||||
}
|
||||
|
||||
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
||||
pub fn definition_source(
|
||||
self,
|
||||
db: &(impl DefDatabase + AstDatabase),
|
||||
) -> (HirFileId, ModuleSource) {
|
||||
pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let decl_id = def_map[self.module_id].declaration;
|
||||
let file_id = def_map[self.module_id].definition;
|
||||
let module_source = ModuleSource::new(db, file_id, decl_id);
|
||||
let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
|
||||
(file_id, module_source)
|
||||
(file_id, module_source).into()
|
||||
}
|
||||
|
||||
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
||||
@ -226,9 +223,9 @@ impl Module {
|
||||
db: &impl HirDatabase,
|
||||
import: ImportId,
|
||||
) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
|
||||
let (file_id, source) = self.definition_source(db);
|
||||
let (_, source_map) = db.raw_items_with_source_map(file_id);
|
||||
source_map.get(&source, import)
|
||||
let src = self.definition_source(db);
|
||||
let (_, source_map) = db.raw_items_with_source_map(src.file_id);
|
||||
source_map.get(&src.ast, import)
|
||||
}
|
||||
|
||||
/// Returns the crate this module is part of.
|
||||
|
@ -48,8 +48,8 @@ impl HasSource for ImplBlock {
|
||||
type Ast = TreeArc<ast::ImplBlock>;
|
||||
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
|
||||
let source_map = db.impls_in_module_with_source_map(self.module).1;
|
||||
let (file_id, source) = self.module.definition_source(db);
|
||||
(file_id, source_map.get(&source, self.impl_id)).into()
|
||||
let src = self.module.definition_source(db);
|
||||
(src.file_id, source_map.get(&src.ast, self.impl_id)).into()
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,8 +69,8 @@ impl ImplBlock {
|
||||
/// Returns the syntax of the impl block
|
||||
pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
|
||||
let source_map = db.impls_in_module_with_source_map(self.module).1;
|
||||
let (file_id, source) = self.module.definition_source(db);
|
||||
(file_id, source_map.get(&source, self.impl_id)).into()
|
||||
let src = self.module.definition_source(db);
|
||||
(src.file_id, source_map.get(&src.ast, self.impl_id)).into()
|
||||
}
|
||||
|
||||
pub fn id(&self) -> ImplId {
|
||||
@ -207,8 +207,8 @@ impl ModuleImplBlocks {
|
||||
impls_by_def: FxHashMap::default(),
|
||||
};
|
||||
|
||||
let (file_id, module_source) = m.module.definition_source(db);
|
||||
let node = match &module_source {
|
||||
let src = m.module.definition_source(db);
|
||||
let node = match &src.ast {
|
||||
ModuleSource::SourceFile(node) => node.syntax(),
|
||||
ModuleSource::Module(node) => {
|
||||
node.item_list().expect("inline module should have item list").syntax()
|
||||
@ -216,7 +216,7 @@ impl ModuleImplBlocks {
|
||||
};
|
||||
|
||||
for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
|
||||
let impl_block = ImplData::from_ast(db, file_id, m.module, impl_block_ast);
|
||||
let impl_block = ImplData::from_ast(db, src.file_id, m.module, impl_block_ast);
|
||||
let id = m.impls.alloc(impl_block);
|
||||
for &impl_item in &m.impls[id].items {
|
||||
m.impls_by_def.insert(impl_item, id);
|
||||
|
@ -84,7 +84,7 @@ impl LangItems {
|
||||
) {
|
||||
// Look for impl targets
|
||||
let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone());
|
||||
let source = module.definition_source(db).1;
|
||||
let source = module.definition_source(db).ast;
|
||||
for (impl_id, _) in impl_blocks.impls.iter() {
|
||||
let impl_block = source_map.get(&source, impl_id);
|
||||
let lang_item_name = impl_block
|
||||
|
@ -97,13 +97,13 @@ pub fn struct_from_module(
|
||||
module: Module,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> Struct {
|
||||
let (file_id, _) = module.definition_source(db);
|
||||
let file_id = module.definition_source(db).file_id;
|
||||
let ctx = LocationCtx::new(db, module, file_id);
|
||||
Struct { id: ctx.to_def(struct_def) }
|
||||
}
|
||||
|
||||
pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum {
|
||||
let (file_id, _) = module.definition_source(db);
|
||||
let file_id = module.definition_source(db).file_id;
|
||||
let ctx = LocationCtx::new(db, module, file_id);
|
||||
Enum { id: ctx.to_def(enum_def) }
|
||||
}
|
||||
@ -113,7 +113,7 @@ pub fn trait_from_module(
|
||||
module: Module,
|
||||
trait_def: &ast::TraitDef,
|
||||
) -> Trait {
|
||||
let (file_id, _) = module.definition_source(db);
|
||||
let file_id = module.definition_source(db).file_id;
|
||||
let ctx = LocationCtx::new(db, module, file_id);
|
||||
Trait { id: ctx.to_def(trait_def) }
|
||||
}
|
||||
|
@ -129,10 +129,10 @@ impl NavigationTarget {
|
||||
}
|
||||
|
||||
pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
|
||||
let (file_id, source) = module.definition_source(db);
|
||||
let file_id = file_id.as_original_file();
|
||||
let src = module.definition_source(db);
|
||||
let file_id = src.file_id.as_original_file();
|
||||
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||
match source {
|
||||
match src.ast {
|
||||
ModuleSource::SourceFile(node) => {
|
||||
NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None)
|
||||
}
|
||||
|
@ -115,8 +115,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||
Some(Def(it)) => {
|
||||
match it {
|
||||
hir::ModuleDef::Module(it) => {
|
||||
let it = it.definition_source(db).1;
|
||||
if let hir::ModuleSource::Module(it) = it {
|
||||
if let hir::ModuleSource::Module(it) = it.definition_source(db).ast {
|
||||
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
|
||||
}
|
||||
}
|
||||
|
@ -148,9 +148,9 @@ fn rename_mod(
|
||||
let mut file_system_edits = Vec::new();
|
||||
if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module)
|
||||
{
|
||||
let (file_id, module_source) = module.definition_source(db);
|
||||
let file_id = file_id.as_original_file();
|
||||
match module_source {
|
||||
let src = module.definition_source(db);
|
||||
let file_id = src.file_id.as_original_file();
|
||||
match src.ast {
|
||||
ModuleSource::SourceFile(..) => {
|
||||
let mod_path: RelativePathBuf = db.file_relative_path(file_id);
|
||||
// mod is defined in path/to/dir/mod.rs
|
||||
|
Loading…
x
Reference in New Issue
Block a user