Skip to content

Commit 855a739

Browse files
bors[bot]ivan770
andauthored
Merge #8207
8207: Show dbg remove assist on empty contents r=edwin0cheng a=ivan770 Closes #8185 Co-authored-by: ivan770 <leshenko.ivan770@gmail.com> Co-authored-by: ivan770 <ivan@ivan770.me>
2 parents 0c0d648 + f9d17c6 commit 855a739

File tree

1 file changed

+94
-11
lines changed

1 file changed

+94
-11
lines changed

crates/ide_assists/src/handlers/remove_dbg.rs

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, AstNode},
2+
ast::{self, AstNode, AstToken},
33
match_ast, SyntaxElement, TextRange, TextSize, T,
44
};
55

@@ -24,7 +24,39 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2424
let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
2525
let new_contents = adjusted_macro_contents(&macro_call)?;
2626

27-
let macro_text_range = macro_call.syntax().text_range();
27+
let parent = macro_call.syntax().parent();
28+
29+
let macro_text_range = if let Some(it) = parent.as_ref() {
30+
if new_contents.is_empty() {
31+
match_ast! {
32+
match it {
33+
ast::BlockExpr(it) => {
34+
macro_call.syntax()
35+
.prev_sibling_or_token()
36+
.and_then(whitespace_start)
37+
.map(|start| TextRange::new(start, macro_call.syntax().text_range().end()))
38+
.unwrap_or(macro_call.syntax().text_range())
39+
},
40+
ast::ExprStmt(it) => {
41+
let start = it
42+
.syntax()
43+
.prev_sibling_or_token()
44+
.and_then(whitespace_start)
45+
.unwrap_or(it.syntax().text_range().start());
46+
let end = it.syntax().text_range().end();
47+
48+
TextRange::new(start, end)
49+
},
50+
_ => macro_call.syntax().text_range()
51+
}
52+
}
53+
} else {
54+
macro_call.syntax().text_range()
55+
}
56+
} else {
57+
macro_call.syntax().text_range()
58+
};
59+
2860
let macro_end = if macro_call.semicolon_token().is_some() {
2961
macro_text_range.end() - TextSize::of(';')
3062
} else {
@@ -36,11 +68,22 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
3668
"Remove dbg!()",
3769
macro_text_range,
3870
|builder| {
39-
builder.replace(TextRange::new(macro_text_range.start(), macro_end), new_contents);
71+
builder.replace(
72+
TextRange::new(macro_text_range.start(), macro_end),
73+
if new_contents.is_empty() && parent.and_then(ast::LetStmt::cast).is_some() {
74+
ast::make::expr_unit().to_string()
75+
} else {
76+
new_contents
77+
},
78+
);
4079
},
4180
)
4281
}
4382

83+
fn whitespace_start(it: SyntaxElement) -> Option<TextSize> {
84+
Some(it.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start())
85+
}
86+
4487
fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> {
4588
let contents = get_valid_macrocall_contents(&macro_call, "dbg")?;
4689
let macro_text_with_brackets = macro_call.token_tree()?.syntax().text();
@@ -94,15 +137,11 @@ fn get_valid_macrocall_contents(
94137
let mut contents_between_brackets = children_with_tokens.collect::<Vec<_>>();
95138
let last_child = contents_between_brackets.pop()?;
96139

97-
if contents_between_brackets.is_empty() {
98-
None
99-
} else {
100-
match (first_child.kind(), last_child.kind()) {
101-
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => {
102-
Some(contents_between_brackets)
103-
}
104-
_ => None,
140+
match (first_child.kind(), last_child.kind()) {
141+
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => {
142+
Some(contents_between_brackets)
105143
}
144+
_ => None,
106145
}
107146
}
108147

@@ -415,6 +454,50 @@ fn main() {
415454
}"#,
416455
r#"fn foo() {
417456
match &x {}
457+
}"#,
458+
);
459+
}
460+
461+
#[test]
462+
fn test_remove_empty_dbg() {
463+
check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#);
464+
check_assist(
465+
remove_dbg,
466+
r#"
467+
fn foo() {
468+
$0dbg!();
469+
}
470+
"#,
471+
r#"
472+
fn foo() {
473+
}
474+
"#,
475+
);
476+
check_assist(
477+
remove_dbg,
478+
r#"
479+
fn foo() {
480+
let test = $0dbg!();
481+
}"#,
482+
r#"
483+
fn foo() {
484+
let test = ();
485+
}"#,
486+
);
487+
check_assist(
488+
remove_dbg,
489+
r#"
490+
fn foo() {
491+
let t = {
492+
println!("Hello, world");
493+
$0dbg!()
494+
};
495+
}"#,
496+
r#"
497+
fn foo() {
498+
let t = {
499+
println!("Hello, world");
500+
};
418501
}"#,
419502
);
420503
}

0 commit comments

Comments
 (0)