Skip to content

Commit 4567ae5

Browse files
bors[bot]petr-tik
andauthored
Merge #5554
5554: Fix remove_dbg r=matklad a=petr-tik Closes #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 Co-authored-by: petr-tik <[email protected]>
2 parents bd5d236 + 6ea393a commit 4567ae5

File tree

1 file changed

+85
-9
lines changed

1 file changed

+85
-9
lines changed

crates/ra_assists/src/handlers/remove_dbg.rs

Lines changed: 85 additions & 9 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,33 @@ 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 is_leaf = macro_call.syntax().next_sibling().is_none();
3131

32-
let macro_content = {
33-
let macro_args = macro_call.token_tree()?.syntax().clone();
32+
let macro_end = if macro_call.semicolon_token().is_some() {
33+
macro_call.syntax().text_range().end() - TextSize::of(';')
34+
} else {
35+
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+
// 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()
3852
};
3953

4054
let target = macro_call.syntax().text_range();
4155
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);
4357
})
4458
}
4559

@@ -99,6 +113,7 @@ fn foo(n: usize) {
99113
",
100114
);
101115
}
116+
102117
#[test]
103118
fn test_remove_dbg_with_brackets_and_braces() {
104119
check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1");
@@ -113,7 +128,7 @@ fn foo(n: usize) {
113128
}
114129

115130
#[test]
116-
fn remove_dbg_target() {
131+
fn test_remove_dbg_target() {
117132
check_assist_target(
118133
remove_dbg,
119134
"
@@ -126,4 +141,65 @@ fn foo(n: usize) {
126141
"dbg!(n.checked_sub(4))",
127142
);
128143
}
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+
}
129205
}

0 commit comments

Comments
 (0)