Skip to content

Commit c7cd0af

Browse files
committed
Remove dbg expression and newline as whole
1 parent 2292ff6 commit c7cd0af

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

crates/ide_assists/src/handlers/remove_dbg.rs

Lines changed: 28 additions & 6 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,11 +24,25 @@ 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();
28-
let macro_end = if macro_call.semicolon_token().is_some() {
29-
macro_text_range.end() - TextSize::of(';')
27+
let macro_text_range = if new_contents.is_empty() {
28+
let parent = macro_call.syntax().parent()?;
29+
30+
let start = parent
31+
.prev_sibling_or_token()
32+
.and_then(|el| {
33+
Some(el.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start())
34+
})
35+
.unwrap_or(parent.text_range().start());
36+
let end = parent.text_range().end();
37+
38+
TextRange::new(start, end)
3039
} else {
31-
macro_text_range.end()
40+
macro_call.syntax().text_range()
41+
};
42+
43+
let macro_end = match macro_call.semicolon_token() {
44+
Some(_) => macro_text_range.end() - TextSize::of(';'),
45+
None => macro_text_range.end(),
3246
};
3347

3448
acc.add(
@@ -417,6 +431,14 @@ fn main() {
417431

418432
#[test]
419433
fn test_remove_empty_dbg() {
420-
check_assist(remove_dbg, r#"$0dbg!()"#, r#""#);
434+
check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#);
435+
check_assist(
436+
remove_dbg,
437+
r#"fn foo() {
438+
$0dbg!();
439+
}"#,
440+
r#"fn foo() {
441+
}"#,
442+
);
421443
}
422444
}

0 commit comments

Comments
 (0)