From 223fd2979c277cf402e4876d0e9a69257702f731 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 1 Nov 2018 00:15:11 +0300 Subject: Introduce ModuleSource --- crates/ra_analysis/src/descriptors/module/imp.rs | 8 +++--- crates/ra_analysis/src/descriptors/module/mod.rs | 31 +++++++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'crates/ra_analysis/src/descriptors/module') diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs index 1c102f4e5..22dbe7184 100644 --- a/crates/ra_analysis/src/descriptors/module/imp.rs +++ b/crates/ra_analysis/src/descriptors/module/imp.rs @@ -14,7 +14,9 @@ use crate::{ 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( db: &impl DescriptorDatabase, @@ -43,7 +45,7 @@ pub(crate) fn module_scope( module_id: ModuleId, ) -> Cancelable> { 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 res = ModuleScope::new(&syntax); Ok(Arc::new(res)) @@ -106,7 +108,7 @@ fn build_subtree( ) -> Cancelable { visited.insert(file_id); let id = tree.push_mod(ModuleData { - file_id, + source: ModuleSource::File(file_id), parent, children: Vec::new(), }); diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index 302e3e81c..cbccdb2e2 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -11,6 +11,13 @@ use crate::FileId; 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)] pub(crate) struct ModuleTree { mods: Vec, @@ -22,7 +29,7 @@ impl ModuleTree { self.mods .iter() .enumerate() - .filter(|(_idx, it)| it.file_id == file_id) + .filter(|(_idx, it)| it.source.is_file(file_id)) .map(|(idx, _)| ModuleId(idx as u32)) .collect() } @@ -50,8 +57,8 @@ pub enum Problem { } impl ModuleId { - pub(crate) fn file_id(self, tree: &ModuleTree) -> FileId { - tree.module(self).file_id + pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource { + tree.module(self).source } pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option { tree.module(self).parent @@ -110,11 +117,27 @@ impl LinkId { #[derive(Debug, PartialEq, Eq, Hash)] struct ModuleData { - file_id: FileId, + source: ModuleSource, parent: Option, children: Vec, } +/// `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)] struct LinkData { owner: ModuleId, -- cgit v1.2.3