Skip to content

Commit 662f02f

Browse files
committed
Make toggle_macro_delimiters work only for macro_calls
1 parent c1245e1 commit 662f02f

File tree

2 files changed

+112
-62
lines changed

2 files changed

+112
-62
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/toggle_macro_delimiter.rs

+91-62
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ use crate::{AssistContext, Assists};
1111
// Change macro delimiters in the order of `( -> { -> [ -> (`.
1212
//
1313
// ```
14-
// macro_rules! sth ();
14+
// macro_rules! sth {
15+
// () => {};
16+
// }
17+
//
1518
// sth! $0( );
1619
// ```
1720
// ->
1821
// ```
19-
// macro_rules! sth! ();
22+
// macro_rules! sth {
23+
// () => {};
24+
// }
25+
//
2026
// sth! { }
2127
// ```
2228
pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
@@ -30,26 +36,13 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
3036
RCur,
3137
}
3238

33-
enum MakroTypes {
34-
MacroRules(ast::MacroRules),
35-
MacroCall(ast::MacroCall),
36-
}
37-
38-
let makro = if let Some(mc) = ctx.find_node_at_offset_with_descend::<ast::MacroCall>() {
39-
MakroTypes::MacroCall(mc)
40-
} else if let Some(mr) = ctx.find_node_at_offset_with_descend::<ast::MacroRules>() {
41-
MakroTypes::MacroRules(mr)
42-
} else {
43-
return None;
44-
};
39+
let makro = ctx.find_node_at_offset_with_descend::<ast::MacroCall>()?.clone_for_update();
40+
let makro_text_range = makro.syntax().text_range();
4541

4642
let cursor_offset = ctx.offset();
47-
let token_tree = match makro {
48-
MakroTypes::MacroRules(mr) => mr.token_tree()?.clone_for_update(),
49-
MakroTypes::MacroCall(md) => md.token_tree()?.clone_for_update(),
50-
};
43+
let semicolon = makro.semicolon_token();
44+
let token_tree = makro.token_tree()?;
5145

52-
let token_tree_text_range = token_tree.syntax().text_range();
5346
let ltoken = token_tree.left_delimiter_token()?;
5447
let rtoken = token_tree.right_delimiter_token()?;
5548

@@ -84,6 +77,9 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
8477
MacroDelims::LPar | MacroDelims::RPar => {
8578
ted::replace(ltoken, make::token(T!['{']));
8679
ted::replace(rtoken, make::token(T!['}']));
80+
if let Some(sc) = semicolon {
81+
ted::remove(sc);
82+
}
8783
}
8884
MacroDelims::LBra | MacroDelims::RBra => {
8985
ted::replace(ltoken, make::token(T!['(']));
@@ -94,7 +90,7 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
9490
ted::replace(rtoken, make::token(T![']']));
9591
}
9692
}
97-
builder.replace(token_tree_text_range, token_tree.syntax().text());
93+
builder.replace(makro_text_range, makro.syntax().text());
9894
},
9995
)
10096
}
@@ -110,12 +106,18 @@ mod tests {
110106
check_assist(
111107
toggle_macro_delimiter,
112108
r#"
113-
macro_rules! sth ();
109+
macro_rules! sth {
110+
() => {};
111+
}
112+
114113
sth! $0( );
115114
"#,
116115
r#"
117-
macro_rules! sth ();
118-
sth! { };
116+
macro_rules! sth {
117+
() => {};
118+
}
119+
120+
sth! { }
119121
"#,
120122
)
121123
}
@@ -125,11 +127,17 @@ sth! { };
125127
check_assist(
126128
toggle_macro_delimiter,
127129
r#"
128-
macro_rules! sth ();
130+
macro_rules! sth {
131+
() => {};
132+
}
133+
129134
sth! $0{ };
130135
"#,
131136
r#"
132-
macro_rules! sth ();
137+
macro_rules! sth {
138+
() => {};
139+
}
140+
133141
sth! [ ];
134142
"#,
135143
)
@@ -140,11 +148,17 @@ sth! [ ];
140148
check_assist(
141149
toggle_macro_delimiter,
142150
r#"
143-
macro_rules! sth ();
151+
macro_rules! sth {
152+
() => {};
153+
}
154+
144155
sth! $0[ ];
145156
"#,
146157
r#"
147-
macro_rules! sth ();
158+
macro_rules! sth {
159+
() => {};
160+
}
161+
148162
sth! ( );
149163
"#,
150164
)
@@ -156,72 +170,87 @@ sth! ( );
156170
toggle_macro_delimiter,
157171
r#"
158172
mod abc {
159-
macro_rules! sth ();
173+
macro_rules! sth {
174+
() => {};
175+
}
176+
160177
sth! $0{ };
161178
}
162179
"#,
163180
r#"
164181
mod abc {
165-
macro_rules! sth ();
182+
macro_rules! sth {
183+
() => {};
184+
}
185+
166186
sth! [ ];
167187
}
168188
"#,
169189
)
170190
}
171191

172192
#[test]
173-
fn test_rules_par() {
174-
check_assist(
193+
fn test_unrelated_par() {
194+
check_assist_not_applicable(
175195
toggle_macro_delimiter,
176196
r#"
177-
macro_rules! sth $0();
178-
sth! ( );
179-
"#,
180-
r#"
181-
macro_rules! sth {};
182-
sth! ( );
183-
"#,
184-
)
185-
}
197+
macro_rules! prt {
198+
($e:expr) => {{
199+
println!("{}", stringify! {$e});
200+
}};
201+
}
202+
203+
prt!(($03 + 5));
186204
187-
#[test]
188-
fn test_rules_braclets() {
189-
check_assist(
190-
toggle_macro_delimiter,
191-
r#"
192-
macro_rules! sth $0{};
193-
sth! ( );
194-
"#,
195-
r#"
196-
macro_rules! sth [];
197-
sth! ( );
198205
"#,
199206
)
200207
}
201208

202209
#[test]
203-
fn test_rules_brackets() {
210+
fn test_longer_macros() {
204211
check_assist(
205212
toggle_macro_delimiter,
206213
r#"
207-
macro_rules! sth $0[];
208-
sth! ( );
209-
"#,
214+
macro_rules! prt {
215+
($e:expr) => {{
216+
println!("{}", stringify! {$e});
217+
}};
218+
}
219+
220+
prt! $0((3 + 5));
221+
"#,
210222
r#"
211-
macro_rules! sth ();
212-
sth! ( );
213-
"#,
223+
macro_rules! prt {
224+
($e:expr) => {{
225+
println!("{}", stringify! {$e});
226+
}};
227+
}
228+
229+
prt! {(3 + 5)}
230+
"#,
214231
)
215232
}
216233

234+
// FIXME @alibektas : Inner macro_call is not seen as such. So this doesn't work.
217235
#[test]
218-
fn test_unrelated_par() {
236+
fn test_nested_macros() {
219237
check_assist_not_applicable(
220238
toggle_macro_delimiter,
221239
r#"
222-
macro_rules! sth [def$0()];
223-
sth! ( );
224-
"#,
240+
macro_rules! prt {
241+
($e:expr) => {{
242+
println!("{}", stringify! {$e});
243+
}};
244+
}
245+
246+
macro_rules! abc {
247+
($e:expr) => {{
248+
println!("{}", stringify! {$e});
249+
}};
250+
}
251+
252+
prt! {abc!($03 + 5)};
253+
"#,
225254
)
226255
}
227256
}

src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs

+21
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,27 @@ fn arithmetics {
30913091
)
30923092
}
30933093

3094+
#[test]
3095+
fn doctest_toggle_macro_delimiter() {
3096+
check_doc_test(
3097+
"toggle_macro_delimiter",
3098+
r#####"
3099+
macro_rules! sth {
3100+
() => {};
3101+
}
3102+
3103+
sth! $0( );
3104+
"#####,
3105+
r#####"
3106+
macro_rules! sth {
3107+
() => {};
3108+
}
3109+
3110+
sth! { }
3111+
"#####,
3112+
)
3113+
}
3114+
30943115
#[test]
30953116
fn doctest_unmerge_match_arm() {
30963117
check_doc_test(

0 commit comments

Comments
 (0)