From 6bbcfca7aec6408cf27415ae6f965adc472d6f18 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 5 Nov 2018 14:10:20 +0300 Subject: Fully add inline modules to module tree --- crates/ra_analysis/src/descriptors/module/imp.rs | 49 ++++++++++++++++-------- crates/ra_analysis/src/descriptors/module/mod.rs | 12 ++++-- 2 files changed, 42 insertions(+), 19 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 257a323ed..b3b1f1f21 100644 --- a/crates/ra_analysis/src/descriptors/module/imp.rs +++ b/crates/ra_analysis/src/descriptors/module/imp.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use ra_syntax::{ - ast::{self, ModuleItemOwner, NameOwner, AstNode}, + ast::{self, ModuleItemOwner, NameOwner}, SmolStr, }; use relative_path::RelativePathBuf; @@ -12,7 +12,6 @@ use crate::{ descriptors::DescriptorDatabase, input::{SourceRoot, SourceRootId}, Cancelable, FileId, FileResolverImp, - syntax_ptr::SyntaxPtr, }; use super::{ @@ -23,7 +22,7 @@ use super::{ #[derive(Clone, Hash, PartialEq, Eq, Debug)] pub(crate) enum Submodule { Declaration(SmolStr), - Definition(SmolStr, SyntaxPtr), + Definition(SmolStr, ModuleSource), } impl Submodule { @@ -60,7 +59,8 @@ pub(crate) fn submodules( if m.has_semi() { Submodule::Declaration(name) } else { - Submodule::Definition(name, SyntaxPtr::new(file_id, m.syntax())) + let src = ModuleSource::new_inline(file_id, m); + Submodule::Definition(name, src) } }) .collect() @@ -121,7 +121,8 @@ fn create_module_tree<'a>( let source_root = db.source_root(source_root); for &file_id in source_root.files.iter() { - if visited.contains(&file_id) { + let source = ModuleSource::File(file_id); + if visited.contains(&source) { continue; // TODO: use explicit crate_roots here } assert!(!roots.contains_key(&file_id)); @@ -132,7 +133,7 @@ fn create_module_tree<'a>( &mut visited, &mut roots, None, - file_id, + source, )?; roots.insert(file_id, module_id); } @@ -143,18 +144,18 @@ fn build_subtree( db: &impl DescriptorDatabase, source_root: &SourceRoot, tree: &mut ModuleTree, - visited: &mut FxHashSet, + visited: &mut FxHashSet, roots: &mut FxHashMap, parent: Option, - file_id: FileId, + source: ModuleSource, ) -> Cancelable { - visited.insert(file_id); + visited.insert(source); let id = tree.push_mod(ModuleData { - source: ModuleSource::File(file_id), + source, parent, children: Vec::new(), }); - for sub in db.submodules(ModuleSource::File(file_id))?.iter() { + for sub in db.submodules(source)?.iter() { let link = tree.push_link(LinkData { name: sub.name().clone(), owner: id, @@ -165,7 +166,7 @@ fn build_subtree( let (points_to, problem) = match sub { Submodule::Declaration(name) => { let (points_to, problem) = - resolve_submodule(file_id, &name, &source_root.file_resolver); + resolve_submodule(source, &name, &source_root.file_resolver); let points_to = points_to .into_iter() .map(|file_id| match roots.remove(&file_id) { @@ -180,13 +181,24 @@ fn build_subtree( visited, roots, Some(link), - file_id, + ModuleSource::File(file_id), ), }) .collect::>>()?; (points_to, problem) } - Submodule::Definition(..) => continue, + Submodule::Definition(_name, submodule_source) => { + let points_to = build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + *submodule_source, + )?; + (vec![points_to], None) + } }; tree.link_mut(link).points_to = points_to; @@ -196,10 +208,17 @@ fn build_subtree( } fn resolve_submodule( - file_id: FileId, + source: ModuleSource, name: &SmolStr, file_resolver: &FileResolverImp, ) -> (Vec, Option) { + let file_id = match source { + ModuleSource::File(it) => it, + ModuleSource::Inline(..) => { + // TODO + return (Vec::new(), None); + } + }; let mod_name = file_resolver.file_stem(file_id); let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index 8464b0618..3d799ba05 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -142,9 +142,7 @@ impl LinkId { .1; ast.into() } - ModuleSourceNode::Inline(..) => { - unimplemented!("https://github.com/rust-analyzer/rust-analyzer/issues/181") - } + ModuleSourceNode::Inline(it) => it, } } } @@ -157,6 +155,12 @@ struct ModuleData { } impl ModuleSource { + pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { + assert!(!module.has_semi()); + let ptr = SyntaxPtr::new(file_id, module.syntax()); + ModuleSource::Inline(ptr) + } + pub(crate) fn as_file(self) -> Option { match self { ModuleSource::File(f) => Some(f), @@ -164,7 +168,7 @@ impl ModuleSource { } } - fn file_id(self) -> FileId { + pub(crate) fn file_id(self) -> FileId { match self { ModuleSource::File(f) => f, ModuleSource::Inline(ptr) => ptr.file_id(), -- cgit v1.2.3