expand to syntax node

This commit is contained in:
Aleksey Kladov 2019-05-14 01:42:59 +03:00
parent 101b3abfd7
commit 16c7405262
6 changed files with 33 additions and 26 deletions

View file

@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use ra_syntax::{SyntaxNode, TreeArc, SourceFile, SmolStr, ast}; use ra_syntax::{SyntaxNode, TreeArc, SmolStr, ast};
use ra_db::{SourceDatabase, salsa}; use ra_db::{SourceDatabase, salsa};
use crate::{ use crate::{
@ -54,8 +54,8 @@ pub trait DefDatabase: SourceDatabase {
#[salsa::invoke(crate::ids::macro_expand_query)] #[salsa::invoke(crate::ids::macro_expand_query)]
fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result<Arc<tt::Subtree>, String>; fn macro_expand(&self, macro_call: ids::MacroCallId) -> Result<Arc<tt::Subtree>, String>;
#[salsa::invoke(crate::ids::HirFileId::hir_parse_query)] #[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)]
fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>;
#[salsa::invoke(crate::adt::StructData::struct_data_query)] #[salsa::invoke(crate::adt::StructData::struct_data_query)]
fn struct_data(&self, s: Struct) -> Arc<StructData>; fn struct_data(&self, s: Struct) -> Arc<StructData>;

View file

@ -1,6 +1,6 @@
use std::{fmt, any::Any}; use std::{fmt, any::Any};
use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode, AstNode}; use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{HirFileId, HirDatabase, Name}; use crate::{HirFileId, HirDatabase, Name};
@ -29,8 +29,8 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
impl dyn Diagnostic { impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(self.file()); let node = db.parse_or_expand(self.file()).unwrap();
self.syntax_node_ptr().to_node(source_file.syntax()).to_owned() self.syntax_node_ptr().to_node(&*node).to_owned()
} }
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
self.as_any().downcast_ref() self.as_any().downcast_ref()

View file

@ -4,7 +4,7 @@ use std::{
}; };
use ra_db::{FileId, salsa}; use ra_db::{FileId, salsa};
use ra_syntax::{TreeArc, SourceFile, AstNode, ast}; use ra_syntax::{TreeArc, AstNode, ast, SyntaxNode};
use mbe::MacroRules; use mbe::MacroRules;
use crate::{ use crate::{
@ -56,17 +56,17 @@ impl HirFileId {
} }
} }
pub(crate) fn hir_parse_query( pub(crate) fn parse_or_expand_query(
db: &impl DefDatabase, db: &impl DefDatabase,
file_id: HirFileId, file_id: HirFileId,
) -> TreeArc<SourceFile> { ) -> Option<TreeArc<SyntaxNode>> {
match file_id.0 { match file_id.0 {
HirFileIdRepr::File(file_id) => db.parse(file_id), HirFileIdRepr::File(file_id) => Some(db.parse(file_id).syntax().to_owned()),
HirFileIdRepr::Macro(macro_file) => { HirFileIdRepr::Macro(macro_file) => {
let macro_call_id = macro_file.macro_call_id; let macro_call_id = macro_file.macro_call_id;
let tt = match db.macro_expand(macro_call_id) { let tt = db
Ok(it) => it, .macro_expand(macro_call_id)
Err(err) => { .map_err(|err| {
// Note: // Note:
// The final goal we would like to make all parse_macro success, // The final goal we would like to make all parse_macro success,
// such that the following log will not call anyway. // such that the following log will not call anyway.
@ -75,12 +75,12 @@ impl HirFileId {
err, err,
macro_call_id.debug_dump(db) macro_call_id.debug_dump(db)
); );
// returning an empty string looks fishy... })
return SourceFile::parse(""); .ok()?;
}
};
match macro_file.macro_file_kind { match macro_file.macro_file_kind {
MacroFileKind::Items => mbe::token_tree_to_ast_item_list(&tt), MacroFileKind::Items => {
Some(mbe::token_tree_to_ast_item_list(&tt).syntax().to_owned())
}
} }
} }
} }

View file

@ -75,8 +75,11 @@ impl RawItems {
source_ast_id_map: db.ast_id_map(file_id.into()), source_ast_id_map: db.ast_id_map(file_id.into()),
source_map: ImportSourceMap::default(), source_map: ImportSourceMap::default(),
}; };
let source_file = db.hir_parse(file_id); if let Some(node) = db.parse_or_expand(file_id) {
collector.process_module(None, &*source_file); if let Some(source_file) = ast::SourceFile::cast(&node) {
collector.process_module(None, &*source_file);
}
}
(Arc::new(collector.raw_items), Arc::new(collector.source_map)) (Arc::new(collector.raw_items), Arc::new(collector.source_map))
} }

View file

@ -81,15 +81,19 @@ pub struct ErasedFileAstId(RawId);
impl_arena_id!(ErasedFileAstId); impl_arena_id!(ErasedFileAstId);
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back. /// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq, Default)]
pub struct AstIdMap { pub struct AstIdMap {
arena: Arena<ErasedFileAstId, SyntaxNodePtr>, arena: Arena<ErasedFileAstId, SyntaxNodePtr>,
} }
impl AstIdMap { impl AstIdMap {
pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> { pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let source_file = db.hir_parse(file_id); let map = if let Some(node) = db.parse_or_expand(file_id) {
Arc::new(AstIdMap::from_source(source_file.syntax())) AstIdMap::from_source(&*node)
} else {
AstIdMap::default()
};
Arc::new(map)
} }
pub(crate) fn file_item_query( pub(crate) fn file_item_query(
@ -97,8 +101,8 @@ impl AstIdMap {
file_id: HirFileId, file_id: HirFileId,
ast_id: ErasedFileAstId, ast_id: ErasedFileAstId,
) -> TreeArc<SyntaxNode> { ) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(file_id); let node = db.parse_or_expand(file_id).unwrap();
db.ast_id_map(file_id).arena[ast_id].to_node(source_file.syntax()).to_owned() db.ast_id_map(file_id).arena[ast_id].to_node(&*node).to_owned()
} }
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> { pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {

View file

@ -222,7 +222,7 @@ impl RootDatabase {
self.query(ra_db::ParseQuery).sweep(sweep); self.query(ra_db::ParseQuery).sweep(sweep);
self.query(hir::db::HirParseQuery).sweep(sweep); self.query(hir::db::ParseOrExpandQuery).sweep(sweep);
self.query(hir::db::AstIdMapQuery).sweep(sweep); self.query(hir::db::AstIdMapQuery).sweep(sweep);
self.query(hir::db::AstIdToNodeQuery).sweep(sweep); self.query(hir::db::AstIdToNodeQuery).sweep(sweep);