2021-04-08 09:20:14 -05:00
|
|
|
use std::iter;
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::Semantics;
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::RootDatabase;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-06-07 09:05:36 -05:00
|
|
|
ast, match_ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T,
|
2019-11-17 12:47:50 -06:00
|
|
|
};
|
2020-02-06 05:52:32 -06:00
|
|
|
|
|
|
|
use crate::FilePosition;
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:48 -06:00
|
|
|
pub struct ExpandedMacro {
|
|
|
|
pub name: String,
|
|
|
|
pub expansion: String,
|
|
|
|
}
|
|
|
|
|
2020-05-31 03:14:36 -05:00
|
|
|
// Feature: Expand Macro Recursively
|
|
|
|
//
|
|
|
|
// Shows the full macro expansion of the macro at current cursor.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
|
|
|
// | VS Code | **Rust Analyzer: Expand macro recursively**
|
|
|
|
// |===
|
2021-03-30 18:08:10 -05:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif[]
|
2019-11-19 08:56:48 -06:00
|
|
|
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(position.file_id);
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2021-06-07 09:05:36 -05:00
|
|
|
let tok = file.syntax().token_at_offset(position.offset).left_biased()?;
|
|
|
|
let mut expanded = None;
|
|
|
|
let mut name = None;
|
|
|
|
for node in tok.ancestors() {
|
|
|
|
match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::MacroCall(mac) => {
|
|
|
|
name = Some(mac.path()?.segment()?.name_ref()?.to_string());
|
|
|
|
expanded = expand_macro_recur(&sema, &mac);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
ast::Item(item) => {
|
|
|
|
// FIXME: add the macro name
|
|
|
|
// FIXME: make this recursive too
|
|
|
|
name = Some("?".to_string());
|
|
|
|
expanded = sema.expand_attr_macro(&item);
|
|
|
|
if expanded.is_some() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
2021-06-07 09:05:36 -05:00
|
|
|
let expansion = insert_whitespaces(expanded?);
|
|
|
|
Some(ExpandedMacro { name: name?, expansion })
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_macro_recur(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
macro_call: &ast::MacroCall,
|
2019-11-17 12:47:50 -06:00
|
|
|
) -> Option<SyntaxNode> {
|
2021-04-19 12:43:26 -05:00
|
|
|
let expanded = sema.expand(macro_call)?.clone_for_update();
|
2019-11-17 12:47:50 -06:00
|
|
|
|
|
|
|
let children = expanded.descendants().filter_map(ast::MacroCall::cast);
|
2021-04-19 12:43:26 -05:00
|
|
|
let mut replacements = Vec::new();
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2021-04-19 12:43:26 -05:00
|
|
|
for child in children {
|
2020-02-18 11:35:10 -06:00
|
|
|
if let Some(new_node) = expand_macro_recur(sema, &child) {
|
2021-04-19 12:43:26 -05:00
|
|
|
// check if the whole original syntax is replaced
|
2019-11-22 20:33:14 -06:00
|
|
|
if expanded == *child.syntax() {
|
2021-04-19 12:43:26 -05:00
|
|
|
return Some(new_node);
|
2019-11-22 20:33:14 -06:00
|
|
|
}
|
2021-04-19 12:43:26 -05:00
|
|
|
replacements.push((child, new_node));
|
2019-11-21 22:04:20 -06:00
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
2021-04-19 12:43:26 -05:00
|
|
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
|
|
|
Some(expanded)
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:12:48 -06:00
|
|
|
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
|
|
|
// which are pretty unreadable at the moment.
|
2019-11-19 08:56:28 -06:00
|
|
|
fn insert_whitespaces(syn: SyntaxNode) -> String {
|
2019-11-19 10:12:48 -06:00
|
|
|
let mut res = String::new();
|
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 10:12:48 -06:00
|
|
|
let mut indent = 0;
|
|
|
|
let mut last: Option<SyntaxKind> = None;
|
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
while let Some(token) = token_iter.next() {
|
2019-11-19 10:12:48 -06:00
|
|
|
let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
|
|
|
|
token_iter.peek().map(|it| f(it.kind())).unwrap_or(default)
|
|
|
|
};
|
2019-12-20 14:14:30 -06:00
|
|
|
let is_last =
|
|
|
|
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
|
2019-11-19 10:12:48 -06:00
|
|
|
|
2021-04-08 09:20:14 -05:00
|
|
|
match token.kind() {
|
|
|
|
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
|
|
|
|
res.push_str(token.text());
|
|
|
|
res.push(' ');
|
|
|
|
}
|
2019-11-19 10:12:48 -06:00
|
|
|
L_CURLY if is_next(|it| it != R_CURLY, true) => {
|
|
|
|
indent += 1;
|
2021-04-08 09:20:14 -05:00
|
|
|
if is_last(is_text, false) {
|
|
|
|
res.push(' ');
|
|
|
|
}
|
|
|
|
res.push_str("{\n");
|
|
|
|
res.extend(iter::repeat(" ").take(2 * indent));
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
|
|
|
R_CURLY if is_last(|it| it != L_CURLY, true) => {
|
2019-12-20 14:14:30 -06:00
|
|
|
indent = indent.saturating_sub(1);
|
2021-04-08 09:20:14 -05:00
|
|
|
res.push('\n');
|
|
|
|
res.extend(iter::repeat(" ").take(2 * indent));
|
|
|
|
res.push_str("}");
|
|
|
|
}
|
|
|
|
R_CURLY => {
|
|
|
|
res.push_str("}\n");
|
|
|
|
res.extend(iter::repeat(" ").take(2 * indent));
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
2021-04-06 22:22:44 -05:00
|
|
|
LIFETIME_IDENT if is_next(|it| it == IDENT, true) => {
|
2021-04-08 09:20:14 -05:00
|
|
|
res.push_str(token.text());
|
|
|
|
res.push(' ');
|
2021-04-06 22:22:44 -05:00
|
|
|
}
|
2021-04-08 09:20:14 -05:00
|
|
|
T![;] => {
|
|
|
|
res.push_str(";\n");
|
|
|
|
res.extend(iter::repeat(" ").take(2 * indent));
|
|
|
|
}
|
|
|
|
T![->] => res.push_str(" -> "),
|
|
|
|
T![=] => res.push_str(" = "),
|
|
|
|
T![=>] => res.push_str(" => "),
|
|
|
|
_ => res.push_str(token.text()),
|
|
|
|
}
|
2019-11-19 10:12:48 -06:00
|
|
|
|
|
|
|
last = Some(token.kind());
|
2019-11-19 08:56:28 -06:00
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-21 12:35:49 -06:00
|
|
|
return res;
|
|
|
|
|
|
|
|
fn is_text(k: SyntaxKind) -> bool {
|
|
|
|
k.is_keyword() || k.is_literal() || k == IDENT
|
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 06:19:31 -05:00
|
|
|
use expect_test::{expect, Expect};
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::fixture;
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, pos) = fixture::position(ra_fixture);
|
2020-07-01 10:52:22 -05:00
|
|
|
let expansion = analysis.expand_macro(pos).unwrap().unwrap();
|
|
|
|
let actual = format!("{}\n{}", expansion.name, expansion.expansion);
|
|
|
|
expect.assert_eq(&actual);
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_recursive_expansion() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-17 12:47:50 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! bar {
|
|
|
|
() => { fn b() {} }
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => { bar!(); }
|
|
|
|
}
|
|
|
|
macro_rules! baz {
|
|
|
|
() => { foo!(); }
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
f$0oo!();
|
2020-07-01 10:52:22 -05:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
foo
|
|
|
|
fn b(){}
|
|
|
|
"#]],
|
2019-11-17 12:47:50 -06:00
|
|
|
);
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_multiple_lines() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-19 10:12:48 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! foo {
|
|
|
|
() => {
|
|
|
|
fn some_thing() -> u32 {
|
|
|
|
let a = 0;
|
|
|
|
a + 10
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
f$0oo!();
|
2019-11-19 10:12:48 -06:00
|
|
|
"#,
|
2020-07-01 10:52:22 -05:00
|
|
|
expect![[r#"
|
|
|
|
foo
|
|
|
|
fn some_thing() -> u32 {
|
|
|
|
let a = 0;
|
|
|
|
a+10
|
|
|
|
}"#]],
|
2019-11-19 10:12:48 -06:00
|
|
|
);
|
2019-11-21 12:35:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_match_ast() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-21 12:35:30 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! match_ast {
|
|
|
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
|
|
|
(match ($node:expr) {
|
|
|
|
$( ast::$ast:ident($it:ident) => $res:block, )*
|
|
|
|
_ => $catch_all:expr $(,)?
|
|
|
|
}) => {{
|
|
|
|
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
|
|
|
{ $catch_all }
|
|
|
|
}};
|
|
|
|
}
|
2020-02-06 05:52:32 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
mat$0ch_ast! {
|
2020-07-01 10:52:22 -05:00
|
|
|
match container {
|
|
|
|
ast::TraitDef(it) => {},
|
|
|
|
ast::ImplDef(it) => {},
|
|
|
|
_ => { continue },
|
2020-02-06 05:52:32 -06:00
|
|
|
}
|
2019-11-21 12:35:30 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-01 10:52:22 -05:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
match_ast
|
|
|
|
{
|
|
|
|
if let Some(it) = ast::TraitDef::cast(container.clone()){}
|
|
|
|
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
|
|
|
|
else {
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#]],
|
|
|
|
);
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
2019-11-21 22:04:20 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_match_ast_inside_let_statement() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-21 22:04:20 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! match_ast {
|
|
|
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
|
|
|
(match ($node:expr) {}) => {{}};
|
|
|
|
}
|
2019-11-21 22:04:20 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
|
|
|
let p = f(|it| {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = mat$0ch_ast! { match c {}};
|
2020-07-01 10:52:22 -05:00
|
|
|
Some(res)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
match_ast
|
|
|
|
{}
|
|
|
|
"#]],
|
2019-11-21 22:04:20 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-22 20:33:14 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_inner_macro_fail_to_expand() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-22 20:33:14 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! bar {
|
|
|
|
(BAD) => {};
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {bar!()};
|
|
|
|
}
|
2019-11-22 20:33:14 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = fo$0o!();
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
foo
|
|
|
|
"#]],
|
2019-11-22 20:33:14 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-26 01:05:53 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_with_dollar_crate() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-26 01:05:53 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {0};
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {$crate::bar!()};
|
|
|
|
}
|
2019-11-26 01:05:53 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = fo$0o!();
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
foo
|
|
|
|
0 "#]],
|
2019-11-26 01:05:53 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|