@@ -11,12 +11,18 @@ use crate::{AssistContext, Assists};
11
11
// Change macro delimiters in the order of `( -> { -> [ -> (`.
12
12
//
13
13
// ```
14
- // macro_rules! sth ();
14
+ // macro_rules! sth {
15
+ // () => {};
16
+ // }
17
+ //
15
18
// sth! $0( );
16
19
// ```
17
20
// ->
18
21
// ```
19
- // macro_rules! sth! ();
22
+ // macro_rules! sth {
23
+ // () => {};
24
+ // }
25
+ //
20
26
// sth! { }
21
27
// ```
22
28
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<'_>)
30
36
RCur ,
31
37
}
32
38
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 ( ) ;
45
41
46
42
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 ( ) ?;
51
45
52
- let token_tree_text_range = token_tree. syntax ( ) . text_range ( ) ;
53
46
let ltoken = token_tree. left_delimiter_token ( ) ?;
54
47
let rtoken = token_tree. right_delimiter_token ( ) ?;
55
48
@@ -84,6 +77,9 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
84
77
MacroDelims :: LPar | MacroDelims :: RPar => {
85
78
ted:: replace ( ltoken, make:: token ( T ! [ '{' ] ) ) ;
86
79
ted:: replace ( rtoken, make:: token ( T ! [ '}' ] ) ) ;
80
+ if let Some ( sc) = semicolon {
81
+ ted:: remove ( sc) ;
82
+ }
87
83
}
88
84
MacroDelims :: LBra | MacroDelims :: RBra => {
89
85
ted:: replace ( ltoken, make:: token ( T ! [ '(' ] ) ) ;
@@ -94,7 +90,7 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
94
90
ted:: replace ( rtoken, make:: token ( T ! [ ']' ] ) ) ;
95
91
}
96
92
}
97
- builder. replace ( token_tree_text_range , token_tree . syntax ( ) . text ( ) ) ;
93
+ builder. replace ( makro_text_range , makro . syntax ( ) . text ( ) ) ;
98
94
} ,
99
95
)
100
96
}
@@ -110,12 +106,18 @@ mod tests {
110
106
check_assist (
111
107
toggle_macro_delimiter,
112
108
r#"
113
- macro_rules! sth ();
109
+ macro_rules! sth {
110
+ () => {};
111
+ }
112
+
114
113
sth! $0( );
115
114
"# ,
116
115
r#"
117
- macro_rules! sth ();
118
- sth! { };
116
+ macro_rules! sth {
117
+ () => {};
118
+ }
119
+
120
+ sth! { }
119
121
"# ,
120
122
)
121
123
}
@@ -125,11 +127,17 @@ sth! { };
125
127
check_assist (
126
128
toggle_macro_delimiter,
127
129
r#"
128
- macro_rules! sth ();
130
+ macro_rules! sth {
131
+ () => {};
132
+ }
133
+
129
134
sth! $0{ };
130
135
"# ,
131
136
r#"
132
- macro_rules! sth ();
137
+ macro_rules! sth {
138
+ () => {};
139
+ }
140
+
133
141
sth! [ ];
134
142
"# ,
135
143
)
@@ -140,11 +148,17 @@ sth! [ ];
140
148
check_assist (
141
149
toggle_macro_delimiter,
142
150
r#"
143
- macro_rules! sth ();
151
+ macro_rules! sth {
152
+ () => {};
153
+ }
154
+
144
155
sth! $0[ ];
145
156
"# ,
146
157
r#"
147
- macro_rules! sth ();
158
+ macro_rules! sth {
159
+ () => {};
160
+ }
161
+
148
162
sth! ( );
149
163
"# ,
150
164
)
@@ -156,72 +170,87 @@ sth! ( );
156
170
toggle_macro_delimiter,
157
171
r#"
158
172
mod abc {
159
- macro_rules! sth ();
173
+ macro_rules! sth {
174
+ () => {};
175
+ }
176
+
160
177
sth! $0{ };
161
178
}
162
179
"# ,
163
180
r#"
164
181
mod abc {
165
- macro_rules! sth ();
182
+ macro_rules! sth {
183
+ () => {};
184
+ }
185
+
166
186
sth! [ ];
167
187
}
168
188
"# ,
169
189
)
170
190
}
171
191
172
192
#[ test]
173
- fn test_rules_par ( ) {
174
- check_assist (
193
+ fn test_unrelated_par ( ) {
194
+ check_assist_not_applicable (
175
195
toggle_macro_delimiter,
176
196
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));
186
204
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! ( );
198
205
"# ,
199
206
)
200
207
}
201
208
202
209
#[ test]
203
- fn test_rules_brackets ( ) {
210
+ fn test_longer_macros ( ) {
204
211
check_assist (
205
212
toggle_macro_delimiter,
206
213
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
+ "# ,
210
222
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
+ "# ,
214
231
)
215
232
}
216
233
234
+ // FIXME @alibektas : Inner macro_call is not seen as such. So this doesn't work.
217
235
#[ test]
218
- fn test_unrelated_par ( ) {
236
+ fn test_nested_macros ( ) {
219
237
check_assist_not_applicable (
220
238
toggle_macro_delimiter,
221
239
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
+ "# ,
225
254
)
226
255
}
227
256
}
0 commit comments