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