From 7dc45745a378c6feb6af524e8bd40e89bab7a822 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Jan 2019 19:51:11 +0300 Subject: save top-level macros in module items --- crates/ra_hir/src/lib.rs | 20 ++++++++++++++++---- crates/ra_hir/src/query_definitions.rs | 10 +--------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4422ac45b..2f9684e33 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -33,7 +33,7 @@ mod ty; use std::ops::Index; -use ra_syntax::{SyntaxNodeRef, SyntaxNode, SyntaxKind}; +use ra_syntax::{SyntaxNodeRef, SyntaxNode, SyntaxKind, SourceFile, AstNode, ast}; use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable}; use crate::{ @@ -169,11 +169,23 @@ pub struct SourceFileItems { } impl SourceFileItems { - fn new(file_id: FileId) -> SourceFileItems { - SourceFileItems { + fn new(file_id: FileId, source_file: SourceFile) -> SourceFileItems { + let mut res = SourceFileItems { file_id, arena: Arena::default(), - } + }; + res.init(source_file); + res + } + + fn init(&mut self, source_file: SourceFile) { + source_file.syntax().descendants().for_each(|it| { + if let Some(module_item) = ast::ModuleItem::cast(it) { + self.alloc(module_item.syntax().owned()); + } else if let Some(macro_call) = ast::MacroCall::cast(it) { + self.alloc(macro_call.syntax().owned()); + } + }); } fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 721bd4195..f66231222 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -48,17 +48,9 @@ pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable Arc { - let mut res = SourceFileItems::new(file_id); let source_file = db.source_file(file_id); let source_file = source_file.borrowed(); - source_file - .syntax() - .descendants() - .filter_map(ast::ModuleItem::cast) - .map(|it| it.syntax().owned()) - .for_each(|it| { - res.alloc(it); - }); + let res = SourceFileItems::new(file_id, source_file); Arc::new(res) } -- cgit v1.2.3