ModuleSource is ItemSource

This commit is contained in:
Aleksey Kladov 2018-12-04 22:46:23 +03:00
parent a9e4142f43
commit 45fce90349
4 changed files with 40 additions and 51 deletions

View file

@ -190,10 +190,7 @@ impl AnalysisImpl {
Some(it) => it, Some(it) => it,
}; };
let root = descr.crate_root(); let root = descr.crate_root();
let file_id = root let file_id = root.source().file_id();
.source()
.as_file()
.expect("root module always has a file as a source");
let crate_graph = self.db.crate_graph(); let crate_graph = self.db.crate_graph();
let crate_id = crate_graph.crate_id_for_crate_root(file_id); let crate_id = crate_graph.crate_id_for_crate_root(file_id);

View file

@ -131,6 +131,10 @@ impl SourceFileItems {
.unwrap(); .unwrap();
id id
} }
pub fn id_of_source_file(&self) -> SourceFileItemId {
let (id, _syntax) = self.arena.iter().next().unwrap();
id
}
} }
impl Index<SourceFileItemId> for SourceFileItems { impl Index<SourceFileItemId> for SourceFileItems {

View file

@ -66,7 +66,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::SourceFile(file_id); let source = ModuleSource::new_file(db, file_id);
if visited.contains(&source) { if visited.contains(&source) {
continue; // TODO: use explicit crate_roots here continue; // TODO: use explicit crate_roots here
} }
@ -126,7 +126,7 @@ fn build_subtree(
visited, visited,
roots, roots,
Some(link), Some(link),
ModuleSource::SourceFile(file_id), ModuleSource::new_file(db, file_id),
), ),
}) })
.collect::<Cancelable<Vec<_>>>()?; .collect::<Cancelable<Vec<_>>>()?;
@ -157,13 +157,8 @@ fn resolve_submodule(
name: &SmolStr, name: &SmolStr,
file_resolver: &FileResolverImp, file_resolver: &FileResolverImp,
) -> (Vec<FileId>, Option<Problem>) { ) -> (Vec<FileId>, Option<Problem>) {
let file_id = match source { // TODO: handle submodules of inline modules properly
ModuleSource::SourceFile(it) => it, let file_id = source.file_id();
ModuleSource::Module(..) => {
// TODO
return (Vec::new(), None);
}
};
let mod_name = file_resolver.file_stem(file_id); let mod_name = file_resolver.file_stem(file_id);
let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main";

View file

@ -14,7 +14,7 @@ use ra_db::{SourceRootId, FileId, FilePosition, Cancelable};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{ use crate::{
DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
arena::{Arena, Id}, arena::{Arena, Id},
}; };
@ -37,7 +37,8 @@ impl Module {
db: &impl HirDatabase, db: &impl HirDatabase,
file_id: FileId, file_id: FileId,
) -> Cancelable<Option<Module>> { ) -> Cancelable<Option<Module>> {
Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) let module_source = ModuleSource::new_file(db, file_id);
Module::guess_from_source(db, module_source)
} }
/// Lookup `Module` by position in the source code. Note that this /// Lookup `Module` by position in the source code. Note that this
@ -51,17 +52,16 @@ impl Module {
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(db, position.file_id, m), Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m),
_ => ModuleSource::SourceFile(position.file_id), _ => ModuleSource::new_file(db, position.file_id),
}; };
Module::guess_from_source(db, position.file_id, module_source) Module::guess_from_source(db, module_source)
} }
fn guess_from_source( fn guess_from_source(
db: &impl HirDatabase, db: &impl HirDatabase,
file_id: FileId,
module_source: ModuleSource, module_source: ModuleSource,
) -> Cancelable<Option<Module>> { ) -> Cancelable<Option<Module>> {
let source_root_id = db.file_source_root(file_id); let source_root_id = db.file_source_root(module_source.file_id());
let module_tree = db.module_tree(source_root_id)?; let module_tree = db.module_tree(source_root_id)?;
let res = match module_tree.any_module_for_source(module_source) { let res = match module_tree.any_module_for_source(module_source) {
@ -209,10 +209,7 @@ 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.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ModuleSource { pub struct ModuleSource(SourceItemId);
SourceFile(FileId),
Module(SourceItemId),
}
/// 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.
@ -310,46 +307,42 @@ pub struct ModuleData {
} }
impl ModuleSource { impl ModuleSource {
// precondition: item_id **must** point to module
fn new(file_id: FileId, item_id: SourceFileItemId) -> ModuleSource {
let source_item_id = SourceItemId { file_id, item_id };
ModuleSource(source_item_id)
}
pub(crate) fn new_file(db: &impl HirDatabase, file_id: FileId) -> ModuleSource {
let file_items = db.file_items(file_id);
let item_id = file_items.id_of_source_file();
ModuleSource::new(file_id, item_id)
}
pub(crate) fn new_inline( pub(crate) fn new_inline(
db: &impl HirDatabase, db: &impl HirDatabase,
file_id: FileId, file_id: FileId,
module: ast::Module, m: ast::Module,
) -> ModuleSource { ) -> ModuleSource {
assert!(!module.has_semi()); assert!(!m.has_semi());
let items = db.file_items(file_id); let file_items = db.file_items(file_id);
let item_id = items.id_of(module.syntax()); let item_id = file_items.id_of(m.syntax());
let id = SourceItemId { file_id, item_id }; ModuleSource::new(file_id, item_id)
ModuleSource::Module(id)
}
pub fn as_file(self) -> Option<FileId> {
match self {
ModuleSource::SourceFile(f) => Some(f),
ModuleSource::Module(..) => None,
}
} }
pub fn file_id(self) -> FileId { pub fn file_id(self) -> FileId {
match self { self.0.file_id
ModuleSource::SourceFile(f) => f,
ModuleSource::Module(source_item_id) => source_item_id.file_id,
}
} }
pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
match self { let syntax_node = db.file_item(self.0);
ModuleSource::SourceFile(file_id) => { let syntax_node = syntax_node.borrowed();
let syntax = db.source_file(file_id); if let Some(file) = ast::SourceFile::cast(syntax_node) {
ModuleSourceNode::SourceFile(syntax.ast().owned()) return ModuleSourceNode::SourceFile(file.owned());
} }
ModuleSource::Module(item_id) => { let module = ast::Module::cast(syntax_node).unwrap();
let syntax = db.file_item(item_id);
let syntax = syntax.borrowed();
let module = ast::Module::cast(syntax).unwrap();
ModuleSourceNode::Module(module.owned()) ModuleSourceNode::Module(module.owned())
} }
}
}
} }
#[derive(Hash, Debug, PartialEq, Eq)] #[derive(Hash, Debug, PartialEq, Eq)]