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/module') 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