From 2292ff64f1d8460324236ac3f13af47553ad90b5 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Fri, 26 Mar 2021 16:15:26 +0200 Subject: Show dbg remove assist on empty contents --- crates/ide_assists/src/handlers/remove_dbg.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index 6114091f2..e0e110c70 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -94,15 +94,11 @@ fn get_valid_macrocall_contents( let mut contents_between_brackets = children_with_tokens.collect::>(); let last_child = contents_between_brackets.pop()?; - if contents_between_brackets.is_empty() { - None - } else { - match (first_child.kind(), last_child.kind()) { - (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => { - Some(contents_between_brackets) - } - _ => None, + match (first_child.kind(), last_child.kind()) { + (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => { + Some(contents_between_brackets) } + _ => None, } } @@ -418,4 +414,9 @@ fn main() { }"#, ); } + + #[test] + fn test_remove_empty_dbg() { + check_assist(remove_dbg, r#"$0dbg!()"#, r#""#); + } } -- cgit v1.2.3 From c7cd0aff4a8332171f014c0ee2893af1d934a3be Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sat, 27 Mar 2021 11:10:20 +0200 Subject: Remove dbg expression and newline as whole --- crates/ide_assists/src/handlers/remove_dbg.rs | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'crates/ide_assists') 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 @@ use syntax::{ - ast::{self, AstNode}, + ast::{self, AstNode, AstToken}, match_ast, SyntaxElement, TextRange, TextSize, T, }; @@ -24,11 +24,25 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let macro_call = ctx.find_node_at_offset::()?; let new_contents = adjusted_macro_contents(¯o_call)?; - let macro_text_range = macro_call.syntax().text_range(); - let macro_end = if macro_call.semicolon_token().is_some() { - macro_text_range.end() - TextSize::of(';') + let macro_text_range = if new_contents.is_empty() { + let parent = macro_call.syntax().parent()?; + + let start = parent + .prev_sibling_or_token() + .and_then(|el| { + Some(el.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start()) + }) + .unwrap_or(parent.text_range().start()); + let end = parent.text_range().end(); + + TextRange::new(start, end) } else { - macro_text_range.end() + macro_call.syntax().text_range() + }; + + let macro_end = match macro_call.semicolon_token() { + Some(_) => macro_text_range.end() - TextSize::of(';'), + None => macro_text_range.end(), }; acc.add( @@ -417,6 +431,14 @@ fn main() { #[test] fn test_remove_empty_dbg() { - check_assist(remove_dbg, r#"$0dbg!()"#, r#""#); + check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#); + check_assist( + remove_dbg, + r#"fn foo() { +$0dbg!(); +}"#, + r#"fn foo() { +}"#, + ); } } -- cgit v1.2.3 From 0a5badbcba9fad01ccff60a71de7bc891fd05300 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sat, 27 Mar 2021 11:17:17 +0200 Subject: Replace match on option with if --- crates/ide_assists/src/handlers/remove_dbg.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index 449795efd..ae0f6b83f 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -40,9 +40,10 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { macro_call.syntax().text_range() }; - let macro_end = match macro_call.semicolon_token() { - Some(_) => macro_text_range.end() - TextSize::of(';'), - None => macro_text_range.end(), + let macro_end = if macro_call.semicolon_token().is_some() { + macro_text_range.end() - TextSize::of(';') + } else { + macro_text_range.end() }; acc.add( -- cgit v1.2.3 From 665266bf66d3702880ca8a0e84c42a80c37342a8 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Sat, 27 Mar 2021 17:34:35 +0200 Subject: Replace empty dbg with unit in letexprs, better removal in blocks --- crates/ide_assists/src/handlers/remove_dbg.rs | 85 +++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 13 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index ae0f6b83f..46033fc12 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -24,18 +24,35 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let macro_call = ctx.find_node_at_offset::()?; let new_contents = adjusted_macro_contents(¯o_call)?; - let macro_text_range = if new_contents.is_empty() { - let parent = macro_call.syntax().parent()?; - - let start = parent - .prev_sibling_or_token() - .and_then(|el| { - Some(el.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start()) - }) - .unwrap_or(parent.text_range().start()); - let end = parent.text_range().end(); - - TextRange::new(start, end) + let parent = macro_call.syntax().parent(); + + let macro_text_range = if let Some(it) = parent.as_ref() { + if new_contents.is_empty() { + match_ast! { + match it { + ast::BlockExpr(it) => { + macro_call.syntax() + .prev_sibling_or_token() + .and_then(whitespace_start) + .map(|start| TextRange::new(start, macro_call.syntax().text_range().end())) + .unwrap_or(macro_call.syntax().text_range()) + }, + ast::ExprStmt(it) => { + let start = it + .syntax() + .prev_sibling_or_token() + .and_then(whitespace_start) + .unwrap_or(it.syntax().text_range().start()); + let end = it.syntax().text_range().end(); + + TextRange::new(start, end) + }, + _ => macro_call.syntax().text_range() + } + } + } else { + macro_call.syntax().text_range() + } } else { macro_call.syntax().text_range() }; @@ -51,11 +68,22 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { "Remove dbg!()", macro_text_range, |builder| { - builder.replace(TextRange::new(macro_text_range.start(), macro_end), new_contents); + builder.replace( + TextRange::new(macro_text_range.start(), macro_end), + if new_contents.is_empty() && parent.and_then(ast::LetStmt::cast).is_some() { + ast::make::expr_unit().to_string() + } else { + new_contents + }, + ); }, ) } +fn whitespace_start(it: SyntaxElement) -> Option { + Some(it.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start()) +} + fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option { let contents = get_valid_macrocall_contents(¯o_call, "dbg")?; let macro_text_with_brackets = macro_call.token_tree()?.syntax().text(); @@ -439,6 +467,37 @@ fn main() { $0dbg!(); }"#, r#"fn foo() { +}"#, + ); + check_assist( + remove_dbg, + r#"fn foo() { +let test = $0dbg!(); +}"#, + r#"fn foo() { +let test = (); +}"#, + ); + check_assist( + remove_dbg, + r#"fn foo() { +$0dbg!() +}"#, + r#"fn foo() { +}"#, + ); + check_assist( + remove_dbg, + r#"fn foo() { +let t = { + println!("Hello, world"); + $0dbg!() +}; +}"#, + r#"fn foo() { +let t = { + println!("Hello, world"); +}; }"#, ); } -- cgit v1.2.3 From 8e5611442e93b7a89ff47c2d3e19ceb3d6b75f25 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Mon, 29 Mar 2021 16:52:30 +0300 Subject: Update crates/ide_assists/src/handlers/remove_dbg.rs Apply standard test style fixes Co-authored-by: Aleksey Kladov --- crates/ide_assists/src/handlers/remove_dbg.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index 46033fc12..2bf005913 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -463,11 +463,15 @@ fn main() { check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#); check_assist( remove_dbg, - r#"fn foo() { -$0dbg!(); -}"#, - r#"fn foo() { -}"#, + r#" +fn foo() { + $0dbg!(); +} +"#, + r#" +fn foo() { +} +"#, ); check_assist( remove_dbg, -- cgit v1.2.3 From f9d17c6d7c117b004dbf56d49718c729b9b7f58a Mon Sep 17 00:00:00 2001 From: ivan770 Date: Mon, 29 Mar 2021 16:00:09 +0200 Subject: Apply test style fixes to all empty remove_dbg tests --- crates/ide_assists/src/handlers/remove_dbg.rs | 38 ++++++++++++--------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index 2bf005913..2862cfa9c 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -475,33 +475,29 @@ fn foo() { ); check_assist( remove_dbg, - r#"fn foo() { -let test = $0dbg!(); -}"#, - r#"fn foo() { -let test = (); -}"#, - ); - check_assist( - remove_dbg, - r#"fn foo() { -$0dbg!() + r#" +fn foo() { + let test = $0dbg!(); }"#, - r#"fn foo() { + r#" +fn foo() { + let test = (); }"#, ); check_assist( remove_dbg, - r#"fn foo() { -let t = { - println!("Hello, world"); - $0dbg!() -}; + r#" +fn foo() { + let t = { + println!("Hello, world"); + $0dbg!() + }; }"#, - r#"fn foo() { -let t = { - println!("Hello, world"); -}; + r#" +fn foo() { + let t = { + println!("Hello, world"); + }; }"#, ); } -- cgit v1.2.3