Add macro_expansion_info in hir_expand

This commit is contained in:
Edwin Cheng 2019-11-03 22:46:12 +08:00
parent 9fd546bec2
commit 159da285e9
5 changed files with 212 additions and 47 deletions

View file

@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher};
use ra_db::{salsa, CrateId, FileId};
use ra_syntax::{
ast::{self, AstNode},
SyntaxNode,
SyntaxNode, TextRange,
};
use crate::ast_id_map::FileAstId;
@ -112,6 +112,39 @@ impl MacroCallId {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// ExpansionInfo mainly describle how to map text range between src and expaned macro
pub struct ExpansionInfo {
pub arg_map: Vec<(TextRange, TextRange)>,
pub def_map: Vec<(TextRange, TextRange)>,
}
impl ExpansionInfo {
pub fn find_range(
&self,
from: TextRange,
(arg_file_id, def_file_id): (HirFileId, HirFileId),
) -> Option<(HirFileId, TextRange)> {
for (src, dest) in &self.arg_map {
dbg!((src, *dest, "arg_map"));
if src.is_subrange(&from) {
dbg!((arg_file_id, *dest));
return Some((arg_file_id, *dest));
}
}
for (src, dest) in &self.def_map {
dbg!((src, *dest, "def_map"));
if src.is_subrange(&from) {
dbg!((arg_file_id, *dest));
return Some((def_file_id, *dest));
}
}
None
}
}
/// `AstId` points to an AST node in any file.
///
/// It is stable across reparses, and can be used as salsa key/value.