mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Introduce ModuleSource
This commit is contained in:
parent
d685a9b564
commit
223fd2979c
3 changed files with 40 additions and 11 deletions
|
@ -14,7 +14,9 @@ use crate::{
|
||||||
Cancelable, FileId, FileResolverImp,
|
Cancelable, FileId, FileResolverImp,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleTree, Problem};
|
use super::{
|
||||||
|
LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleTree, Problem,
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) fn submodules(
|
pub(crate) fn submodules(
|
||||||
db: &impl DescriptorDatabase,
|
db: &impl DescriptorDatabase,
|
||||||
|
@ -43,7 +45,7 @@ pub(crate) fn module_scope(
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
) -> Cancelable<Arc<ModuleScope>> {
|
) -> Cancelable<Arc<ModuleScope>> {
|
||||||
let tree = db.module_tree(source_root_id)?;
|
let tree = db.module_tree(source_root_id)?;
|
||||||
let file_id = module_id.file_id(&tree);
|
let ModuleSource::File(file_id) = module_id.source(&tree);
|
||||||
let syntax = db.file_syntax(file_id);
|
let syntax = db.file_syntax(file_id);
|
||||||
let res = ModuleScope::new(&syntax);
|
let res = ModuleScope::new(&syntax);
|
||||||
Ok(Arc::new(res))
|
Ok(Arc::new(res))
|
||||||
|
@ -106,7 +108,7 @@ fn build_subtree(
|
||||||
) -> Cancelable<ModuleId> {
|
) -> Cancelable<ModuleId> {
|
||||||
visited.insert(file_id);
|
visited.insert(file_id);
|
||||||
let id = tree.push_mod(ModuleData {
|
let id = tree.push_mod(ModuleData {
|
||||||
file_id,
|
source: ModuleSource::File(file_id),
|
||||||
parent,
|
parent,
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,6 +11,13 @@ use crate::FileId;
|
||||||
|
|
||||||
pub(crate) use self::scope::ModuleScope;
|
pub(crate) use self::scope::ModuleScope;
|
||||||
|
|
||||||
|
/// Phisically, rust source is organized as a set of files, but logically it is
|
||||||
|
/// organized as a tree of modules. Usually, a single file corresponds to a
|
||||||
|
/// single module, but it is not nessary the case.
|
||||||
|
///
|
||||||
|
/// Module encapsulate the logic of transitioning from the fuzzy world of files
|
||||||
|
/// (which can have multiple parents) to the precise world of modules (which
|
||||||
|
/// always have one parent).
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
pub(crate) struct ModuleTree {
|
pub(crate) struct ModuleTree {
|
||||||
mods: Vec<ModuleData>,
|
mods: Vec<ModuleData>,
|
||||||
|
@ -22,7 +29,7 @@ impl ModuleTree {
|
||||||
self.mods
|
self.mods
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(_idx, it)| it.file_id == file_id)
|
.filter(|(_idx, it)| it.source.is_file(file_id))
|
||||||
.map(|(idx, _)| ModuleId(idx as u32))
|
.map(|(idx, _)| ModuleId(idx as u32))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -50,8 +57,8 @@ pub enum Problem {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleId {
|
impl ModuleId {
|
||||||
pub(crate) fn file_id(self, tree: &ModuleTree) -> FileId {
|
pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource {
|
||||||
tree.module(self).file_id
|
tree.module(self).source
|
||||||
}
|
}
|
||||||
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
|
pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
|
||||||
tree.module(self).parent
|
tree.module(self).parent
|
||||||
|
@ -110,11 +117,27 @@ impl LinkId {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
struct ModuleData {
|
struct ModuleData {
|
||||||
file_id: FileId,
|
source: ModuleSource,
|
||||||
parent: Option<LinkId>,
|
parent: Option<LinkId>,
|
||||||
children: Vec<LinkId>,
|
children: Vec<LinkId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `ModuleSource` is the syntax tree element that produced this module:
|
||||||
|
/// either a file, or an inlinde module.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub(crate) enum ModuleSource {
|
||||||
|
File(FileId),
|
||||||
|
// Inline(SyntaxPtr),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModuleSource {
|
||||||
|
fn is_file(self, file_id: FileId) -> bool {
|
||||||
|
match self {
|
||||||
|
ModuleSource::File(f) => f == file_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Hash, Debug, PartialEq, Eq)]
|
#[derive(Hash, Debug, PartialEq, Eq)]
|
||||||
struct LinkData {
|
struct LinkData {
|
||||||
owner: ModuleId,
|
owner: ModuleId,
|
||||||
|
|
|
@ -20,7 +20,7 @@ use crate::{
|
||||||
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
db::{self, FileSyntaxQuery, SyntaxDatabase},
|
||||||
descriptors::{
|
descriptors::{
|
||||||
function::{FnDescriptor, FnId},
|
function::{FnDescriptor, FnId},
|
||||||
module::{ModuleTree, Problem},
|
module::{ModuleSource, ModuleTree, Problem},
|
||||||
DeclarationDescriptor, DescriptorDatabase,
|
DeclarationDescriptor, DescriptorDatabase,
|
||||||
},
|
},
|
||||||
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
|
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
|
||||||
|
@ -222,7 +222,7 @@ impl AnalysisImpl {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|module_id| {
|
.filter_map(|module_id| {
|
||||||
let link = module_id.parent_link(&module_tree)?;
|
let link = module_id.parent_link(&module_tree)?;
|
||||||
let file_id = link.owner(&module_tree).file_id(&module_tree);
|
let ModuleSource::File(file_id) = link.owner(&module_tree).source(&module_tree);
|
||||||
let syntax = self.db.file_syntax(file_id);
|
let syntax = self.db.file_syntax(file_id);
|
||||||
let decl = link.bind_source(&module_tree, syntax.ast());
|
let decl = link.bind_source(&module_tree, syntax.ast());
|
||||||
|
|
||||||
|
@ -243,7 +243,9 @@ impl AnalysisImpl {
|
||||||
.modules_for_file(file_id)
|
.modules_for_file(file_id)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|it| it.root(&module_tree))
|
.map(|it| it.root(&module_tree))
|
||||||
.map(|it| it.file_id(&module_tree))
|
.map(|it| match it.source(&module_tree) {
|
||||||
|
ModuleSource::File(file_id) => file_id,
|
||||||
|
})
|
||||||
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
|
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -533,7 +535,9 @@ impl AnalysisImpl {
|
||||||
};
|
};
|
||||||
module_id
|
module_id
|
||||||
.child(module_tree, name.as_str())
|
.child(module_tree, name.as_str())
|
||||||
.map(|it| it.file_id(module_tree))
|
.map(|it| match it.source(&module_tree) {
|
||||||
|
ModuleSource::File(file_id) => file_id,
|
||||||
|
})
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue