mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Use ItemTree for modules in attrs_query
This commit is contained in:
parent
5f9a5825e0
commit
0081ef3834
3 changed files with 32 additions and 26 deletions
|
@ -374,30 +374,23 @@ impl AttrsWithOwner {
|
||||||
let mod_data = &def_map[module.local_id];
|
let mod_data = &def_map[module.local_id];
|
||||||
|
|
||||||
match mod_data.origin {
|
match mod_data.origin {
|
||||||
// FIXME: We should be able to leverage the item tree instead of parsing declaration here
|
ModuleOrigin::File { definition, declaration_tree_id, .. } => {
|
||||||
// but we don't save the module's item tree id anywhere
|
let decl_attrs = declaration_tree_id
|
||||||
ModuleOrigin::File { definition, declaration, .. } => {
|
.item_tree(db)
|
||||||
let value = declaration.to_node(db.upcast());
|
.raw_attrs(AttrOwner::ModItem(declaration_tree_id.value.into()))
|
||||||
let it = InFile { file_id: declaration.file_id, value };
|
.clone();
|
||||||
let raw_attrs = RawAttrs::from_attrs_owner(
|
|
||||||
db,
|
|
||||||
it.as_ref().map(|it| it as &dyn ast::HasAttrs),
|
|
||||||
);
|
|
||||||
let tree = db.file_item_tree(definition.into());
|
let tree = db.file_item_tree(definition.into());
|
||||||
raw_attrs.merge(tree.raw_attrs(AttrOwner::TopLevel).clone())
|
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
|
||||||
|
decl_attrs.merge(def_attrs)
|
||||||
}
|
}
|
||||||
ModuleOrigin::CrateRoot { definition } => {
|
ModuleOrigin::CrateRoot { definition } => {
|
||||||
let tree = db.file_item_tree(definition.into());
|
let tree = db.file_item_tree(definition.into());
|
||||||
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
||||||
}
|
}
|
||||||
// FIXME: We should be able to leverage the item tree instead of parsing here
|
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id
|
||||||
// but we don't save the module's item tree id anywhere
|
.item_tree(db)
|
||||||
ModuleOrigin::Inline { definition } => RawAttrs::from_attrs_owner(
|
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
|
||||||
db,
|
.clone(),
|
||||||
InFile::new(definition.file_id, definition.to_node(db.upcast()))
|
|
||||||
.as_ref()
|
|
||||||
.map(|it| it as &dyn ast::HasAttrs),
|
|
||||||
),
|
|
||||||
ModuleOrigin::BlockExpr { block } => RawAttrs::from_attrs_owner(
|
ModuleOrigin::BlockExpr { block } => RawAttrs::from_attrs_owner(
|
||||||
db,
|
db,
|
||||||
InFile::new(block.file_id, block.to_node(db.upcast()))
|
InFile::new(block.file_id, block.to_node(db.upcast()))
|
||||||
|
|
|
@ -70,7 +70,7 @@ use syntax::{ast, SmolStr};
|
||||||
use crate::{
|
use crate::{
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
item_scope::{BuiltinShadowMode, ItemScope},
|
item_scope::{BuiltinShadowMode, ItemScope},
|
||||||
item_tree::TreeId,
|
item_tree::{ItemTreeId, Mod, TreeId},
|
||||||
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
||||||
path::ModPath,
|
path::ModPath,
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
|
@ -141,9 +141,11 @@ pub enum ModuleOrigin {
|
||||||
File {
|
File {
|
||||||
is_mod_rs: bool,
|
is_mod_rs: bool,
|
||||||
declaration: AstId<ast::Module>,
|
declaration: AstId<ast::Module>,
|
||||||
|
declaration_tree_id: ItemTreeId<Mod>,
|
||||||
definition: FileId,
|
definition: FileId,
|
||||||
},
|
},
|
||||||
Inline {
|
Inline {
|
||||||
|
definition_tree_id: ItemTreeId<Mod>,
|
||||||
definition: AstId<ast::Module>,
|
definition: AstId<ast::Module>,
|
||||||
},
|
},
|
||||||
/// Pseudo-module introduced by a block scope (contains only inner items).
|
/// Pseudo-module introduced by a block scope (contains only inner items).
|
||||||
|
@ -186,7 +188,7 @@ impl ModuleOrigin {
|
||||||
let sf = db.parse(file_id).tree();
|
let sf = db.parse(file_id).tree();
|
||||||
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
|
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
|
||||||
}
|
}
|
||||||
ModuleOrigin::Inline { definition } => InFile::new(
|
ModuleOrigin::Inline { definition, .. } => InFile::new(
|
||||||
definition.file_id,
|
definition.file_id,
|
||||||
ModuleSource::Module(definition.to_node(db.upcast())),
|
ModuleSource::Module(definition.to_node(db.upcast())),
|
||||||
),
|
),
|
||||||
|
|
|
@ -1525,7 +1525,7 @@ impl ModCollector<'_, '_> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match item {
|
match item {
|
||||||
ModItem::Mod(m) => self.collect_module(&self.item_tree[m], &attrs),
|
ModItem::Mod(m) => self.collect_module(m, &attrs),
|
||||||
ModItem::Import(import_id) => {
|
ModItem::Import(import_id) => {
|
||||||
let imports = Import::from_use(
|
let imports = Import::from_use(
|
||||||
db,
|
db,
|
||||||
|
@ -1700,9 +1700,10 @@ impl ModCollector<'_, '_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
|
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
|
||||||
let path_attr = attrs.by_key("path").string_value();
|
let path_attr = attrs.by_key("path").string_value();
|
||||||
let is_macro_use = attrs.by_key("macro_use").exists();
|
let is_macro_use = attrs.by_key("macro_use").exists();
|
||||||
|
let module = &self.item_tree[module_id];
|
||||||
match &module.kind {
|
match &module.kind {
|
||||||
// inline module, just recurse
|
// inline module, just recurse
|
||||||
ModKind::Inline { items } => {
|
ModKind::Inline { items } => {
|
||||||
|
@ -1711,6 +1712,7 @@ impl ModCollector<'_, '_> {
|
||||||
AstId::new(self.file_id(), module.ast_id),
|
AstId::new(self.file_id(), module.ast_id),
|
||||||
None,
|
None,
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
|
module_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
|
if let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
|
||||||
|
@ -1748,6 +1750,7 @@ impl ModCollector<'_, '_> {
|
||||||
ast_id,
|
ast_id,
|
||||||
Some((file_id, is_mod_rs)),
|
Some((file_id, is_mod_rs)),
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
|
module_id,
|
||||||
);
|
);
|
||||||
ModCollector {
|
ModCollector {
|
||||||
def_collector: self.def_collector,
|
def_collector: self.def_collector,
|
||||||
|
@ -1774,6 +1777,7 @@ impl ModCollector<'_, '_> {
|
||||||
ast_id,
|
ast_id,
|
||||||
None,
|
None,
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
|
module_id,
|
||||||
);
|
);
|
||||||
self.def_collector.def_map.diagnostics.push(
|
self.def_collector.def_map.diagnostics.push(
|
||||||
DefDiagnostic::unresolved_module(self.module_id, ast_id, candidates),
|
DefDiagnostic::unresolved_module(self.module_id, ast_id, candidates),
|
||||||
|
@ -1790,6 +1794,7 @@ impl ModCollector<'_, '_> {
|
||||||
declaration: AstId<ast::Module>,
|
declaration: AstId<ast::Module>,
|
||||||
definition: Option<(FileId, bool)>,
|
definition: Option<(FileId, bool)>,
|
||||||
visibility: &crate::visibility::RawVisibility,
|
visibility: &crate::visibility::RawVisibility,
|
||||||
|
mod_tree_id: FileItemTreeId<Mod>,
|
||||||
) -> LocalModuleId {
|
) -> LocalModuleId {
|
||||||
let def_map = &mut self.def_collector.def_map;
|
let def_map = &mut self.def_collector.def_map;
|
||||||
let vis = def_map
|
let vis = def_map
|
||||||
|
@ -1797,10 +1802,16 @@ impl ModCollector<'_, '_> {
|
||||||
.unwrap_or(Visibility::Public);
|
.unwrap_or(Visibility::Public);
|
||||||
let modules = &mut def_map.modules;
|
let modules = &mut def_map.modules;
|
||||||
let origin = match definition {
|
let origin = match definition {
|
||||||
None => ModuleOrigin::Inline { definition: declaration },
|
None => ModuleOrigin::Inline {
|
||||||
Some((definition, is_mod_rs)) => {
|
definition: declaration,
|
||||||
ModuleOrigin::File { declaration, definition, is_mod_rs }
|
definition_tree_id: ItemTreeId::new(self.tree_id, mod_tree_id),
|
||||||
}
|
},
|
||||||
|
Some((definition, is_mod_rs)) => ModuleOrigin::File {
|
||||||
|
declaration,
|
||||||
|
definition,
|
||||||
|
is_mod_rs,
|
||||||
|
declaration_tree_id: ItemTreeId::new(self.tree_id, mod_tree_id),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = modules.alloc(ModuleData::new(origin, vis));
|
let res = modules.alloc(ModuleData::new(origin, vis));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue