aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src
diff options
context:
space:
mode:
authorivan770 <[email protected]>2021-03-27 09:10:20 +0000
committerivan770 <[email protected]>2021-03-27 09:10:20 +0000
commitc7cd0aff4a8332171f014c0ee2893af1d934a3be (patch)
treef536809747b65e4894f3b7156fc385f8a38cb6ba /crates/ide_assists/src
parent2292ff64f1d8460324236ac3f13af47553ad90b5 (diff)
Remove dbg expression and newline as whole
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r--crates/ide_assists/src/handlers/remove_dbg.rs34
1 files changed, 28 insertions, 6 deletions
diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs
index e0e110c70..449795efd 100644
--- a/crates/ide_assists/src/handlers/remove_dbg.rs
+++ b/crates/ide_assists/src/handlers/remove_dbg.rs
@@ -1,5 +1,5 @@
1use syntax::{ 1use syntax::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode, AstToken},
3 match_ast, SyntaxElement, TextRange, TextSize, T, 3 match_ast, SyntaxElement, TextRange, TextSize, T,
4}; 4};
5 5
@@ -24,11 +24,25 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; 24 let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
25 let new_contents = adjusted_macro_contents(&macro_call)?; 25 let new_contents = adjusted_macro_contents(&macro_call)?;
26 26
27 let macro_text_range = macro_call.syntax().text_range(); 27 let macro_text_range = if new_contents.is_empty() {
28 let macro_end = if macro_call.semicolon_token().is_some() { 28 let parent = macro_call.syntax().parent()?;
29 macro_text_range.end() - TextSize::of(';') 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)
30 } else { 39 } 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(),
32 }; 46 };
33 47
34 acc.add( 48 acc.add(
@@ -417,6 +431,14 @@ fn main() {
417 431
418 #[test] 432 #[test]
419 fn test_remove_empty_dbg() { 433 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 );
421 } 443 }
422} 444}