mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-31 23:38:45 +00:00
Deduplicate FileId field in ModuleOrigin
This commit is contained in:
parent
e5b23e3bc1
commit
fde2d9b47c
3 changed files with 23 additions and 18 deletions
|
@ -431,7 +431,7 @@ impl AttrsWithOwner {
|
||||||
.item_tree(db)
|
.item_tree(db)
|
||||||
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
|
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
|
||||||
.clone(),
|
.clone(),
|
||||||
ModuleOrigin::BlockExpr { id, block } => {
|
ModuleOrigin::BlockExpr { id, .. } => {
|
||||||
let tree = db.block_item_tree_query(id);
|
let tree = db.block_item_tree_query(id);
|
||||||
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ mod tests;
|
||||||
use std::{cmp::Ord, ops::Deref};
|
use std::{cmp::Ord, ops::Deref};
|
||||||
|
|
||||||
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
|
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
|
||||||
use hir_expand::{name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
|
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use la_arena::Arena;
|
use la_arena::Arena;
|
||||||
use profile::Count;
|
use profile::Count;
|
||||||
|
@ -217,13 +217,13 @@ pub enum ModuleOrigin {
|
||||||
/// Note that non-inline modules, by definition, live inside non-macro file.
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
||||||
File {
|
File {
|
||||||
is_mod_rs: bool,
|
is_mod_rs: bool,
|
||||||
declaration: AstId<ast::Module>,
|
declaration: FileAstId<ast::Module>,
|
||||||
declaration_tree_id: ItemTreeId<Mod>,
|
declaration_tree_id: ItemTreeId<Mod>,
|
||||||
definition: FileId,
|
definition: FileId,
|
||||||
},
|
},
|
||||||
Inline {
|
Inline {
|
||||||
definition_tree_id: ItemTreeId<Mod>,
|
definition_tree_id: ItemTreeId<Mod>,
|
||||||
definition: AstId<ast::Module>,
|
definition: FileAstId<ast::Module>,
|
||||||
},
|
},
|
||||||
/// Pseudo-module introduced by a block scope (contains only inner items).
|
/// Pseudo-module introduced by a block scope (contains only inner items).
|
||||||
BlockExpr {
|
BlockExpr {
|
||||||
|
@ -235,8 +235,12 @@ pub enum ModuleOrigin {
|
||||||
impl ModuleOrigin {
|
impl ModuleOrigin {
|
||||||
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
|
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
|
||||||
match self {
|
match self {
|
||||||
ModuleOrigin::File { declaration: module, .. }
|
&ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
|
||||||
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),
|
Some(AstId::new(declaration_tree_id.file_id(), declaration))
|
||||||
|
}
|
||||||
|
&ModuleOrigin::Inline { definition, definition_tree_id } => {
|
||||||
|
Some(AstId::new(definition_tree_id.file_id(), definition))
|
||||||
|
}
|
||||||
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,14 +265,15 @@ impl ModuleOrigin {
|
||||||
/// That is, a file or a `mod foo {}` with items.
|
/// That is, a file or a `mod foo {}` with items.
|
||||||
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
||||||
match self {
|
match self {
|
||||||
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
&ModuleOrigin::File { definition, .. } | &ModuleOrigin::CrateRoot { definition } => {
|
||||||
let file_id = *definition;
|
let sf = db.parse(definition).tree();
|
||||||
let sf = db.parse(file_id).tree();
|
InFile::new(definition.into(), ModuleSource::SourceFile(sf))
|
||||||
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
|
|
||||||
}
|
}
|
||||||
ModuleOrigin::Inline { definition, .. } => InFile::new(
|
&ModuleOrigin::Inline { definition, definition_tree_id } => InFile::new(
|
||||||
definition.file_id,
|
definition_tree_id.file_id(),
|
||||||
ModuleSource::Module(definition.to_node(db.upcast())),
|
ModuleSource::Module(
|
||||||
|
AstId::new(definition_tree_id.file_id(), definition).to_node(db.upcast()),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
ModuleOrigin::BlockExpr { block, .. } => {
|
ModuleOrigin::BlockExpr { block, .. } => {
|
||||||
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
|
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
|
||||||
|
@ -645,7 +650,7 @@ impl ModuleData {
|
||||||
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
||||||
definition.into()
|
definition.into()
|
||||||
}
|
}
|
||||||
ModuleOrigin::Inline { definition, .. } => definition.file_id,
|
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id.file_id(),
|
||||||
ModuleOrigin::BlockExpr { block, .. } => block.file_id,
|
ModuleOrigin::BlockExpr { block, .. } => block.file_id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1843,7 +1843,7 @@ impl ModCollector<'_, '_> {
|
||||||
ModKind::Inline { items } => {
|
ModKind::Inline { items } => {
|
||||||
let module_id = self.push_child_module(
|
let module_id = self.push_child_module(
|
||||||
module.name.clone(),
|
module.name.clone(),
|
||||||
AstId::new(self.file_id(), module.ast_id),
|
module.ast_id,
|
||||||
None,
|
None,
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
module_id,
|
module_id,
|
||||||
|
@ -1881,7 +1881,7 @@ impl ModCollector<'_, '_> {
|
||||||
if is_enabled {
|
if is_enabled {
|
||||||
let module_id = self.push_child_module(
|
let module_id = self.push_child_module(
|
||||||
module.name.clone(),
|
module.name.clone(),
|
||||||
ast_id,
|
ast_id.value,
|
||||||
Some((file_id, is_mod_rs)),
|
Some((file_id, is_mod_rs)),
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
module_id,
|
module_id,
|
||||||
|
@ -1908,7 +1908,7 @@ impl ModCollector<'_, '_> {
|
||||||
Err(candidates) => {
|
Err(candidates) => {
|
||||||
self.push_child_module(
|
self.push_child_module(
|
||||||
module.name.clone(),
|
module.name.clone(),
|
||||||
ast_id,
|
ast_id.value,
|
||||||
None,
|
None,
|
||||||
&self.item_tree[module.visibility],
|
&self.item_tree[module.visibility],
|
||||||
module_id,
|
module_id,
|
||||||
|
@ -1925,7 +1925,7 @@ impl ModCollector<'_, '_> {
|
||||||
fn push_child_module(
|
fn push_child_module(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: Name,
|
name: Name,
|
||||||
declaration: AstId<ast::Module>,
|
declaration: FileAstId<ast::Module>,
|
||||||
definition: Option<(FileId, bool)>,
|
definition: Option<(FileId, bool)>,
|
||||||
visibility: &crate::visibility::RawVisibility,
|
visibility: &crate::visibility::RawVisibility,
|
||||||
mod_tree_id: FileItemTreeId<Mod>,
|
mod_tree_id: FileItemTreeId<Mod>,
|
||||||
|
|
Loading…
Reference in a new issue