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