@@ -43,15 +43,18 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
43
43
44
44
fn adjusted_macro_contents ( macro_call : & ast:: MacroCall ) -> Option < String > {
45
45
let contents = get_valid_macrocall_contents ( & macro_call, "dbg" ) ?;
46
- let is_leaf = macro_call. syntax ( ) . next_sibling ( ) . is_none ( ) ;
47
46
let macro_text_with_brackets = macro_call. token_tree ( ) ?. syntax ( ) . text ( ) ;
48
- let slice_index = if is_leaf || !needs_parentheses_around_macro_contents ( contents) {
49
- TextRange :: new ( TextSize :: of ( '(' ) , macro_text_with_brackets. len ( ) - TextSize :: of ( ')' ) )
47
+ let macro_text_in_brackets = macro_text_with_brackets. slice ( TextRange :: new (
48
+ TextSize :: of ( '(' ) ,
49
+ macro_text_with_brackets. len ( ) - TextSize :: of ( ')' ) ,
50
+ ) ) ;
51
+
52
+ let is_leaf = macro_call. syntax ( ) . next_sibling ( ) . is_none ( ) ;
53
+ Some ( if !is_leaf && needs_parentheses_around_macro_contents ( contents) {
54
+ format ! ( "({})" , macro_text_in_brackets)
50
55
} else {
51
- // leave parenthesis around macro contents to preserve the semantics
52
- TextRange :: up_to ( macro_text_with_brackets. len ( ) )
53
- } ;
54
- Some ( macro_text_with_brackets. slice ( slice_index) . to_string ( ) )
56
+ macro_text_in_brackets. to_string ( )
57
+ } )
55
58
}
56
59
57
60
/// Verifies that the given macro_call actually matches the given name
@@ -90,19 +93,10 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
90
93
if macro_contents. len ( ) < 2 {
91
94
return false ;
92
95
}
93
-
94
- let mut macro_contents_kind_not_in_brackets = Vec :: with_capacity ( macro_contents. len ( ) ) ;
95
-
96
- let mut first_bracket_in_macro = None ;
97
96
let mut unpaired_brackets_in_contents = Vec :: new ( ) ;
98
97
for element in macro_contents {
99
98
match element. kind ( ) {
100
- T ! [ '(' ] | T ! [ '[' ] | T ! [ '{' ] => {
101
- if let None = first_bracket_in_macro {
102
- first_bracket_in_macro = Some ( element. clone ( ) )
103
- }
104
- unpaired_brackets_in_contents. push ( element) ;
105
- }
99
+ T ! [ '(' ] | T ! [ '[' ] | T ! [ '{' ] => unpaired_brackets_in_contents. push ( element) ,
106
100
T ! [ ')' ] => {
107
101
if !matches ! ( unpaired_brackets_in_contents. pop( ) , Some ( correct_bracket) if correct_bracket. kind( ) == T ![ '(' ] )
108
102
{
@@ -121,19 +115,15 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
121
115
return true ;
122
116
}
123
117
}
124
- other_kind => {
125
- if unpaired_brackets_in_contents. is_empty ( ) {
126
- macro_contents_kind_not_in_brackets. push ( other_kind) ;
118
+ symbol_kind => {
119
+ let symbol_not_in_bracket = unpaired_brackets_in_contents. is_empty ( ) ;
120
+ if symbol_not_in_bracket && symbol_kind. is_punct ( ) {
121
+ return true ;
127
122
}
128
123
}
129
124
}
130
125
}
131
-
132
126
!unpaired_brackets_in_contents. is_empty ( )
133
- || matches ! ( first_bracket_in_macro, Some ( bracket) if bracket. kind( ) != T ![ '(' ] )
134
- || macro_contents_kind_not_in_brackets
135
- . into_iter ( )
136
- . any ( |macro_contents_kind| macro_contents_kind. is_punct ( ) )
137
127
}
138
128
139
129
#[ cfg( test) ]
@@ -244,6 +234,7 @@ fn main() {
244
234
) ;
245
235
246
236
check_assist ( remove_dbg, r#"let res = <|>dbg!(2 + 2) * 5"# , r#"let res = (2 + 2) * 5"# ) ;
237
+ check_assist ( remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"# , r#"let res = (2 + 2) * 5"# ) ;
247
238
}
248
239
249
240
#[ test]
0 commit comments