save top-level macros in module items

This commit is contained in:
Aleksey Kladov 2019-01-01 19:51:11 +03:00
parent e5b2fd6771
commit 7dc45745a3
3 changed files with 18 additions and 14 deletions

View file

@ -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 {

View file

@ -48,17 +48,9 @@ pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<
}
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);
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)
}

View file

@ -51,7 +51,7 @@ use ra_text_edit::AtomTextEdit;
use crate::yellow::GreenNode;
/// `SourceFileNode` represents a parse tree for a single Rust file.
pub use crate::ast::SourceFileNode;
pub use crate::ast::{SourceFile, SourceFileNode};
impl SourceFileNode {
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode {