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

@ -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)]