diff options
-rw-r--r-- | crates/ra_assists/src/handlers/remove_dbg.rs | 94 |
1 files changed, 85 insertions, 9 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index a616cca57..9430ce1b5 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | TextSize, T, | 3 | TextRange, TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -27,19 +27,33 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | 29 | ||
30 | let macro_range = macro_call.syntax().text_range(); | 30 | let is_leaf = macro_call.syntax().next_sibling().is_none(); |
31 | 31 | ||
32 | let macro_content = { | 32 | let macro_end = if macro_call.semicolon_token().is_some() { |
33 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 33 | macro_call.syntax().text_range().end() - TextSize::of(';') |
34 | } else { | ||
35 | macro_call.syntax().text_range().end() | ||
36 | }; | ||
34 | 37 | ||
35 | let text = macro_args.text(); | 38 | // macro_range determines what will be deleted and replaced with macro_content |
36 | let without_parens = TextSize::of('(')..text.len() - TextSize::of(')'); | 39 | let macro_range = TextRange::new(macro_call.syntax().text_range().start(), macro_end); |
37 | text.slice(without_parens).to_string() | 40 | let paste_instead_of_dbg = { |
41 | let text = macro_call.token_tree()?.syntax().text(); | ||
42 | |||
43 | // leafiness determines if we should include the parenthesis or not | ||
44 | let slice_index: TextRange = if is_leaf { | ||
45 | // leaf means - we can extract the contents of the dbg! in text | ||
46 | TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')) | ||
47 | } else { | ||
48 | // not leaf - means we should keep the parens | ||
49 | TextRange::up_to(text.len()) | ||
50 | }; | ||
51 | text.slice(slice_index).to_string() | ||
38 | }; | 52 | }; |
39 | 53 | ||
40 | let target = macro_call.syntax().text_range(); | 54 | let target = macro_call.syntax().text_range(); |
41 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { | 55 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { |
42 | builder.replace(macro_range, macro_content); | 56 | builder.replace(macro_range, paste_instead_of_dbg); |
43 | }) | 57 | }) |
44 | } | 58 | } |
45 | 59 | ||
@@ -99,6 +113,7 @@ fn foo(n: usize) { | |||
99 | ", | 113 | ", |
100 | ); | 114 | ); |
101 | } | 115 | } |
116 | |||
102 | #[test] | 117 | #[test] |
103 | fn test_remove_dbg_with_brackets_and_braces() { | 118 | fn test_remove_dbg_with_brackets_and_braces() { |
104 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); | 119 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); |
@@ -113,7 +128,7 @@ fn foo(n: usize) { | |||
113 | } | 128 | } |
114 | 129 | ||
115 | #[test] | 130 | #[test] |
116 | fn remove_dbg_target() { | 131 | fn test_remove_dbg_target() { |
117 | check_assist_target( | 132 | check_assist_target( |
118 | remove_dbg, | 133 | remove_dbg, |
119 | " | 134 | " |
@@ -126,4 +141,65 @@ fn foo(n: usize) { | |||
126 | "dbg!(n.checked_sub(4))", | 141 | "dbg!(n.checked_sub(4))", |
127 | ); | 142 | ); |
128 | } | 143 | } |
144 | |||
145 | #[test] | ||
146 | fn test_remove_dbg_keep_semicolon() { | ||
147 | // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779 | ||
148 | // not quite though | ||
149 | // adding a comment at the end of the line makes | ||
150 | // the ast::MacroCall to include the semicolon at the end | ||
151 | check_assist( | ||
152 | remove_dbg, | ||
153 | r#"let res = <|>dbg!(1 * 20); // needless comment"#, | ||
154 | r#"let res = 1 * 20; // needless comment"#, | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | #[test] | ||
159 | fn test_remove_dbg_keep_expression() { | ||
160 | check_assist( | ||
161 | remove_dbg, | ||
162 | r#"let res = <|>dbg!(a + b).foo();"#, | ||
163 | r#"let res = (a + b).foo();"#, | ||
164 | ); | ||
165 | } | ||
166 | |||
167 | #[test] | ||
168 | fn test_remove_dbg_from_inside_fn() { | ||
169 | check_assist_target( | ||
170 | remove_dbg, | ||
171 | r#" | ||
172 | fn square(x: u32) -> u32 { | ||
173 | x * x | ||
174 | } | ||
175 | |||
176 | fn main() { | ||
177 | let x = square(dbg<|>!(5 + 10)); | ||
178 | println!("{}", x); | ||
179 | }"#, | ||
180 | "dbg!(5 + 10)", | ||
181 | ); | ||
182 | |||
183 | check_assist( | ||
184 | remove_dbg, | ||
185 | r#" | ||
186 | fn square(x: u32) -> u32 { | ||
187 | x * x | ||
188 | } | ||
189 | |||
190 | fn main() { | ||
191 | let x = square(dbg<|>!(5 + 10)); | ||
192 | println!("{}", x); | ||
193 | }"#, | ||
194 | r#" | ||
195 | fn square(x: u32) -> u32 { | ||
196 | x * x | ||
197 | } | ||
198 | |||
199 | fn main() { | ||
200 | let x = square(5 + 10); | ||
201 | println!("{}", x); | ||
202 | }"#, | ||
203 | ); | ||
204 | } | ||
129 | } | 205 | } |