diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/handlers/remove_dbg.rs | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index c43b89661..9430ce1b5 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs | |||
@@ -27,12 +27,12 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | 29 | ||
30 | let semicolon_on_end = macro_call.semicolon_token().is_some(); | ||
31 | let is_leaf = macro_call.syntax().next_sibling().is_none(); | 30 | let is_leaf = macro_call.syntax().next_sibling().is_none(); |
32 | 31 | ||
33 | let macro_end = match semicolon_on_end { | 32 | let macro_end = if macro_call.semicolon_token().is_some() { |
34 | true => macro_call.syntax().text_range().end() - TextSize::of(';'), | 33 | macro_call.syntax().text_range().end() - TextSize::of(';') |
35 | false => macro_call.syntax().text_range().end(), | 34 | } else { |
35 | macro_call.syntax().text_range().end() | ||
36 | }; | 36 | }; |
37 | 37 | ||
38 | // macro_range determines what will be deleted and replaced with macro_content | 38 | // macro_range determines what will be deleted and replaced with macro_content |
@@ -40,12 +40,13 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
40 | let paste_instead_of_dbg = { | 40 | let paste_instead_of_dbg = { |
41 | let text = macro_call.token_tree()?.syntax().text(); | 41 | let text = macro_call.token_tree()?.syntax().text(); |
42 | 42 | ||
43 | // leafines determines if we should include the parenthesis or not | 43 | // leafiness determines if we should include the parenthesis or not |
44 | let slice_index: TextRange = match is_leaf { | 44 | let slice_index: TextRange = if is_leaf { |
45 | // leaf means - we can extract the contents of the dbg! in text | 45 | // leaf means - we can extract the contents of the dbg! in text |
46 | true => TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')), | 46 | TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')) |
47 | } else { | ||
47 | // not leaf - means we should keep the parens | 48 | // not leaf - means we should keep the parens |
48 | false => TextRange::new(TextSize::from(0 as u32), text.len()), | 49 | TextRange::up_to(text.len()) |
49 | }; | 50 | }; |
50 | text.slice(slice_index).to_string() | 51 | text.slice(slice_index).to_string() |
51 | }; | 52 | }; |
@@ -147,45 +148,58 @@ fn foo(n: usize) { | |||
147 | // not quite though | 148 | // not quite though |
148 | // adding a comment at the end of the line makes | 149 | // adding a comment at the end of the line makes |
149 | // the ast::MacroCall to include the semicolon at the end | 150 | // the ast::MacroCall to include the semicolon at the end |
150 | let code = " | 151 | check_assist( |
151 | let res = <|>dbg!(1 * 20); // needless comment | 152 | remove_dbg, |
152 | "; | 153 | r#"let res = <|>dbg!(1 * 20); // needless comment"#, |
153 | let expected = " | 154 | r#"let res = 1 * 20; // needless comment"#, |
154 | let res = 1 * 20; // needless comment | 155 | ); |
155 | "; | ||
156 | check_assist(remove_dbg, code, expected); | ||
157 | } | 156 | } |
158 | 157 | ||
159 | #[test] | 158 | #[test] |
160 | fn test_remove_dbg_keep_expression() { | 159 | fn test_remove_dbg_keep_expression() { |
161 | let code = " | 160 | check_assist( |
162 | let res = <|>dbg!(a + b).foo();"; | 161 | remove_dbg, |
163 | let expected = "let res = (a + b).foo();"; | 162 | r#"let res = <|>dbg!(a + b).foo();"#, |
164 | check_assist(remove_dbg, code, expected); | 163 | r#"let res = (a + b).foo();"#, |
164 | ); | ||
165 | } | 165 | } |
166 | 166 | ||
167 | #[test] | 167 | #[test] |
168 | fn test_remove_dbg_from_inside_fn() { | 168 | fn test_remove_dbg_from_inside_fn() { |
169 | let code = " | 169 | check_assist_target( |
170 | remove_dbg, | ||
171 | r#" | ||
170 | fn square(x: u32) -> u32 { | 172 | fn square(x: u32) -> u32 { |
171 | x * x | 173 | x * x |
172 | } | 174 | } |
173 | 175 | ||
174 | fn main() { | 176 | fn main() { |
175 | let x = square(dbg<|>!(5 + 10)); | 177 | let x = square(dbg<|>!(5 + 10)); |
176 | println!(\"{}\", x); | 178 | println!("{}", x); |
177 | }"; | 179 | }"#, |
180 | "dbg!(5 + 10)", | ||
181 | ); | ||
178 | 182 | ||
179 | let expected = " | 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#" | ||
180 | fn square(x: u32) -> u32 { | 195 | fn square(x: u32) -> u32 { |
181 | x * x | 196 | x * x |
182 | } | 197 | } |
183 | 198 | ||
184 | fn main() { | 199 | fn main() { |
185 | let x = square(5 + 10); | 200 | let x = square(5 + 10); |
186 | println!(\"{}\", x); | 201 | println!("{}", x); |
187 | }"; | 202 | }"#, |
188 | check_assist_target(remove_dbg, code, "dbg!(5 + 10)"); | 203 | ); |
189 | check_assist(remove_dbg, code, expected); | ||
190 | } | 204 | } |
191 | } | 205 | } |