Skip to content

Commit 6ea28c3

Browse files
committed
Fixed #5129
Addresses two issues: - keep the parens from dbg!() in case the call is chained or there is semantic difference if parens are excluded - Exclude the semicolon after the dbg!(); by checking if it was accidentally included in the macro_call investigated, but decided against: fix ast::MacroCall extraction to never include semicolons at the end - this logic lives in rowan. Defensively shorten the macro_range if there is a semicolon token. Deleted unneccessary temp variable macro_args Renamed macro_content to "paste_instead_of_dbg", because it isn't a simple extraction of text inside dbg!() anymore
1 parent 01bdeaa commit 6ea28c3

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

crates/ra_assists/src/handlers/remove_dbg.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_syntax::{
22
ast::{self, AstNode},
3-
TextSize, T,
3+
TextRange, TextSize, T,
44
};
55

66
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -27,19 +27,32 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2727
return None;
2828
}
2929

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();
3132

32-
let macro_content = {
33-
let macro_args = macro_call.token_tree()?.syntax().clone();
33+
let macro_end = match semicolon_on_end {
34+
true => macro_call.syntax().text_range().end() - TextSize::of(';'),
35+
false => macro_call.syntax().text_range().end(),
36+
};
3437

35-
let text = macro_args.text();
36-
let without_parens = TextSize::of('(')..text.len() - TextSize::of(')');
37-
text.slice(without_parens).to_string()
38+
// macro_range determines what will be deleted and replaced with macro_content
39+
let macro_range = TextRange::new(macro_call.syntax().text_range().start(), macro_end);
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()
3851
};
3952

4053
let target = macro_call.syntax().text_range();
4154
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);
4356
})
4457
}
4558

@@ -132,6 +145,8 @@ fn foo(n: usize) {
132145
fn test_remove_dbg_keep_semicolon() {
133146
// https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779
134147
// 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
135150
let code = "
136151
let res = <|>dbg!(1 * 20); // needless comment
137152
";

0 commit comments

Comments
 (0)