File module source does not depend on syntax

This commit is contained in:
Aleksey Kladov 2018-12-19 01:48:46 +03:00
parent 9664fbcac5
commit 1c5ce7a868
6 changed files with 21 additions and 17 deletions

View File

@ -109,7 +109,8 @@ pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceItemId {
file_id: FileId,
item_id: SourceFileItemId,
/// None for the whole file.
item_id: Option<SourceFileItemId>,
}
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.

View File

@ -271,15 +271,13 @@ pub struct ModuleData {
impl ModuleSource {
// precondition: item_id **must** point to module
fn new(file_id: FileId, item_id: SourceFileItemId) -> ModuleSource {
fn new(file_id: FileId, item_id: Option<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_file(file_id: FileId) -> ModuleSource {
ModuleSource::new(file_id, None)
}
pub(crate) fn new_inline(
@ -290,7 +288,7 @@ pub(crate) fn new_inline(
assert!(!m.has_semi());
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, m.syntax());
ModuleSource::new(file_id, item_id)
ModuleSource::new(file_id, Some(item_id))
}
pub fn file_id(self) -> FileId {

View File

@ -66,7 +66,7 @@ fn create_module_tree<'a>(
let source_root = db.source_root(source_root);
for &file_id in source_root.files.iter() {
let source = ModuleSource::new_file(db, file_id);
let source = ModuleSource::new_file(file_id);
if visited.contains(&source) {
continue; // TODO: use explicit crate_roots here
}
@ -126,7 +126,7 @@ fn build_subtree(
visited,
roots,
Some(link),
ModuleSource::new_file(db, file_id),
ModuleSource::new_file(file_id),
),
})
.collect::<Cancelable<Vec<_>>>()?;

View File

@ -98,7 +98,7 @@ impl NamedImport {
pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
let source_item_id = SourceItemId {
file_id,
item_id: self.file_item_id,
item_id: Some(self.file_item_id),
};
let syntax = db.file_item(source_item_id);
let offset = syntax.borrowed().range().start();
@ -281,7 +281,7 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) ->
module_id,
source_item_id: SourceItemId {
file_id,
item_id: item.id,
item_id: Some(item.id),
},
};
let def_id = def_loc.id(self.db);

View File

@ -38,7 +38,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> {
pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
let mut res = SourceFileItems::new(file_id);
let source_file = db.source_file(file_id);
res.alloc(source_file.syntax().owned());
let source_file = source_file.borrowed();
source_file
.syntax()
@ -52,7 +51,10 @@ pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFi
}
pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode {
db.file_items(source_item_id.file_id)[source_item_id.item_id].clone()
match source_item_id.item_id {
Some(id) => db.file_items(source_item_id.file_id)[id].clone(),
None => db.source_file(source_item_id.file_id).syntax().owned(),
}
}
pub(crate) fn submodules(

View File

@ -20,7 +20,7 @@
/// Locates the module by `FileId`. Picks topmost module in the file.
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> {
let module_source = ModuleSource::new_file(db, file_id);
let module_source = ModuleSource::new_file(file_id);
module_from_source(db, module_source)
}
@ -32,7 +32,7 @@ pub fn module_from_position(
let file = db.source_file(position.file_id);
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),
_ => ModuleSource::new_file(db, position.file_id),
_ => ModuleSource::new_file(position.file_id),
};
module_from_source(db, module_source)
}
@ -50,7 +50,7 @@ pub fn module_from_child_node(
{
ModuleSource::new_inline(db, file_id, m)
} else {
ModuleSource::new_file(db, file_id)
ModuleSource::new_file(file_id)
};
module_from_source(db, module_source)
}
@ -76,7 +76,10 @@ pub fn function_from_source(
let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?);
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, fn_def.syntax());
let source_item_id = SourceItemId { file_id, item_id };
let source_item_id = SourceItemId {
file_id,
item_id: Some(item_id),
};
let def_loc = DefLoc {
kind: DefKind::Function,
source_root_id: module.source_root_id,