]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/expand_macro.rs
Merge #10870
[rust.git] / crates / ide / src / expand_macro.rs
1 use std::iter;
2
3 use hir::Semantics;
4 use ide_db::{helpers::pick_best_token, RootDatabase};
5 use itertools::Itertools;
6 use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, T};
7
8 use crate::FilePosition;
9
10 pub struct ExpandedMacro {
11     pub name: String,
12     pub expansion: String,
13 }
14
15 // Feature: Expand Macro Recursively
16 //
17 // Shows the full macro expansion of the macro at current cursor.
18 //
19 // |===
20 // | Editor  | Action Name
21 //
22 // | VS Code | **Rust Analyzer: Expand macro recursively**
23 // |===
24 //
25 // image::https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif[]
26 pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
27     let sema = Semantics::new(db);
28     let file = sema.parse(position.file_id);
29
30     let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
31         SyntaxKind::IDENT => 1,
32         _ => 0,
33     })?;
34
35     // due to how Rust Analyzer works internally, we need to special case derive attributes,
36     // otherwise they might not get found, e.g. here with the cursor at $0 `#[attr]` would expand:
37     // ```
38     // #[attr]
39     // #[derive($0Foo)]
40     // struct Bar;
41     // ```
42
43     let derive = sema.descend_into_macros(tok.clone()).iter().find_map(|descended| {
44         let attr = descended.ancestors().find_map(ast::Attr::cast)?;
45         let (path, tt) = attr.as_simple_call()?;
46         if path == "derive" {
47             let mut tt = tt.syntax().children_with_tokens().skip(1).join("");
48             tt.pop();
49             let expansions = sema.expand_derive_macro(&attr)?;
50             Some(ExpandedMacro {
51                 name: tt,
52                 expansion: expansions.into_iter().map(insert_whitespaces).join(""),
53             })
54         } else {
55             None
56         }
57     });
58
59     if derive.is_some() {
60         return derive;
61     }
62
63     // FIXME: Intermix attribute and bang! expansions
64     // currently we only recursively expand one of the two types
65     let mut expanded = None;
66     let mut name = None;
67     for node in tok.ancestors() {
68         if let Some(item) = ast::Item::cast(node.clone()) {
69             if let Some(def) = sema.resolve_attr_macro_call(&item) {
70                 name = def.name(db).map(|name| name.to_string());
71                 expanded = expand_attr_macro_recur(&sema, &item);
72                 break;
73             }
74         }
75         if let Some(mac) = ast::MacroCall::cast(node) {
76             name = Some(mac.path()?.segment()?.name_ref()?.to_string());
77             expanded = expand_macro_recur(&sema, &mac);
78             break;
79         }
80     }
81
82     // FIXME:
83     // macro expansion may lose all white space information
84     // But we hope someday we can use ra_fmt for that
85     let expansion = insert_whitespaces(expanded?);
86     Some(ExpandedMacro { name: name.unwrap_or_else(|| "???".to_owned()), expansion })
87 }
88
89 fn expand_macro_recur(
90     sema: &Semantics<RootDatabase>,
91     macro_call: &ast::MacroCall,
92 ) -> Option<SyntaxNode> {
93     let expanded = sema.expand(macro_call)?.clone_for_update();
94     expand(sema, expanded, ast::MacroCall::cast, expand_macro_recur)
95 }
96
97 fn expand_attr_macro_recur(sema: &Semantics<RootDatabase>, item: &ast::Item) -> Option<SyntaxNode> {
98     let expanded = sema.expand_attr_macro(item)?.clone_for_update();
99     expand(sema, expanded, ast::Item::cast, expand_attr_macro_recur)
100 }
101
102 fn expand<T: AstNode>(
103     sema: &Semantics<RootDatabase>,
104     expanded: SyntaxNode,
105     f: impl FnMut(SyntaxNode) -> Option<T>,
106     exp: impl Fn(&Semantics<RootDatabase>, &T) -> Option<SyntaxNode>,
107 ) -> Option<SyntaxNode> {
108     let children = expanded.descendants().filter_map(f);
109     let mut replacements = Vec::new();
110
111     for child in children {
112         if let Some(new_node) = exp(sema, &child) {
113             // check if the whole original syntax is replaced
114             if expanded == *child.syntax() {
115                 return Some(new_node);
116             }
117             replacements.push((child, new_node));
118         }
119     }
120
121     replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
122     Some(expanded)
123 }
124
125 // FIXME: It would also be cool to share logic here and in the mbe tests,
126 // which are pretty unreadable at the moment.
127 fn insert_whitespaces(syn: SyntaxNode) -> String {
128     use SyntaxKind::*;
129     let mut res = String::new();
130
131     let mut indent = 0;
132     let mut last: Option<SyntaxKind> = None;
133
134     for event in syn.preorder_with_tokens() {
135         let token = match event {
136             WalkEvent::Enter(NodeOrToken::Token(token)) => token,
137             WalkEvent::Leave(NodeOrToken::Node(node))
138                 if matches!(node.kind(), ATTR | MATCH_ARM | STRUCT | ENUM | UNION | FN | IMPL) =>
139             {
140                 res.push('\n');
141                 res.extend(iter::repeat(" ").take(2 * indent));
142                 continue;
143             }
144             _ => continue,
145         };
146         let is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
147             token.next_token().map(|it| f(it.kind())).unwrap_or(default)
148         };
149         let is_last =
150             |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
151
152         match token.kind() {
153             k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
154                 res.push_str(token.text());
155                 res.push(' ');
156             }
157             L_CURLY if is_next(|it| it != R_CURLY, true) => {
158                 indent += 1;
159                 if is_last(is_text, false) {
160                     res.push(' ');
161                 }
162                 res.push_str("{\n");
163                 res.extend(iter::repeat(" ").take(2 * indent));
164             }
165             R_CURLY if is_last(|it| it != L_CURLY, true) => {
166                 indent = indent.saturating_sub(1);
167                 res.push('\n');
168                 res.extend(iter::repeat(" ").take(2 * indent));
169                 res.push_str("}");
170             }
171             R_CURLY => {
172                 res.push_str("}\n");
173                 res.extend(iter::repeat(" ").take(2 * indent));
174             }
175             LIFETIME_IDENT if is_next(|it| it == IDENT || it == MUT_KW, true) => {
176                 res.push_str(token.text());
177                 res.push(' ');
178             }
179             AS_KW => {
180                 res.push_str(token.text());
181                 res.push(' ');
182             }
183             T![;] => {
184                 res.push_str(";\n");
185                 res.extend(iter::repeat(" ").take(2 * indent));
186             }
187             T![->] => res.push_str(" -> "),
188             T![=] => res.push_str(" = "),
189             T![=>] => res.push_str(" => "),
190             _ => res.push_str(token.text()),
191         }
192
193         last = Some(token.kind());
194     }
195
196     return res;
197
198     fn is_text(k: SyntaxKind) -> bool {
199         k.is_keyword() || k.is_literal() || k == IDENT
200     }
201 }
202
203 #[cfg(test)]
204 mod tests {
205     use expect_test::{expect, Expect};
206
207     use crate::fixture;
208
209     #[track_caller]
210     fn check(ra_fixture: &str, expect: Expect) {
211         let (analysis, pos) = fixture::position(ra_fixture);
212         let expansion = analysis.expand_macro(pos).unwrap().unwrap();
213         let actual = format!("{}\n{}", expansion.name, expansion.expansion);
214         expect.assert_eq(&actual);
215     }
216
217     #[test]
218     fn macro_expand_as_keyword() {
219         check(
220             r#"
221 macro_rules! bar {
222     ($i:tt) => { $i as _ }
223 }
224 fn main() {
225     let x: u64 = ba$0r!(5i64);
226 }
227 "#,
228             expect![[r#"
229                 bar
230                 5i64 as _"#]],
231         );
232     }
233
234     #[test]
235     fn macro_expand_recursive_expansion() {
236         check(
237             r#"
238 macro_rules! bar {
239     () => { fn  b() {} }
240 }
241 macro_rules! foo {
242     () => { bar!(); }
243 }
244 macro_rules! baz {
245     () => { foo!(); }
246 }
247 f$0oo!();
248 "#,
249             expect![[r#"
250                 foo
251                 fn b(){}
252
253             "#]],
254         );
255     }
256
257     #[test]
258     fn macro_expand_multiple_lines() {
259         check(
260             r#"
261 macro_rules! foo {
262     () => {
263         fn some_thing() -> u32 {
264             let a = 0;
265             a + 10
266         }
267     }
268 }
269 f$0oo!();
270         "#,
271             expect![[r#"
272                 foo
273                 fn some_thing() -> u32 {
274                   let a = 0;
275                   a+10
276                 }
277             "#]],
278         );
279     }
280
281     #[test]
282     fn macro_expand_match_ast() {
283         check(
284             r#"
285 macro_rules! match_ast {
286     (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
287     (match ($node:expr) {
288         $( ast::$ast:ident($it:ident) => $res:block, )*
289         _ => $catch_all:expr $(,)?
290     }) => {{
291         $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
292         { $catch_all }
293     }};
294 }
295
296 fn main() {
297     mat$0ch_ast! {
298         match container {
299             ast::TraitDef(it) => {},
300             ast::ImplDef(it) => {},
301             _ => { continue },
302         }
303     }
304 }
305 "#,
306             expect![[r#"
307        match_ast
308        {
309          if let Some(it) = ast::TraitDef::cast(container.clone()){}
310          else if let Some(it) = ast::ImplDef::cast(container.clone()){}
311          else {
312            {
313              continue
314            }
315          }
316        }"#]],
317         );
318     }
319
320     #[test]
321     fn macro_expand_match_ast_inside_let_statement() {
322         check(
323             r#"
324 macro_rules! match_ast {
325     (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
326     (match ($node:expr) {}) => {{}};
327 }
328
329 fn main() {
330     let p = f(|it| {
331         let res = mat$0ch_ast! { match c {}};
332         Some(res)
333     })?;
334 }
335 "#,
336             expect![[r#"
337                 match_ast
338                 {}
339             "#]],
340         );
341     }
342
343     #[test]
344     fn macro_expand_inner_macro_fail_to_expand() {
345         check(
346             r#"
347 macro_rules! bar {
348     (BAD) => {};
349 }
350 macro_rules! foo {
351     () => {bar!()};
352 }
353
354 fn main() {
355     let res = fo$0o!();
356 }
357 "#,
358             expect![[r#"
359                 foo
360             "#]],
361         );
362     }
363
364     #[test]
365     fn macro_expand_with_dollar_crate() {
366         check(
367             r#"
368 #[macro_export]
369 macro_rules! bar {
370     () => {0};
371 }
372 macro_rules! foo {
373     () => {$crate::bar!()};
374 }
375
376 fn main() {
377     let res = fo$0o!();
378 }
379 "#,
380             expect![[r#"
381                 foo
382                 0 "#]],
383         );
384     }
385
386     #[test]
387     fn macro_expand_derive() {
388         check(
389             r#"
390 //- proc_macros: identity
391 //- minicore: clone, derive
392
393 #[proc_macros::identity]
394 #[derive(C$0lone)]
395 struct Foo {}
396 "#,
397             expect![[r#"
398                 Clone
399                 impl< >core::clone::Clone for Foo< >{}
400
401             "#]],
402         );
403     }
404
405     #[test]
406     fn macro_expand_derive2() {
407         check(
408             r#"
409 //- minicore: copy, clone, derive
410
411 #[derive(Cop$0y)]
412 #[derive(Clone)]
413 struct Foo {}
414 "#,
415             expect![[r#"
416                 Copy
417                 impl< >core::marker::Copy for Foo< >{}
418
419             "#]],
420         );
421     }
422
423     #[test]
424     fn macro_expand_derive_multi() {
425         check(
426             r#"
427 //- minicore: copy, clone, derive
428
429 #[derive(Cop$0y, Clone)]
430 struct Foo {}
431 "#,
432             expect![[r#"
433                 Copy, Clone
434                 impl< >core::marker::Copy for Foo< >{}
435
436                 impl< >core::clone::Clone for Foo< >{}
437
438             "#]],
439         );
440     }
441 }