rename module source

This commit is contained in:
Aleksey Kladov 2018-11-07 18:42:30 +03:00
parent 2ed1514df3
commit 22949dab26
4 changed files with 32 additions and 32 deletions

View file

@ -34,7 +34,8 @@ pub(crate) fn resolve_based_completion(
let source_root_id = db.file_source_root(position.file_id); let source_root_id = db.file_source_root(position.file_id);
let file = db.file_syntax(position.file_id); let file = db.file_syntax(position.file_id);
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id)?;
let module_id = match module_tree.any_module_for_source(ModuleSource::File(position.file_id)) { let module_id =
match module_tree.any_module_for_source(ModuleSource::SourceFile(position.file_id)) {
None => return Ok(None), None => return Ok(None),
Some(it) => it, Some(it) => it,
}; };

View file

@ -41,8 +41,8 @@ pub(crate) fn submodules(
db::check_canceled(db)?; db::check_canceled(db)?;
let file_id = source.file_id(); let file_id = source.file_id();
let submodules = match source.resolve(db) { let submodules = match source.resolve(db) {
ModuleSourceNode::Root(it) => collect_submodules(file_id, it.borrowed()), ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()),
ModuleSourceNode::Inline(it) => it ModuleSourceNode::Module(it) => it
.borrowed() .borrowed()
.item_list() .item_list()
.map(|it| collect_submodules(file_id, it)) .map(|it| collect_submodules(file_id, it))
@ -89,8 +89,8 @@ pub(crate) fn module_scope(
let tree = db.module_tree(source_root_id)?; let tree = db.module_tree(source_root_id)?;
let source = module_id.source(&tree).resolve(db); let source = module_id.source(&tree).resolve(db);
let res = match source { let res = match source {
ModuleSourceNode::Root(root) => ModuleScope::new(root.borrowed().items()), ModuleSourceNode::SourceFile(it) => ModuleScope::new(it.borrowed().items()),
ModuleSourceNode::Inline(inline) => match inline.borrowed().item_list() { ModuleSourceNode::Module(it) => match it.borrowed().item_list() {
Some(items) => ModuleScope::new(items.items()), Some(items) => ModuleScope::new(items.items()),
None => ModuleScope::new(std::iter::empty()), None => ModuleScope::new(std::iter::empty()),
}, },
@ -121,7 +121,7 @@ fn create_module_tree<'a>(
let source_root = db.source_root(source_root); let source_root = db.source_root(source_root);
for &file_id in source_root.files.iter() { for &file_id in source_root.files.iter() {
let source = ModuleSource::File(file_id); let source = ModuleSource::SourceFile(file_id);
if visited.contains(&source) { if visited.contains(&source) {
continue; // TODO: use explicit crate_roots here continue; // TODO: use explicit crate_roots here
} }
@ -181,7 +181,7 @@ fn build_subtree(
visited, visited,
roots, roots,
Some(link), Some(link),
ModuleSource::File(file_id), ModuleSource::SourceFile(file_id),
), ),
}) })
.collect::<Cancelable<Vec<_>>>()?; .collect::<Cancelable<Vec<_>>>()?;
@ -213,8 +213,8 @@ fn resolve_submodule(
file_resolver: &FileResolverImp, file_resolver: &FileResolverImp,
) -> (Vec<FileId>, Option<Problem>) { ) -> (Vec<FileId>, Option<Problem>) {
let file_id = match source { let file_id = match source {
ModuleSource::File(it) => it, ModuleSource::SourceFile(it) => it,
ModuleSource::Inline(..) => { ModuleSource::Module(..) => {
// TODO // TODO
return (Vec::new(), None); return (Vec::new(), None);
} }

View file

@ -41,19 +41,18 @@ impl ModuleTree {
/// `ModuleSource` is the syntax tree element that produced this module: /// `ModuleSource` is the syntax tree element that produced this module:
/// either a file, or an inlinde module. /// either a file, or an inlinde module.
/// TODO: we don't produce Inline modules yet
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum ModuleSource { pub(crate) enum ModuleSource {
File(FileId), SourceFile(FileId),
#[allow(dead_code)] #[allow(dead_code)]
Inline(SyntaxPtr), Module(SyntaxPtr),
} }
/// An owned syntax node for a module. Unlike `ModuleSource`, /// An owned syntax node for a module. Unlike `ModuleSource`,
/// this holds onto the AST for the whole file. /// this holds onto the AST for the whole file.
enum ModuleSourceNode { enum ModuleSourceNode {
Root(ast::SourceFileNode), SourceFile(ast::SourceFileNode),
Inline(ast::ModuleNode), Module(ast::ModuleNode),
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
@ -135,14 +134,14 @@ impl LinkId {
) -> ast::ModuleNode { ) -> ast::ModuleNode {
let owner = self.owner(tree); let owner = self.owner(tree);
match owner.source(tree).resolve(db) { match owner.source(tree).resolve(db) {
ModuleSourceNode::Root(root) => { ModuleSourceNode::SourceFile(root) => {
let ast = imp::modules(root.borrowed()) let ast = imp::modules(root.borrowed())
.find(|(name, _)| name == &tree.link(self).name) .find(|(name, _)| name == &tree.link(self).name)
.unwrap() .unwrap()
.1; .1;
ast.owned() ast.owned()
} }
ModuleSourceNode::Inline(it) => it, ModuleSourceNode::Module(it) => it,
} }
} }
} }
@ -158,34 +157,34 @@ impl ModuleSource {
pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource {
assert!(!module.has_semi()); assert!(!module.has_semi());
let ptr = SyntaxPtr::new(file_id, module.syntax()); let ptr = SyntaxPtr::new(file_id, module.syntax());
ModuleSource::Inline(ptr) ModuleSource::Module(ptr)
} }
pub(crate) fn as_file(self) -> Option<FileId> { pub(crate) fn as_file(self) -> Option<FileId> {
match self { match self {
ModuleSource::File(f) => Some(f), ModuleSource::SourceFile(f) => Some(f),
ModuleSource::Inline(..) => None, ModuleSource::Module(..) => None,
} }
} }
pub(crate) fn file_id(self) -> FileId { pub(crate) fn file_id(self) -> FileId {
match self { match self {
ModuleSource::File(f) => f, ModuleSource::SourceFile(f) => f,
ModuleSource::Inline(ptr) => ptr.file_id(), ModuleSource::Module(ptr) => ptr.file_id(),
} }
} }
fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode {
match self { match self {
ModuleSource::File(file_id) => { ModuleSource::SourceFile(file_id) => {
let syntax = db.file_syntax(file_id); let syntax = db.file_syntax(file_id);
ModuleSourceNode::Root(syntax.ast().owned()) ModuleSourceNode::SourceFile(syntax.ast().owned())
} }
ModuleSource::Inline(ptr) => { ModuleSource::Module(ptr) => {
let syntax = db.resolve_syntax_ptr(ptr); let syntax = db.resolve_syntax_ptr(ptr);
let syntax = syntax.borrowed(); let syntax = syntax.borrowed();
let module = ast::Module::cast(syntax).unwrap(); let module = ast::Module::cast(syntax).unwrap();
ModuleSourceNode::Inline(module.owned()) ModuleSourceNode::Module(module.owned())
} }
} }
} }

View file

@ -226,7 +226,7 @@ impl AnalysisImpl {
let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset)
{ {
Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m),
_ => ModuleSource::File(position.file_id), _ => ModuleSource::SourceFile(position.file_id),
}; };
let res = module_tree let res = module_tree
@ -254,7 +254,7 @@ impl AnalysisImpl {
let module_tree = self.module_tree(file_id)?; let module_tree = self.module_tree(file_id)?;
let crate_graph = self.db.crate_graph(); let crate_graph = self.db.crate_graph();
let res = module_tree let res = module_tree
.modules_for_source(ModuleSource::File(file_id)) .modules_for_source(ModuleSource::SourceFile(file_id))
.into_iter() .into_iter()
.map(|it| it.root(&module_tree)) .map(|it| it.root(&module_tree))
.filter_map(|it| it.source(&module_tree).as_file()) .filter_map(|it| it.source(&module_tree).as_file())
@ -386,7 +386,7 @@ impl AnalysisImpl {
fix: None, fix: None,
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if let Some(m) = module_tree.any_module_for_source(ModuleSource::File(file_id)) { if let Some(m) = module_tree.any_module_for_source(ModuleSource::SourceFile(file_id)) {
for (name_node, problem) in m.problems(&module_tree, &*self.db) { 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 } => {
@ -548,7 +548,7 @@ impl AnalysisImpl {
Some(name) => name.text(), Some(name) => name.text(),
None => return Vec::new(), None => return Vec::new(),
}; };
let module_id = match module_tree.any_module_for_source(ModuleSource::File(file_id)) { let module_id = match module_tree.any_module_for_source(ModuleSource::SourceFile(file_id)) {
Some(id) => id, Some(id) => id,
None => return Vec::new(), None => return Vec::new(),
}; };