mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Merge #682
682: remove Option<SourceFileItemId> hack r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
0974e6abeb
8 changed files with 97 additions and 104 deletions
|
@ -25,9 +25,10 @@ impl Module {
|
||||||
|
|
||||||
pub(crate) fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
|
pub(crate) fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
|
||||||
let module_tree = db.module_tree(self.krate);
|
let module_tree = db.module_tree(self.krate);
|
||||||
let source = self.module_id.source(&module_tree);
|
let file_id = self.module_id.file_id(&module_tree);
|
||||||
let module_source = ModuleSource::from_source_item_id(db, source);
|
let decl_id = self.module_id.decl_id(&module_tree);
|
||||||
let file_id = source.file_id.as_original_file();
|
let module_source = ModuleSource::new(db, file_id, decl_id);
|
||||||
|
let file_id = file_id.as_original_file();
|
||||||
(file_id, module_source)
|
(file_id, module_source)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +40,7 @@ impl Module {
|
||||||
let link = self.module_id.parent_link(&module_tree)?;
|
let link = self.module_id.parent_link(&module_tree)?;
|
||||||
let file_id = link
|
let file_id = link
|
||||||
.owner(&module_tree)
|
.owner(&module_tree)
|
||||||
.source(&module_tree)
|
.file_id(&module_tree)
|
||||||
.file_id
|
|
||||||
.as_original_file();
|
.as_original_file();
|
||||||
let src = link.source(&module_tree, db);
|
let src = link.source(&module_tree, db);
|
||||||
Some((file_id, src))
|
Some((file_id, src))
|
||||||
|
|
|
@ -16,6 +16,7 @@ use crate::{
|
||||||
adt::{StructData, EnumData},
|
adt::{StructData, EnumData},
|
||||||
impl_block::ModuleImplBlocks,
|
impl_block::ModuleImplBlocks,
|
||||||
generics::{GenericParams, GenericDef},
|
generics::{GenericParams, GenericDef},
|
||||||
|
ids::SourceFileItemId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[salsa::query_group(HirDatabaseStorage)]
|
#[salsa::query_group(HirDatabaseStorage)]
|
||||||
|
@ -51,7 +52,11 @@ pub trait HirDatabase: SourceDatabase + AsRef<HirInterner> {
|
||||||
fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>;
|
fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::module_tree::Submodule::submodules_query)]
|
#[salsa::invoke(crate::module_tree::Submodule::submodules_query)]
|
||||||
fn submodules(&self, source: SourceItemId) -> Arc<Vec<crate::module_tree::Submodule>>;
|
fn submodules(
|
||||||
|
&self,
|
||||||
|
file_id: HirFileId,
|
||||||
|
delc_id: Option<SourceFileItemId>,
|
||||||
|
) -> Arc<Vec<crate::module_tree::Submodule>>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_query)]
|
#[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_query)]
|
||||||
fn lower_module(&self, module: Module) -> (Arc<LoweredModule>, Arc<ImportSourceMap>);
|
fn lower_module(&self, module: Module) -> (Arc<LoweredModule>, Arc<ImportSourceMap>);
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use ra_db::{LocationIntener, FileId};
|
use ra_db::{LocationIntener, FileId};
|
||||||
use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast};
|
use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast};
|
||||||
use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
|
use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -203,7 +203,7 @@ pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
|
||||||
let items = ctx.db.file_items(ctx.file_id);
|
let items = ctx.db.file_items(ctx.file_id);
|
||||||
let raw = SourceItemId {
|
let raw = SourceItemId {
|
||||||
file_id: ctx.file_id,
|
file_id: ctx.file_id,
|
||||||
item_id: Some(items.id_of(ctx.file_id, ast.syntax())),
|
item_id: items.id_of(ctx.file_id, ast.syntax()),
|
||||||
};
|
};
|
||||||
let loc = ItemLoc {
|
let loc = ItemLoc {
|
||||||
module: ctx.module,
|
module: ctx.module,
|
||||||
|
@ -301,15 +301,14 @@ impl_arena_id!(SourceFileItemId);
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct SourceItemId {
|
pub struct SourceItemId {
|
||||||
pub(crate) file_id: HirFileId,
|
pub(crate) file_id: HirFileId,
|
||||||
/// None for the whole file.
|
pub(crate) item_id: SourceFileItemId,
|
||||||
pub(crate) item_id: Option<SourceFileItemId>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
|
/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct SourceFileItems {
|
pub struct SourceFileItems {
|
||||||
file_id: HirFileId,
|
file_id: HirFileId,
|
||||||
arena: Arena<SourceFileItemId, TreeArc<SyntaxNode>>,
|
arena: Arena<SourceFileItemId, SyntaxNodePtr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceFileItems {
|
impl SourceFileItems {
|
||||||
|
@ -329,15 +328,15 @@ impl SourceFileItems {
|
||||||
// trait does not chage ids of top-level items, which helps caching.
|
// trait does not chage ids of top-level items, which helps caching.
|
||||||
bfs(source_file.syntax(), |it| {
|
bfs(source_file.syntax(), |it| {
|
||||||
if let Some(module_item) = ast::ModuleItem::cast(it) {
|
if let Some(module_item) = ast::ModuleItem::cast(it) {
|
||||||
self.alloc(module_item.syntax().to_owned());
|
self.alloc(module_item.syntax());
|
||||||
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
|
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
|
||||||
self.alloc(macro_call.syntax().to_owned());
|
self.alloc(macro_call.syntax());
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc(&mut self, item: TreeArc<SyntaxNode>) -> SourceFileItemId {
|
fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
|
||||||
self.arena.alloc(item)
|
self.arena.alloc(SyntaxNodePtr::new(item))
|
||||||
}
|
}
|
||||||
pub(crate) fn id_of(&self, file_id: HirFileId, item: &SyntaxNode) -> SourceFileItemId {
|
pub(crate) fn id_of(&self, file_id: HirFileId, item: &SyntaxNode) -> SourceFileItemId {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -348,17 +347,8 @@ impl SourceFileItems {
|
||||||
self.id_of_unchecked(item)
|
self.id_of_unchecked(item)
|
||||||
}
|
}
|
||||||
pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId {
|
pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId {
|
||||||
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| *i == item) {
|
let ptr = SyntaxNodePtr::new(item);
|
||||||
return id;
|
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) {
|
||||||
}
|
|
||||||
// This should not happen. Let's try to give a sensible diagnostics.
|
|
||||||
if let Some((id, i)) = self.arena.iter().find(|(_id, i)| i.range() == item.range()) {
|
|
||||||
// FIXME(#288): whyyy are we getting here?
|
|
||||||
log::error!(
|
|
||||||
"unequal syntax nodes with the same range:\n{:?}\n{:?}",
|
|
||||||
item,
|
|
||||||
i
|
|
||||||
);
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
panic!(
|
panic!(
|
||||||
|
@ -367,15 +357,11 @@ impl SourceFileItems {
|
||||||
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
|
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub fn id_of_parse(&self) -> SourceFileItemId {
|
|
||||||
let (id, _syntax) = self.arena.iter().next().unwrap();
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Index<SourceFileItemId> for SourceFileItems {
|
impl std::ops::Index<SourceFileItemId> for SourceFileItems {
|
||||||
type Output = SyntaxNode;
|
type Output = SyntaxNodePtr;
|
||||||
fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
|
fn index(&self, idx: SourceFileItemId) -> &SyntaxNodePtr {
|
||||||
&self.arena[idx]
|
&self.arena[idx]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,21 +11,28 @@ use ra_syntax::{
|
||||||
use ra_arena::{Arena, RawId, impl_arena_id};
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource};
|
use crate::{
|
||||||
|
Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource,
|
||||||
|
ids::SourceFileItemId,
|
||||||
|
};
|
||||||
|
|
||||||
impl ModuleSource {
|
impl ModuleSource {
|
||||||
pub(crate) fn from_source_item_id(
|
pub(crate) fn new(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
source_item_id: SourceItemId,
|
file_id: HirFileId,
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
) -> ModuleSource {
|
) -> ModuleSource {
|
||||||
let module_syntax = db.file_item(source_item_id);
|
match decl_id {
|
||||||
if let Some(source_file) = ast::SourceFile::cast(&module_syntax) {
|
Some(item_id) => {
|
||||||
ModuleSource::SourceFile(source_file.to_owned())
|
let module = db.file_item(SourceItemId { file_id, item_id });
|
||||||
} else if let Some(module) = ast::Module::cast(&module_syntax) {
|
let module = ast::Module::cast(&*module).unwrap();
|
||||||
assert!(module.item_list().is_some(), "expected inline module");
|
assert!(module.item_list().is_some(), "expected inline module");
|
||||||
ModuleSource::Module(module.to_owned())
|
ModuleSource::Module(module.to_owned())
|
||||||
} else {
|
}
|
||||||
panic!("expected file or inline module")
|
None => {
|
||||||
|
let source_file = db.hir_parse(file_id);
|
||||||
|
ModuleSource::SourceFile(source_file)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,18 +41,18 @@ impl ModuleSource {
|
||||||
pub struct Submodule {
|
pub struct Submodule {
|
||||||
name: Name,
|
name: Name,
|
||||||
is_declaration: bool,
|
is_declaration: bool,
|
||||||
source: SourceItemId,
|
decl_id: SourceFileItemId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Submodule {
|
impl Submodule {
|
||||||
pub(crate) fn submodules_query(
|
pub(crate) fn submodules_query(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
source: SourceItemId,
|
file_id: HirFileId,
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
) -> Arc<Vec<Submodule>> {
|
) -> Arc<Vec<Submodule>> {
|
||||||
db.check_canceled();
|
db.check_canceled();
|
||||||
let file_id = source.file_id;
|
|
||||||
let file_items = db.file_items(file_id);
|
let file_items = db.file_items(file_id);
|
||||||
let module_source = ModuleSource::from_source_item_id(db, source);
|
let module_source = ModuleSource::new(db, file_id, decl_id);
|
||||||
let submodules = match module_source {
|
let submodules = match module_source {
|
||||||
ModuleSource::SourceFile(source_file) => {
|
ModuleSource::SourceFile(source_file) => {
|
||||||
collect_submodules(file_id, &file_items, &*source_file)
|
collect_submodules(file_id, &file_items, &*source_file)
|
||||||
|
@ -54,6 +61,7 @@ impl Submodule {
|
||||||
collect_submodules(file_id, &file_items, module.item_list().unwrap())
|
collect_submodules(file_id, &file_items, module.item_list().unwrap())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return Arc::new(submodules);
|
return Arc::new(submodules);
|
||||||
|
|
||||||
fn collect_submodules(
|
fn collect_submodules(
|
||||||
|
@ -75,10 +83,7 @@ impl Submodule {
|
||||||
let sub = Submodule {
|
let sub = Submodule {
|
||||||
name,
|
name,
|
||||||
is_declaration: module.has_semi(),
|
is_declaration: module.has_semi(),
|
||||||
source: SourceItemId {
|
decl_id: file_items.id_of(file_id, module.syntax()),
|
||||||
file_id,
|
|
||||||
item_id: Some(file_items.id_of(file_id, module.syntax())),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
Some(sub)
|
Some(sub)
|
||||||
})
|
})
|
||||||
|
@ -110,7 +115,9 @@ pub struct ModuleTree {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct ModuleData {
|
pub struct ModuleData {
|
||||||
source: SourceItemId,
|
file_id: HirFileId,
|
||||||
|
/// Points to `ast::Module`, `None` for the whole file.
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
parent: Option<LinkId>,
|
parent: Option<LinkId>,
|
||||||
children: Vec<LinkId>,
|
children: Vec<LinkId>,
|
||||||
}
|
}
|
||||||
|
@ -136,8 +143,15 @@ impl ModuleTree {
|
||||||
self.mods.iter().map(|(id, _)| id)
|
self.mods.iter().map(|(id, _)| id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_module_by_source(&self, source: SourceItemId) -> Option<ModuleId> {
|
pub(crate) fn find_module_by_source(
|
||||||
let (res, _) = self.mods.iter().find(|(_, m)| m.source == source)?;
|
&self,
|
||||||
|
file_id: HirFileId,
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
|
) -> Option<ModuleId> {
|
||||||
|
let (res, _) = self
|
||||||
|
.mods
|
||||||
|
.iter()
|
||||||
|
.find(|(_, m)| (m.file_id, m.decl_id) == (file_id, decl_id))?;
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,11 +161,7 @@ impl ModuleTree {
|
||||||
let source_root_id = db.file_source_root(file_id);
|
let source_root_id = db.file_source_root(file_id);
|
||||||
|
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
let source = SourceItemId {
|
self.init_subtree(db, &source_root, None, file_id.into(), None);
|
||||||
file_id: file_id.into(),
|
|
||||||
item_id: None,
|
|
||||||
};
|
|
||||||
self.init_subtree(db, &source_root, None, source);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_subtree(
|
fn init_subtree(
|
||||||
|
@ -159,16 +169,21 @@ impl ModuleTree {
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
source_root: &SourceRoot,
|
source_root: &SourceRoot,
|
||||||
parent: Option<LinkId>,
|
parent: Option<LinkId>,
|
||||||
source: SourceItemId,
|
file_id: HirFileId,
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
) -> ModuleId {
|
) -> ModuleId {
|
||||||
let id = self.alloc_mod(ModuleData {
|
let id = self.alloc_mod(ModuleData {
|
||||||
source,
|
file_id,
|
||||||
|
decl_id,
|
||||||
parent,
|
parent,
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
});
|
});
|
||||||
for sub in db.submodules(source).iter() {
|
for sub in db.submodules(file_id, decl_id).iter() {
|
||||||
let link = self.alloc_link(LinkData {
|
let link = self.alloc_link(LinkData {
|
||||||
source: sub.source,
|
source: SourceItemId {
|
||||||
|
file_id,
|
||||||
|
item_id: sub.decl_id,
|
||||||
|
},
|
||||||
name: sub.name.clone(),
|
name: sub.name.clone(),
|
||||||
owner: id,
|
owner: id,
|
||||||
points_to: Vec::new(),
|
points_to: Vec::new(),
|
||||||
|
@ -176,24 +191,17 @@ impl ModuleTree {
|
||||||
});
|
});
|
||||||
|
|
||||||
let (points_to, problem) = if sub.is_declaration {
|
let (points_to, problem) = if sub.is_declaration {
|
||||||
let (points_to, problem) = resolve_submodule(db, source.file_id, &sub.name);
|
let (points_to, problem) = resolve_submodule(db, file_id, &sub.name);
|
||||||
let points_to = points_to
|
let points_to = points_to
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|file_id| {
|
.map(|file_id| {
|
||||||
self.init_subtree(
|
self.init_subtree(db, source_root, Some(link), file_id.into(), None)
|
||||||
db,
|
|
||||||
source_root,
|
|
||||||
Some(link),
|
|
||||||
SourceItemId {
|
|
||||||
file_id: file_id.into(),
|
|
||||||
item_id: None,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
(points_to, problem)
|
(points_to, problem)
|
||||||
} else {
|
} else {
|
||||||
let points_to = self.init_subtree(db, source_root, Some(link), sub.source);
|
let points_to =
|
||||||
|
self.init_subtree(db, source_root, Some(link), file_id, Some(sub.decl_id));
|
||||||
(vec![points_to], None)
|
(vec![points_to], None)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -216,8 +224,11 @@ impl ModuleTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleId {
|
impl ModuleId {
|
||||||
pub(crate) fn source(self, tree: &ModuleTree) -> SourceItemId {
|
pub(crate) fn file_id(self, tree: &ModuleTree) -> HirFileId {
|
||||||
tree.mods[self].source
|
tree.mods[self].file_id
|
||||||
|
}
|
||||||
|
pub(crate) fn decl_id(self, tree: &ModuleTree) -> Option<SourceFileItemId> {
|
||||||
|
tree.mods[self].decl_id
|
||||||
}
|
}
|
||||||
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
|
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
|
||||||
tree.mods[self].parent
|
tree.mods[self].parent
|
||||||
|
|
|
@ -215,7 +215,7 @@ where
|
||||||
// Populate extern crates prelude
|
// Populate extern crates prelude
|
||||||
{
|
{
|
||||||
let root_id = module_id.crate_root(&self.module_tree);
|
let root_id = module_id.crate_root(&self.module_tree);
|
||||||
let file_id = root_id.source(&self.module_tree).file_id;
|
let file_id = root_id.file_id(&self.module_tree);
|
||||||
let crate_graph = self.db.crate_graph();
|
let crate_graph = self.db.crate_graph();
|
||||||
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
|
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
|
||||||
{
|
{
|
||||||
|
|
|
@ -121,10 +121,7 @@ impl LoweredModule {
|
||||||
let item_id = file_items.id_of_unchecked(macro_call.syntax());
|
let item_id = file_items.id_of_unchecked(macro_call.syntax());
|
||||||
let loc = MacroCallLoc {
|
let loc = MacroCallLoc {
|
||||||
module,
|
module,
|
||||||
source_item_id: SourceItemId {
|
source_item_id: SourceItemId { file_id, item_id },
|
||||||
file_id,
|
|
||||||
item_id: Some(item_id),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
let id = loc.id(db);
|
let id = loc.id(db);
|
||||||
let file_id = HirFileId::from(id);
|
let file_id = HirFileId::from(id);
|
||||||
|
|
|
@ -4,9 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use ra_syntax::{
|
use ra_syntax::{SyntaxNode, TreeArc};
|
||||||
AstNode, SyntaxNode, TreeArc,
|
|
||||||
};
|
|
||||||
use ra_db::{CrateId};
|
use ra_db::{CrateId};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -32,10 +30,10 @@ pub(super) fn file_item(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
source_item_id: SourceItemId,
|
source_item_id: SourceItemId,
|
||||||
) -> TreeArc<SyntaxNode> {
|
) -> TreeArc<SyntaxNode> {
|
||||||
match source_item_id.item_id {
|
let source_file = db.hir_parse(source_item_id.file_id);
|
||||||
Some(id) => db.file_items(source_item_id.file_id)[id].to_owned(),
|
db.file_items(source_item_id.file_id)[source_item_id.item_id]
|
||||||
None => db.hir_parse(source_item_id.file_id).syntax().to_owned(),
|
.to_node(&source_file)
|
||||||
}
|
.to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn item_map(db: &impl HirDatabase, crate_id: CrateId) -> Arc<ItemMap> {
|
pub(super) fn item_map(db: &impl HirDatabase, crate_id: CrateId) -> Arc<ItemMap> {
|
||||||
|
|
|
@ -13,18 +13,14 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Function, SourceItemId, ModuleDef,
|
HirDatabase, Function, ModuleDef,
|
||||||
AsName, Module,
|
AsName, Module, HirFileId,
|
||||||
ids::LocationCtx,
|
ids::{LocationCtx, SourceFileItemId},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Locates the module by `FileId`. Picks topmost module in the file.
|
/// Locates the module by `FileId`. Picks topmost module in the file.
|
||||||
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Module> {
|
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Module> {
|
||||||
let module_source = SourceItemId {
|
module_from_source(db, file_id.into(), None)
|
||||||
file_id: file_id.into(),
|
|
||||||
item_id: None,
|
|
||||||
};
|
|
||||||
module_from_source(db, module_source)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locates the child module by `mod child;` declaration.
|
/// Locates the child module by `mod child;` declaration.
|
||||||
|
@ -59,11 +55,7 @@ fn module_from_inline(
|
||||||
let file_id = file_id.into();
|
let file_id = file_id.into();
|
||||||
let file_items = db.file_items(file_id);
|
let file_items = db.file_items(file_id);
|
||||||
let item_id = file_items.id_of(file_id, module.syntax());
|
let item_id = file_items.id_of(file_id, module.syntax());
|
||||||
let source = SourceItemId {
|
module_from_source(db, file_id, Some(item_id))
|
||||||
file_id,
|
|
||||||
item_id: Some(item_id),
|
|
||||||
};
|
|
||||||
module_from_source(db, source)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locates the module by child syntax element within the module
|
/// Locates the module by child syntax element within the module
|
||||||
|
@ -83,13 +75,17 @@ pub fn module_from_child_node(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Option<Module> {
|
fn module_from_source(
|
||||||
let source_root_id = db.file_source_root(source.file_id.as_original_file());
|
db: &impl HirDatabase,
|
||||||
|
file_id: HirFileId,
|
||||||
|
decl_id: Option<SourceFileItemId>,
|
||||||
|
) -> Option<Module> {
|
||||||
|
let source_root_id = db.file_source_root(file_id.as_original_file());
|
||||||
db.source_root_crates(source_root_id)
|
db.source_root_crates(source_root_id)
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|&krate| {
|
.find_map(|&krate| {
|
||||||
let module_tree = db.module_tree(krate);
|
let module_tree = db.module_tree(krate);
|
||||||
let module_id = module_tree.find_module_by_source(source)?;
|
let module_id = module_tree.find_module_by_source(file_id, decl_id)?;
|
||||||
Some(Module { krate, module_id })
|
Some(Module { krate, module_id })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue