aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/handlers/remove_dbg.rs31
1 files changed, 23 insertions, 8 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs
index 1116d2a2c..c43b89661 100644
--- a/crates/ra_assists/src/handlers/remove_dbg.rs
+++ b/crates/ra_assists/src/handlers/remove_dbg.rs
@@ -1,6 +1,6 @@
1use ra_syntax::{ 1use ra_syntax::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode},
3 TextSize, T, 3 TextRange, TextSize, T,
4}; 4};
5 5
6use crate::{AssistContext, AssistId, AssistKind, Assists}; 6use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -27,19 +27,32 @@ 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 semicolon_on_end = macro_call.semicolon_token().is_some();
31 let is_leaf = macro_call.syntax().next_sibling().is_none();
31 32
32 let macro_content = { 33 let macro_end = match semicolon_on_end {
33 let macro_args = macro_call.token_tree()?.syntax().clone(); 34 true => macro_call.syntax().text_range().end() - TextSize::of(';'),
35 false => 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 // leafines determines if we should include the parenthesis or not
44 let slice_index: TextRange = match is_leaf {
45 // leaf means - we can extract the contents of the dbg! in text
46 true => TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')),
47 // not leaf - means we should keep the parens
48 false => TextRange::new(TextSize::from(0 as u32), text.len()),
49 };
50 text.slice(slice_index).to_string()
38 }; 51 };
39 52
40 let target = macro_call.syntax().text_range(); 53 let target = macro_call.syntax().text_range();
41 acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { 54 acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| {
42 builder.replace(macro_range, macro_content); 55 builder.replace(macro_range, paste_instead_of_dbg);
43 }) 56 })
44} 57}
45 58
@@ -132,6 +145,8 @@ fn foo(n: usize) {
132 fn test_remove_dbg_keep_semicolon() { 145 fn test_remove_dbg_keep_semicolon() {
133 // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779 146 // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779
134 // not quite though 147 // not quite though
148 // adding a comment at the end of the line makes
149 // the ast::MacroCall to include the semicolon at the end
135 let code = " 150 let code = "
136let res = <|>dbg!(1 * 20); // needless comment 151let res = <|>dbg!(1 * 20); // needless comment
137"; 152";