From 6675d4c57641135a73658d9ba66b78205bb45742 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 25 Oct 2020 14:27:32 +0100 Subject: Don't keep parens around with remove-dbg when encountering method chaining --- crates/assists/src/handlers/remove_dbg.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index e10616779..e7072d2b9 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs @@ -119,6 +119,7 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec) - let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); if symbol_not_in_bracket && symbol_kind != SyntaxKind::COLON + && symbol_kind != SyntaxKind::DOT && symbol_kind.is_punct() { return true; @@ -242,6 +243,25 @@ fn main() { check_assist(remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"#, r#"let res = (2 + 2) * 5"#); } + #[test] + fn test_remove_dbg_method_chaining() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(foo().bar()).baz();"#, + r#"let res = foo().bar().baz();"#, + ); + check_assist( + remove_dbg, + r#"let res = <|>dbg!(foo.bar()).baz();"#, + r#"let res = foo.bar().baz();"#, + ); + } + + #[test] + fn test_remove_dbg_field_chaining() { + check_assist(remove_dbg, r#"let res = <|>dbg!(foo.bar).baz;"#, r#"let res = foo.bar.baz;"#); + } + #[test] fn test_remove_dbg_from_inside_fn() { check_assist_target( -- cgit v1.2.3 From 3182c067521ced88f546900cca3c6e9b82d18df6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 25 Oct 2020 15:12:21 +0100 Subject: Don't keep parens around in remove-dbg --- crates/assists/src/handlers/remove_dbg.rs | 62 ++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index e7072d2b9..be3afa250 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs @@ -118,9 +118,13 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec) - symbol_kind => { let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); if symbol_not_in_bracket + // paths && symbol_kind != SyntaxKind::COLON + // field/method access && symbol_kind != SyntaxKind::DOT - && symbol_kind.is_punct() + // try operator + && symbol_kind != SyntaxKind::QUESTION + && (symbol_kind.is_punct() || symbol_kind == SyntaxKind::AS_KW) { return true; } @@ -300,4 +304,60 @@ fn main() { }"#, ); } + + #[test] + fn test_remove_dbg_try_expr() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(result?).foo();"#, + r#"let res = result?.foo();"#, + ); + } + + #[test] + fn test_remove_dbg_await_expr() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(fut.await).foo();"#, + r#"let res = fut.await.foo();"#, + ); + } + + #[test] + fn test_remove_dbg_as_cast() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(3 as usize).foo();"#, + r#"let res = (3 as usize).foo();"#, + ); + } + + #[test] + fn test_remove_dbg_index_expr() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(array[3]).foo();"#, + r#"let res = array[3].foo();"#, + ); + check_assist( + remove_dbg, + r#"let res = <|>dbg!(tuple.3).foo();"#, + r#"let res = tuple.3.foo();"#, + ); + } + + #[test] + #[ignore] // FIXME: we encounter SyntaxKind::DOT instead of SyntaxKind::DOT2 causing this to fail + fn test_remove_dbg_range_expr() { + check_assist( + remove_dbg, + r#"let res = <|>dbg!(foo..bar).foo();"#, + r#"let res = (foo..bar).foo();"#, + ); + check_assist( + remove_dbg, + r#"let res = <|>dbg!(foo..=bar).foo();"#, + r#"let res = (foo..=bar).foo();"#, + ); + } } -- cgit v1.2.3 From 551bf65e6d035f95e77b116679a4622d6cb590db Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 25 Oct 2020 15:36:02 +0100 Subject: Keep parens around in remove-dbg for range expressions --- crates/assists/src/handlers/remove_dbg.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index be3afa250..9731344b8 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs @@ -93,8 +93,9 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec) - if macro_contents.len() < 2 { return false; } + let mut macro_contents = macro_contents.into_iter().peekable(); let mut unpaired_brackets_in_contents = Vec::new(); - for element in macro_contents { + while let Some(element) = macro_contents.next() { match element.kind() { T!['('] | T!['['] | T!['{'] => unpaired_brackets_in_contents.push(element), T![')'] => { @@ -118,12 +119,13 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec) - symbol_kind => { let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); if symbol_not_in_bracket - // paths - && symbol_kind != SyntaxKind::COLON - // field/method access - && symbol_kind != SyntaxKind::DOT - // try operator - && symbol_kind != SyntaxKind::QUESTION + && symbol_kind != SyntaxKind::COLON // paths + && (symbol_kind != SyntaxKind::DOT // field/method access + || macro_contents // range expressions consist of two SyntaxKind::Dot in macro invocations + .peek() + .map(|element| element.kind() == SyntaxKind::DOT) + .unwrap_or(false)) + && symbol_kind != SyntaxKind::QUESTION // try operator && (symbol_kind.is_punct() || symbol_kind == SyntaxKind::AS_KW) { return true; @@ -347,7 +349,6 @@ fn main() { } #[test] - #[ignore] // FIXME: we encounter SyntaxKind::DOT instead of SyntaxKind::DOT2 causing this to fail fn test_remove_dbg_range_expr() { check_assist( remove_dbg, -- cgit v1.2.3