Add inline source
This commit is contained in:
parent
223fd2979c
commit
f2b654fd44
5 changed files with 93 additions and 46 deletions
|
@ -148,7 +148,7 @@ fn complete_module_items(
|
||||||
this_item: Option<ast::NameRef>,
|
this_item: Option<ast::NameRef>,
|
||||||
acc: &mut Vec<CompletionItem>,
|
acc: &mut Vec<CompletionItem>,
|
||||||
) {
|
) {
|
||||||
let scope = ModuleScope::from_items(items);
|
let scope = ModuleScope::new(items); // FIXME
|
||||||
acc.extend(
|
acc.extend(
|
||||||
scope
|
scope
|
||||||
.entries()
|
.entries()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, NameOwner},
|
ast::{self, ModuleItemOwner, NameOwner},
|
||||||
SmolStr,
|
SmolStr,
|
||||||
};
|
};
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
|
@ -15,7 +15,8 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleTree, Problem,
|
LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleSource, ModuleSourceNode,
|
||||||
|
ModuleTree, Problem,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn submodules(
|
pub(crate) fn submodules(
|
||||||
|
@ -45,9 +46,14 @@ pub(crate) fn module_scope(
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
) -> Cancelable<Arc<ModuleScope>> {
|
) -> Cancelable<Arc<ModuleScope>> {
|
||||||
let tree = db.module_tree(source_root_id)?;
|
let tree = db.module_tree(source_root_id)?;
|
||||||
let ModuleSource::File(file_id) = module_id.source(&tree);
|
let source = module_id.source(&tree).resolve(db);
|
||||||
let syntax = db.file_syntax(file_id);
|
let res = match source {
|
||||||
let res = ModuleScope::new(&syntax);
|
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))
|
Ok(Arc::new(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
|
|
||||||
use crate::FileId;
|
use crate::{db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId};
|
||||||
|
|
||||||
pub(crate) use self::scope::ModuleScope;
|
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)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
||||||
pub(crate) struct ModuleId(u32);
|
pub(crate) struct ModuleId(u32);
|
||||||
|
|
||||||
|
@ -89,14 +106,18 @@ impl ModuleId {
|
||||||
.find(|it| it.name == name)?;
|
.find(|it| it.name == name)?;
|
||||||
Some(*link.points_to.first()?)
|
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)
|
tree.module(self)
|
||||||
.children
|
.children
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|&it| {
|
.filter_map(|&it| {
|
||||||
let p = tree.link(it).problem.clone()?;
|
let p = tree.link(it).problem.clone()?;
|
||||||
let s = it.bind_source(tree, root);
|
let s = it.bind_source(tree, db);
|
||||||
let s = s.name().unwrap().syntax().owned();
|
let s = s.ast().name().unwrap().syntax().owned();
|
||||||
Some((s, p))
|
Some((s, p))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -107,11 +128,24 @@ impl LinkId {
|
||||||
pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
|
pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
|
||||||
tree.link(self).owner
|
tree.link(self).owner
|
||||||
}
|
}
|
||||||
pub(crate) fn bind_source<'a>(self, tree: &ModuleTree, root: ast::Root<'a>) -> ast::Module<'a> {
|
pub(crate) fn bind_source<'a>(
|
||||||
imp::modules(root)
|
self,
|
||||||
.find(|(name, _)| name == &tree.link(self).name)
|
tree: &ModuleTree,
|
||||||
.unwrap()
|
db: &impl SyntaxDatabase,
|
||||||
.1
|
) -> 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>,
|
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 {
|
impl ModuleSource {
|
||||||
fn is_file(self, file_id: FileId) -> bool {
|
pub(crate) fn as_file(self) -> Option<FileId> {
|
||||||
match self {
|
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)]
|
#[derive(Hash, Debug, PartialEq, Eq)]
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
//! Backend for module-level scope resolution & completion
|
//! Backend for module-level scope resolution & completion
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{ast, AstNode, SmolStr};
|
||||||
ast::{self, ModuleItemOwner},
|
|
||||||
AstNode, File, SmolStr,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::syntax_ptr::LocalSyntaxPtr;
|
use crate::syntax_ptr::LocalSyntaxPtr;
|
||||||
|
|
||||||
|
@ -28,11 +25,7 @@ enum EntryKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleScope {
|
impl ModuleScope {
|
||||||
pub fn new(file: &File) -> ModuleScope {
|
pub(crate) fn new<'a>(items: impl Iterator<Item = ast::ModuleItem<'a>>) -> ModuleScope {
|
||||||
ModuleScope::from_items(file.ast().items())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_items<'a>(items: impl Iterator<Item = ast::ModuleItem<'a>>) -> ModuleScope {
|
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
for item in items {
|
for item in items {
|
||||||
let entry = match item {
|
let entry = match item {
|
||||||
|
@ -102,11 +95,11 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use ra_syntax::File;
|
use ra_syntax::{ast::ModuleItemOwner, File};
|
||||||
|
|
||||||
fn do_check(code: &str, expected: &[&str]) {
|
fn do_check(code: &str, expected: &[&str]) {
|
||||||
let file = File::parse(&code);
|
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<_>>();
|
let actual = scope.entries.iter().map(|it| it.name()).collect::<Vec<_>>();
|
||||||
assert_eq!(expected, actual.as_slice());
|
assert_eq!(expected, actual.as_slice());
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,9 +222,15 @@ impl AnalysisImpl {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|module_id| {
|
.filter_map(|module_id| {
|
||||||
let link = module_id.parent_link(&module_tree)?;
|
let link = module_id.parent_link(&module_tree)?;
|
||||||
let ModuleSource::File(file_id) = link.owner(&module_tree).source(&module_tree);
|
let file_id = match link.owner(&module_tree).source(&module_tree) {
|
||||||
let syntax = self.db.file_syntax(file_id);
|
ModuleSource::File(file_id) => file_id,
|
||||||
let decl = link.bind_source(&module_tree, syntax.ast());
|
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 {
|
let sym = FileSymbol {
|
||||||
name: decl.name().unwrap().text(),
|
name: decl.name().unwrap().text(),
|
||||||
|
@ -243,9 +249,7 @@ impl AnalysisImpl {
|
||||||
.modules_for_file(file_id)
|
.modules_for_file(file_id)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|it| it.root(&module_tree))
|
.map(|it| it.root(&module_tree))
|
||||||
.map(|it| match it.source(&module_tree) {
|
.filter_map(|it| it.source(&module_tree).as_file())
|
||||||
ModuleSource::File(file_id) => file_id,
|
|
||||||
})
|
|
||||||
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
|
.filter_map(|it| crate_graph.crate_id_for_crate_root(it))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -367,7 +371,7 @@ impl AnalysisImpl {
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if let Some(m) = module_tree.any_module_for_file(file_id) {
|
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 {
|
let diag = match problem {
|
||||||
Problem::UnresolvedModule { candidate } => {
|
Problem::UnresolvedModule { candidate } => {
|
||||||
let create_file = FileSystemEdit::CreateFile {
|
let create_file = FileSystemEdit::CreateFile {
|
||||||
|
@ -535,9 +539,7 @@ impl AnalysisImpl {
|
||||||
};
|
};
|
||||||
module_id
|
module_id
|
||||||
.child(module_tree, name.as_str())
|
.child(module_tree, name.as_str())
|
||||||
.map(|it| match it.source(&module_tree) {
|
.and_then(|it| it.source(&module_tree).as_file())
|
||||||
ModuleSource::File(file_id) => file_id,
|
|
||||||
})
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue