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') 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 From f2b654fd443ce1a0a31a9eaf1a1c25e911d0001a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 1 Nov 2018 00:50:17 +0300 Subject: Add inline source --- crates/ra_analysis/src/descriptors/module/imp.rs | 16 +++-- crates/ra_analysis/src/descriptors/module/mod.rs | 84 +++++++++++++++++----- crates/ra_analysis/src/descriptors/module/scope.rs | 15 ++-- 3 files changed, 80 insertions(+), 35 deletions(-) (limited to 'crates/ra_analysis/src/descriptors') diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs index 22dbe7184..3a010ecf5 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, NameOwner}, + ast::{self, ModuleItemOwner, NameOwner}, SmolStr, }; use relative_path::RelativePathBuf; @@ -15,7 +15,8 @@ use crate::{ }; use super::{ - LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleTree, Problem, + LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleSourceNode, + ModuleTree, Problem, }; pub(crate) fn submodules( @@ -45,9 +46,14 @@ pub(crate) fn module_scope( module_id: ModuleId, ) -> Cancelable> { let tree = db.module_tree(source_root_id)?; - let ModuleSource::File(file_id) = module_id.source(&tree); - let syntax = db.file_syntax(file_id); - let res = ModuleScope::new(&syntax); + let source = module_id.source(&tree).resolve(db); + let res = match source { + ModuleSourceNode::Root(root) => ModuleScope::new(root.ast().items()), + ModuleSourceNode::Inline(inline) => match inline.ast().item_list() { + Some(items) => ModuleScope::new(items.items()), + None => ModuleScope::new(std::iter::empty()), + }, + }; Ok(Arc::new(res)) } diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index cbccdb2e2..e22489fc1 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -7,7 +7,7 @@ use ra_syntax::{ }; use relative_path::RelativePathBuf; -use crate::FileId; +use crate::{db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId}; pub(crate) use self::scope::ModuleScope; @@ -39,6 +39,23 @@ impl ModuleTree { } } +/// `ModuleSource` is the syntax tree element that produced this module: +/// either a file, or an inlinde module. +/// TODO: we don't produce Inline modules yet +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) enum ModuleSource { + File(FileId), + #[allow(dead_code)] + Inline(SyntaxPtr), +} + +/// An owned syntax node for a module. Unlike `ModuleSource`, +/// this holds onto the AST for the whole file. +enum ModuleSourceNode { + Root(ast::RootNode), + Inline(ast::ModuleNode), +} + #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] pub(crate) struct ModuleId(u32); @@ -89,14 +106,18 @@ impl ModuleId { .find(|it| it.name == name)?; Some(*link.points_to.first()?) } - pub(crate) fn problems(self, tree: &ModuleTree, root: ast::Root) -> Vec<(SyntaxNode, Problem)> { + pub(crate) fn problems( + self, + tree: &ModuleTree, + db: &impl SyntaxDatabase, + ) -> Vec<(SyntaxNode, Problem)> { tree.module(self) .children .iter() .filter_map(|&it| { let p = tree.link(it).problem.clone()?; - let s = it.bind_source(tree, root); - let s = s.name().unwrap().syntax().owned(); + let s = it.bind_source(tree, db); + let s = s.ast().name().unwrap().syntax().owned(); Some((s, p)) }) .collect() @@ -107,11 +128,24 @@ impl LinkId { pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { tree.link(self).owner } - pub(crate) fn bind_source<'a>(self, tree: &ModuleTree, root: ast::Root<'a>) -> ast::Module<'a> { - imp::modules(root) - .find(|(name, _)| name == &tree.link(self).name) - .unwrap() - .1 + pub(crate) fn bind_source<'a>( + self, + tree: &ModuleTree, + db: &impl SyntaxDatabase, + ) -> ast::ModuleNode { + let owner = self.owner(tree); + match owner.source(tree).resolve(db) { + ModuleSourceNode::Root(root) => { + let ast = imp::modules(root.ast()) + .find(|(name, _)| name == &tree.link(self).name) + .unwrap() + .1; + ast.into() + } + ModuleSourceNode::Inline(..) => { + unimplemented!("https://github.com/rust-analyzer/rust-analyzer/issues/181") + } + } } } @@ -122,20 +156,32 @@ struct ModuleData { 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 { + pub(crate) fn as_file(self) -> Option { + match self { + ModuleSource::File(f) => Some(f), + ModuleSource::Inline(..) => None, + } + } + + fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { match self { - ModuleSource::File(f) => f == file_id, + ModuleSource::File(file_id) => { + let syntax = db.file_syntax(file_id); + ModuleSourceNode::Root(syntax.ast().into()) + } + ModuleSource::Inline(ptr) => { + let syntax = db.resolve_syntax_ptr(ptr); + let syntax = syntax.borrowed(); + let module = ast::Module::cast(syntax).unwrap(); + ModuleSourceNode::Inline(module.into()) + } } } + + fn is_file(self, file_id: FileId) -> bool { + self.as_file() == Some(file_id) + } } #[derive(Hash, Debug, PartialEq, Eq)] diff --git a/crates/ra_analysis/src/descriptors/module/scope.rs b/crates/ra_analysis/src/descriptors/module/scope.rs index 681e272c2..215b31f8e 100644 --- a/crates/ra_analysis/src/descriptors/module/scope.rs +++ b/crates/ra_analysis/src/descriptors/module/scope.rs @@ -1,9 +1,6 @@ //! Backend for module-level scope resolution & completion -use ra_syntax::{ - ast::{self, ModuleItemOwner}, - AstNode, File, SmolStr, -}; +use ra_syntax::{ast, AstNode, SmolStr}; use crate::syntax_ptr::LocalSyntaxPtr; @@ -28,11 +25,7 @@ enum EntryKind { } impl ModuleScope { - pub fn new(file: &File) -> ModuleScope { - ModuleScope::from_items(file.ast().items()) - } - - pub fn from_items<'a>(items: impl Iterator>) -> ModuleScope { + pub(crate) fn new<'a>(items: impl Iterator>) -> ModuleScope { let mut entries = Vec::new(); for item in items { let entry = match item { @@ -102,11 +95,11 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec) { #[cfg(test)] mod tests { use super::*; - use ra_syntax::File; + use ra_syntax::{ast::ModuleItemOwner, File}; fn do_check(code: &str, expected: &[&str]) { let file = File::parse(&code); - let scope = ModuleScope::new(&file); + let scope = ModuleScope::new(file.ast().items()); let actual = scope.entries.iter().map(|it| it.name()).collect::>(); assert_eq!(expected, actual.as_slice()); } -- cgit v1.2.3