2019-11-17 12:47:50 -06:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
|
|
|
use crate::{db::RootDatabase, FilePosition};
|
2019-11-17 13:35:46 -06:00
|
|
|
use hir::db::AstDatabase;
|
2019-11-17 12:47:50 -06:00
|
|
|
use ra_db::SourceDatabase;
|
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
|
|
|
use ra_syntax::{
|
|
|
|
algo::{find_node_at_offset, replace_descendants},
|
|
|
|
ast::{self},
|
|
|
|
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent,
|
|
|
|
};
|
|
|
|
|
2019-11-19 08:56:48 -06:00
|
|
|
pub struct ExpandedMacro {
|
|
|
|
pub name: String,
|
|
|
|
pub expansion: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
|
2019-11-19 08:56:28 -06:00
|
|
|
let parse = db.parse(position.file_id);
|
|
|
|
let file = parse.tree();
|
|
|
|
let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)?;
|
|
|
|
let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?;
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
let source = hir::Source::new(position.file_id.into(), mac.syntax());
|
|
|
|
let expanded = expand_macro_recur(db, source, &mac)?;
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
// FIXME:
|
|
|
|
// macro expansion may lose all white space information
|
|
|
|
// But we hope someday we can use ra_fmt for that
|
2019-11-19 08:56:48 -06:00
|
|
|
let expansion = insert_whitespaces(expanded);
|
|
|
|
Some(ExpandedMacro { name: name_ref.text().to_string(), expansion })
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_macro_recur(
|
|
|
|
db: &RootDatabase,
|
|
|
|
source: hir::Source<&SyntaxNode>,
|
|
|
|
macro_call: &ast::MacroCall,
|
|
|
|
) -> Option<SyntaxNode> {
|
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, source, None);
|
|
|
|
let expansion = analyzer.expand(db, ¯o_call)?;
|
2019-11-19 08:56:28 -06:00
|
|
|
let macro_file_id = expansion.file_id();
|
|
|
|
let expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?;
|
2019-11-17 12:47:50 -06:00
|
|
|
|
|
|
|
let children = expanded.descendants().filter_map(ast::MacroCall::cast);
|
|
|
|
let mut replaces = FxHashMap::default();
|
|
|
|
|
|
|
|
for child in children.into_iter() {
|
2019-11-19 08:56:28 -06:00
|
|
|
let source = hir::Source::new(macro_file_id, source.ast);
|
2019-11-17 12:47:50 -06:00
|
|
|
let new_node = expand_macro_recur(db, source, &child)?;
|
|
|
|
|
|
|
|
replaces.insert(child.syntax().clone().into(), new_node.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(replace_descendants(&expanded, &replaces))
|
|
|
|
}
|
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
fn insert_whitespaces(syn: SyntaxNode) -> String {
|
|
|
|
let mut res = String::new();
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
let mut token_iter = syn
|
|
|
|
.preorder_with_tokens()
|
|
|
|
.filter_map(|event| {
|
|
|
|
if let WalkEvent::Enter(NodeOrToken::Token(token)) = event {
|
|
|
|
Some(token)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.peekable();
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
while let Some(token) = token_iter.next() {
|
|
|
|
res += &token.text().to_string();
|
|
|
|
if token.kind().is_keyword()
|
|
|
|
|| token.kind().is_literal()
|
|
|
|
|| token.kind() == SyntaxKind::IDENT
|
|
|
|
{
|
|
|
|
if !token_iter.peek().map(|it| it.kind().is_punct()).unwrap_or(false) {
|
|
|
|
res += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
res
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::mock_analysis::analysis_and_position;
|
|
|
|
|
|
|
|
fn check_expand_macro(fixture: &str, expected: (&str, &str)) {
|
|
|
|
let (analysis, pos) = analysis_and_position(fixture);
|
|
|
|
|
|
|
|
let result = analysis.expand_macro(pos).unwrap().unwrap();
|
2019-11-19 08:56:48 -06:00
|
|
|
assert_eq!(result.name, expected.0.to_string());
|
|
|
|
assert_eq!(result.expansion, expected.1.to_string());
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_recursive_expansion() {
|
|
|
|
check_expand_macro(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! bar {
|
|
|
|
() => { fn b() {} }
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => { bar!(); }
|
|
|
|
}
|
|
|
|
macro_rules! baz {
|
|
|
|
() => { foo!(); }
|
|
|
|
}
|
|
|
|
f<|>oo!();
|
|
|
|
"#,
|
|
|
|
("foo", "fn b(){}"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|