From 1c5ce7a8682c9e14ce726388b88897644721b19b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Dec 2018 01:48:46 +0300 Subject: File module source does not depend on syntax --- crates/ra_hir/src/lib.rs | 3 ++- crates/ra_hir/src/module.rs | 10 ++++------ crates/ra_hir/src/module/imp.rs | 4 ++-- crates/ra_hir/src/module/nameres.rs | 4 ++-- crates/ra_hir/src/query_definitions.rs | 6 ++++-- crates/ra_hir/src/source_binder.rs | 11 +++++++---- 6 files changed, 21 insertions(+), 17 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 80354a49d..5941a9ea3 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -109,7 +109,8 @@ pub(crate) type SourceFileItemId = Id; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct SourceItemId { file_id: FileId, - item_id: SourceFileItemId, + /// None for the whole file. + item_id: Option, } /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index c6bb76d56..d5866f6ef 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs @@ -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) -> 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 @@ impl ModuleSource { 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 { diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 9f144e139..4a19842c4 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs @@ -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::>>()?; diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 9afeade9e..5540b827f 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -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 @@ where 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); diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 37c4f9e4f..efaeb1525 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -38,7 +38,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { 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 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( diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0c16ccc24..ce2a0f2e8 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -20,7 +20,7 @@ use crate::{ /// Locates the module by `FileId`. Picks topmost module in the file. pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable> { - 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::(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, -- cgit v1.2.3