Add inline source

This commit is contained in:
Aleksey Kladov 2018-11-01 00:50:17 +03:00
parent 223fd2979c
commit f2b654fd44
5 changed files with 93 additions and 46 deletions

View file

@ -148,7 +148,7 @@ fn complete_module_items(
this_item: Option<ast::NameRef>,
acc: &mut Vec<CompletionItem>,
) {
let scope = ModuleScope::from_items(items);
let scope = ModuleScope::new(items); // FIXME
acc.extend(
scope
.entries()

View file

@ -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<Arc<ModuleScope>> {
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))
}

View file

@ -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<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 {
pub(crate) fn as_file(self) -> Option<FileId> {
match self {
ModuleSource::File(f) => f == file_id,
ModuleSource::File(f) => Some(f),
ModuleSource::Inline(..) => None,
}
}
fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode {
match self {
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)]

View file

@ -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<Item = ast::ModuleItem<'a>>) -> ModuleScope {
pub(crate) fn new<'a>(items: impl Iterator<Item = ast::ModuleItem<'a>>) -> 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<Entry>) {
#[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::<Vec<_>>();
assert_eq!(expected, actual.as_slice());
}

View file

@ -222,9 +222,15 @@ impl AnalysisImpl {
.into_iter()
.filter_map(|module_id| {
let link = module_id.parent_link(&module_tree)?;
let ModuleSource::File(file_id) = link.owner(&module_tree).source(&module_tree);
let syntax = self.db.file_syntax(file_id);
let decl = link.bind_source(&module_tree, syntax.ast());
let file_id = match link.owner(&module_tree).source(&module_tree) {
ModuleSource::File(file_id) => file_id,
ModuleSource::Inline(..) => {
//TODO: https://github.com/rust-analyzer/rust-analyzer/issues/181
return None;
}
};
let decl = link.bind_source(&module_tree, &self.db);
let decl = decl.ast();
let sym = FileSymbol {
name: decl.name().unwrap().text(),
@ -243,9 +249,7 @@ impl AnalysisImpl {
.modules_for_file(file_id)
.into_iter()
.map(|it| it.root(&module_tree))
.map(|it| match it.source(&module_tree) {
ModuleSource::File(file_id) => file_id,
})
.filter_map(|it| it.source(&module_tree).as_file())
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
.collect();
@ -367,7 +371,7 @@ impl AnalysisImpl {
})
.collect::<Vec<_>>();
if let Some(m) = module_tree.any_module_for_file(file_id) {
for (name_node, problem) in m.problems(&module_tree, syntax.ast()) {
for (name_node, problem) in m.problems(&module_tree, &self.db) {
let diag = match problem {
Problem::UnresolvedModule { candidate } => {
let create_file = FileSystemEdit::CreateFile {
@ -535,9 +539,7 @@ impl AnalysisImpl {
};
module_id
.child(module_tree, name.as_str())
.map(|it| match it.source(&module_tree) {
ModuleSource::File(file_id) => file_id,
})
.and_then(|it| it.source(&module_tree).as_file())
.into_iter()
.collect()
}