1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
use std::iter;
use hir::Semantics;
use ide_db::{helpers::pick_best_token, RootDatabase};
use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T};
use crate::FilePosition;
pub struct ExpandedMacro {
pub name: String,
pub expansion: String,
}
// 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**
// |===
//
// image::https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif[]
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
let sema = Semantics::new(db);
let file = sema.parse(position.file_id);
let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
SyntaxKind::IDENT => 1,
_ => 0,
})?;
let mut expanded = None;
let mut name = None;
for node in tok.ancestors() {
if let Some(item) = ast::Item::cast(node.clone()) {
expanded = sema.expand_attr_macro(&item);
if expanded.is_some() {
// FIXME: add the macro name
// FIXME: make this recursive too
name = Some("?".to_string());
break;
}
}
if let Some(mac) = ast::MacroCall::cast(node) {
name = Some(mac.path()?.segment()?.name_ref()?.to_string());
expanded = expand_macro_recur(&sema, &mac);
break;
}
}
// FIXME:
// macro expansion may lose all white space information
// But we hope someday we can use ra_fmt for that
let expansion = insert_whitespaces(expanded?);
Some(ExpandedMacro { name: name?, expansion })
}
fn expand_macro_recur(
sema: &Semantics<RootDatabase>,
macro_call: &ast::MacroCall,
) -> Option<SyntaxNode> {
let expanded = sema.expand(macro_call)?.clone_for_update();
let children = expanded.descendants().filter_map(ast::MacroCall::cast);
let mut replacements = Vec::new();
for child in children {
if let Some(new_node) = expand_macro_recur(sema, &child) {
// check if the whole original syntax is replaced
if expanded == *child.syntax() {
return Some(new_node);
}
replacements.push((child, new_node));
}
}
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
Some(expanded)
}
// FIXME: It would also be cool to share logic here and in the mbe tests,
// which are pretty unreadable at the moment.
fn insert_whitespaces(syn: SyntaxNode) -> String {
let mut res = String::new();
let mut token_iter = syn
.preorder_with_tokens()
.filter_map(|event| {
if let WalkEvent::Enter(NodeOrToken::Token(token)) = event {
Some(token)
} else {
None
}
})
.peekable();
let mut indent = 0;
let mut last: Option<SyntaxKind> = None;
while let Some(token) = token_iter.next() {
let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
token_iter.peek().map(|it| f(it.kind())).unwrap_or(default)
};
let is_last =
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
match token.kind() {
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
res.push_str(token.text());
res.push(' ');
}
L_CURLY if is_next(|it| it != R_CURLY, true) => {
indent += 1;
if is_last(is_text, false) {
res.push(' ');
}
res.push_str("{\n");
res.extend(iter::repeat(" ").take(2 * indent));
}
R_CURLY if is_last(|it| it != L_CURLY, true) => {
indent = indent.saturating_sub(1);
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));
}
LIFETIME_IDENT if is_next(|it| it == IDENT, true) => {
res.push_str(token.text());
res.push(' ');
}
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()),
}
last = Some(token.kind());
}
return res;
fn is_text(k: SyntaxKind) -> bool {
k.is_keyword() || k.is_literal() || k == IDENT
}
}
#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
use crate::fixture;
fn check(ra_fixture: &str, expect: Expect) {
let (analysis, pos) = fixture::position(ra_fixture);
let expansion = analysis.expand_macro(pos).unwrap().unwrap();
let actual = format!("{}\n{}", expansion.name, expansion.expansion);
expect.assert_eq(&actual);
}
#[test]
fn macro_expand_recursive_expansion() {
check(
r#"
macro_rules! bar {
() => { fn b() {} }
}
macro_rules! foo {
() => { bar!(); }
}
macro_rules! baz {
() => { foo!(); }
}
f$0oo!();
"#,
expect![[r#"
foo
fn b(){}
"#]],
);
}
#[test]
fn macro_expand_multiple_lines() {
check(
r#"
macro_rules! foo {
() => {
fn some_thing() -> u32 {
let a = 0;
a + 10
}
}
}
f$0oo!();
"#,
expect![[r#"
foo
fn some_thing() -> u32 {
let a = 0;
a+10
}"#]],
);
}
#[test]
fn macro_expand_match_ast() {
check(
r#"
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 }
}};
}
fn main() {
mat$0ch_ast! {
match container {
ast::TraitDef(it) => {},
ast::ImplDef(it) => {},
_ => { continue },
}
}
}
"#,
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
}
}
}"#]],
);
}
#[test]
fn macro_expand_match_ast_inside_let_statement() {
check(
r#"
macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
(match ($node:expr) {}) => {{}};
}
fn main() {
let p = f(|it| {
let res = mat$0ch_ast! { match c {}};
Some(res)
})?;
}
"#,
expect![[r#"
match_ast
{}
"#]],
);
}
#[test]
fn macro_expand_inner_macro_fail_to_expand() {
check(
r#"
macro_rules! bar {
(BAD) => {};
}
macro_rules! foo {
() => {bar!()};
}
fn main() {
let res = fo$0o!();
}
"#,
expect![[r#"
foo
"#]],
);
}
#[test]
fn macro_expand_with_dollar_crate() {
check(
r#"
#[macro_export]
macro_rules! bar {
() => {0};
}
macro_rules! foo {
() => {$crate::bar!()};
}
fn main() {
let res = fo$0o!();
}
"#,
expect![[r#"
foo
0 "#]],
);
}
}
|