2021-07-16 08:20:50 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2023-03-01 03:43:50 -06:00
|
|
|
ast::{self, make, AstNode, AstToken},
|
|
|
|
match_ast, ted, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
|
2019-02-07 12:00:58 -06:00
|
|
|
};
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2019-10-27 04:22:53 -05:00
|
|
|
|
|
|
|
// Assist: remove_dbg
|
|
|
|
//
|
|
|
|
// Removes `dbg!()` macro call.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
2023-03-01 03:43:50 -06:00
|
|
|
// let x = $0dbg!(42 * dbg!(4 + 2));$0
|
2019-10-27 04:22:53 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
2023-03-01 03:43:50 -06:00
|
|
|
// let x = 42 * (4 + 2);
|
2019-10-27 04:22:53 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2022-11-17 10:39:31 -06:00
|
|
|
let macro_calls = if ctx.has_empty_selection() {
|
2023-03-01 03:43:50 -06:00
|
|
|
vec![ctx.find_node_at_offset::<ast::MacroExpr>()?]
|
2022-11-17 10:39:31 -06:00
|
|
|
} else {
|
|
|
|
ctx.covering_element()
|
|
|
|
.as_node()?
|
|
|
|
.descendants()
|
|
|
|
.filter(|node| ctx.selection_trimmed().contains_range(node.text_range()))
|
2023-03-01 03:43:50 -06:00
|
|
|
// When the selection exactly covers the macro call to be removed, `covering_element()`
|
|
|
|
// returns `ast::MacroCall` instead of its parent `ast::MacroExpr` that we want. So
|
|
|
|
// first try finding `ast::MacroCall`s and then retrieve their parent.
|
2022-11-17 10:39:31 -06:00
|
|
|
.filter_map(ast::MacroCall::cast)
|
2023-03-01 03:43:50 -06:00
|
|
|
.filter_map(|it| it.syntax().parent().and_then(ast::MacroExpr::cast))
|
2022-11-17 10:39:31 -06:00
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let replacements =
|
|
|
|
macro_calls.into_iter().filter_map(compute_dbg_replacement).collect::<Vec<_>>();
|
|
|
|
if replacements.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("remove_dbg", AssistKind::Refactor),
|
|
|
|
"Remove dbg!()",
|
|
|
|
ctx.selection_trimmed(),
|
|
|
|
|builder| {
|
2023-03-01 03:43:50 -06:00
|
|
|
for (range, expr) in replacements {
|
|
|
|
if let Some(expr) = expr {
|
|
|
|
builder.replace(range, expr.to_string());
|
|
|
|
} else {
|
|
|
|
builder.delete(range);
|
|
|
|
}
|
2022-11-17 10:39:31 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-01 03:43:50 -06:00
|
|
|
/// Returns `None` when either
|
|
|
|
/// - macro call is not `dbg!()`
|
|
|
|
/// - any node inside `dbg!()` could not be parsed as an expression
|
|
|
|
/// - (`macro_expr` has no parent - is that possible?)
|
|
|
|
///
|
|
|
|
/// Returns `Some(_, None)` when the macro call should just be removed.
|
|
|
|
fn compute_dbg_replacement(macro_expr: ast::MacroExpr) -> Option<(TextRange, Option<ast::Expr>)> {
|
|
|
|
let macro_call = macro_expr.macro_call()?;
|
2021-07-16 08:20:50 -05:00
|
|
|
let tt = macro_call.token_tree()?;
|
|
|
|
let r_delim = NodeOrToken::Token(tt.right_delimiter_token()?);
|
|
|
|
if macro_call.path()?.segment()?.name_ref()?.text() != "dbg"
|
|
|
|
|| macro_call.excl_token().is_none()
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
2021-03-27 10:34:35 -05:00
|
|
|
|
2021-07-16 08:20:50 -05:00
|
|
|
let mac_input = tt.syntax().children_with_tokens().skip(1).take_while(|it| *it != r_delim);
|
2022-03-10 13:53:50 -06:00
|
|
|
let input_expressions = mac_input.group_by(|tok| tok.kind() == T![,]);
|
2021-07-16 08:20:50 -05:00
|
|
|
let input_expressions = input_expressions
|
|
|
|
.into_iter()
|
2022-12-30 02:30:23 -06:00
|
|
|
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
|
2021-12-28 10:13:30 -06:00
|
|
|
.map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
|
|
|
|
.collect::<Option<Vec<ast::Expr>>>()?;
|
2021-07-16 08:20:50 -05:00
|
|
|
|
2022-04-05 10:42:07 -05:00
|
|
|
let parent = macro_expr.syntax().parent()?;
|
2022-11-17 10:39:31 -06:00
|
|
|
Some(match &*input_expressions {
|
2021-07-16 08:20:50 -05:00
|
|
|
// dbg!()
|
|
|
|
[] => {
|
2021-03-27 10:34:35 -05:00
|
|
|
match_ast! {
|
2021-07-16 08:20:50 -05:00
|
|
|
match parent {
|
2023-03-01 03:43:50 -06:00
|
|
|
ast::StmtList(_) => {
|
2022-04-05 10:42:07 -05:00
|
|
|
let range = macro_expr.syntax().text_range();
|
|
|
|
let range = match whitespace_start(macro_expr.syntax().prev_sibling_or_token()) {
|
2021-07-16 08:20:50 -05:00
|
|
|
Some(start) => range.cover_offset(start),
|
|
|
|
None => range,
|
|
|
|
};
|
2023-03-01 03:43:50 -06:00
|
|
|
(range, None)
|
2021-03-27 10:34:35 -05:00
|
|
|
},
|
|
|
|
ast::ExprStmt(it) => {
|
2021-07-16 08:20:50 -05:00
|
|
|
let range = it.syntax().text_range();
|
|
|
|
let range = match whitespace_start(it.syntax().prev_sibling_or_token()) {
|
|
|
|
Some(start) => range.cover_offset(start),
|
|
|
|
None => range,
|
|
|
|
};
|
2023-03-01 03:43:50 -06:00
|
|
|
(range, None)
|
2021-03-27 10:34:35 -05:00
|
|
|
},
|
2023-03-01 03:43:50 -06:00
|
|
|
_ => (macro_call.syntax().text_range(), Some(make::expr_unit())),
|
2021-03-27 10:34:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 08:20:50 -05:00
|
|
|
// dbg!(expr0)
|
|
|
|
[expr] => {
|
2023-03-01 03:43:50 -06:00
|
|
|
// dbg!(expr, &parent);
|
2021-07-16 08:20:50 -05:00
|
|
|
let wrap = match ast::Expr::cast(parent) {
|
|
|
|
Some(parent) => match (expr, parent) {
|
|
|
|
(ast::Expr::CastExpr(_), ast::Expr::CastExpr(_)) => false,
|
|
|
|
(
|
2023-03-01 03:43:50 -06:00
|
|
|
ast::Expr::BoxExpr(_)
|
|
|
|
| ast::Expr::PrefixExpr(_)
|
|
|
|
| ast::Expr::RefExpr(_)
|
|
|
|
| ast::Expr::MacroExpr(_),
|
2021-07-16 08:20:50 -05:00
|
|
|
ast::Expr::AwaitExpr(_)
|
|
|
|
| ast::Expr::CallExpr(_)
|
|
|
|
| ast::Expr::CastExpr(_)
|
|
|
|
| ast::Expr::FieldExpr(_)
|
|
|
|
| ast::Expr::IndexExpr(_)
|
|
|
|
| ast::Expr::MethodCallExpr(_)
|
|
|
|
| ast::Expr::RangeExpr(_)
|
|
|
|
| ast::Expr::TryExpr(_),
|
|
|
|
) => true,
|
|
|
|
(
|
2023-03-01 03:43:50 -06:00
|
|
|
ast::Expr::BinExpr(_)
|
|
|
|
| ast::Expr::CastExpr(_)
|
|
|
|
| ast::Expr::RangeExpr(_)
|
|
|
|
| ast::Expr::MacroExpr(_),
|
2021-07-16 08:20:50 -05:00
|
|
|
ast::Expr::AwaitExpr(_)
|
|
|
|
| ast::Expr::BinExpr(_)
|
|
|
|
| ast::Expr::CallExpr(_)
|
|
|
|
| ast::Expr::CastExpr(_)
|
|
|
|
| ast::Expr::FieldExpr(_)
|
|
|
|
| ast::Expr::IndexExpr(_)
|
|
|
|
| ast::Expr::MethodCallExpr(_)
|
|
|
|
| ast::Expr::PrefixExpr(_)
|
|
|
|
| ast::Expr::RangeExpr(_)
|
|
|
|
| ast::Expr::RefExpr(_)
|
|
|
|
| ast::Expr::TryExpr(_),
|
|
|
|
) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
None => false,
|
|
|
|
};
|
2023-03-01 03:43:50 -06:00
|
|
|
let expr = replace_nested_dbgs(expr.clone());
|
|
|
|
let expr = if wrap { make::expr_paren(expr) } else { expr.clone_subtree() };
|
|
|
|
(macro_call.syntax().text_range(), Some(expr))
|
2021-07-16 08:20:50 -05:00
|
|
|
}
|
|
|
|
// dbg!(expr0, expr1, ...)
|
2023-03-01 03:43:50 -06:00
|
|
|
exprs => {
|
|
|
|
let exprs = exprs.iter().cloned().map(replace_nested_dbgs);
|
|
|
|
let expr = make::expr_tuple(exprs);
|
|
|
|
(macro_call.syntax().text_range(), Some(expr))
|
|
|
|
}
|
2021-07-16 08:20:50 -05:00
|
|
|
})
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|
|
|
|
|
2023-03-01 03:43:50 -06:00
|
|
|
fn replace_nested_dbgs(expanded: ast::Expr) -> ast::Expr {
|
|
|
|
if let ast::Expr::MacroExpr(mac) = &expanded {
|
|
|
|
// Special-case when `expanded` itself is `dbg!()` since we cannot replace the whole tree
|
|
|
|
// with `ted`. It should be fairly rare as it means the user wrote `dbg!(dbg!(..))` but you
|
|
|
|
// never know how code ends up being!
|
|
|
|
let replaced = if let Some((_, expr_opt)) = compute_dbg_replacement(mac.clone()) {
|
|
|
|
match expr_opt {
|
|
|
|
Some(expr) => expr,
|
|
|
|
None => {
|
|
|
|
stdx::never!("dbg! inside dbg! should not be just removed");
|
|
|
|
expanded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expanded
|
|
|
|
};
|
|
|
|
|
|
|
|
return replaced;
|
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = expanded.clone_for_update();
|
|
|
|
|
|
|
|
// We need to collect to avoid mutation during traversal.
|
|
|
|
let macro_exprs: Vec<_> =
|
|
|
|
expanded.syntax().descendants().filter_map(ast::MacroExpr::cast).collect();
|
|
|
|
|
|
|
|
for mac in macro_exprs {
|
|
|
|
let expr_opt = match compute_dbg_replacement(mac.clone()) {
|
|
|
|
Some((_, expr)) => expr,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(expr) = expr_opt {
|
|
|
|
ted::replace(mac.syntax(), expr.syntax().clone_for_update());
|
|
|
|
} else {
|
|
|
|
ted::remove(mac.syntax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expanded
|
|
|
|
}
|
|
|
|
|
2021-07-16 08:20:50 -05:00
|
|
|
fn whitespace_start(it: Option<SyntaxElement>) -> Option<TextSize> {
|
|
|
|
Some(it?.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start())
|
2020-09-10 06:14:24 -05:00
|
|
|
}
|
|
|
|
|
2019-02-07 12:00:58 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-07-16 08:20:50 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2020-08-19 11:44:33 -05:00
|
|
|
use super::*;
|
|
|
|
|
2021-07-16 08:20:50 -05:00
|
|
|
fn check(ra_fixture_before: &str, ra_fixture_after: &str) {
|
2019-02-07 12:00:58 -06:00
|
|
|
check_assist(
|
|
|
|
remove_dbg,
|
2022-10-10 13:20:27 -05:00
|
|
|
&format!("fn main() {{\n{ra_fixture_before}\n}}"),
|
|
|
|
&format!("fn main() {{\n{ra_fixture_after}\n}}"),
|
2019-02-07 12:00:58 -06:00
|
|
|
);
|
|
|
|
}
|
2020-07-27 15:28:41 -05:00
|
|
|
|
2019-02-07 12:00:58 -06:00
|
|
|
#[test]
|
2021-07-16 08:20:50 -05:00
|
|
|
fn test_remove_dbg() {
|
|
|
|
check("$0dbg!(1 + 1)", "1 + 1");
|
2021-07-16 08:46:29 -05:00
|
|
|
check("dbg!$0(1 + 1)", "1 + 1");
|
2021-07-16 08:20:50 -05:00
|
|
|
check("dbg!(1 $0+ 1)", "1 + 1");
|
|
|
|
check("dbg![$01 + 1]", "1 + 1");
|
|
|
|
check("dbg!{$01 + 1}", "1 + 1");
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
|
|
|
|
#[test]
|
2021-07-16 08:20:50 -05:00
|
|
|
fn test_remove_dbg_not_applicable() {
|
|
|
|
check_assist_not_applicable(remove_dbg, "fn main() {$0vec![1, 2, 3]}");
|
|
|
|
check_assist_not_applicable(remove_dbg, "fn main() {$0dbg(5, 6, 7)}");
|
|
|
|
check_assist_not_applicable(remove_dbg, "fn main() {$0dbg!(5, 6, 7}");
|
2019-02-08 17:34:05 -06:00
|
|
|
}
|
2020-07-24 12:57:40 -05:00
|
|
|
|
|
|
|
#[test]
|
2021-07-16 08:46:29 -05:00
|
|
|
fn test_remove_dbg_keep_semicolon_in_let() {
|
2022-07-08 08:44:49 -05:00
|
|
|
// https://github.com/rust-lang/rust-analyzer/issues/5129#issuecomment-651399779
|
2021-07-16 08:20:50 -05:00
|
|
|
check(
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"let res = $0dbg!(1 * 20); // needless comment"#,
|
2020-07-29 16:12:53 -05:00
|
|
|
r#"let res = 1 * 20; // needless comment"#,
|
|
|
|
);
|
2021-07-16 08:46:29 -05:00
|
|
|
check(r#"let res = $0dbg!(); // needless comment"#, r#"let res = (); // needless comment"#);
|
|
|
|
check(
|
|
|
|
r#"let res = $0dbg!(1, 2); // needless comment"#,
|
|
|
|
r#"let res = (1, 2); // needless comment"#,
|
2020-09-10 06:14:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-24 12:57:40 -05:00
|
|
|
#[test]
|
2021-07-16 08:46:29 -05:00
|
|
|
fn test_remove_dbg_cast_cast() {
|
|
|
|
check(r#"let res = $0dbg!(x as u32) as u32;"#, r#"let res = x as u32 as u32;"#);
|
2020-07-27 15:28:41 -05:00
|
|
|
}
|
2020-10-25 09:12:21 -05:00
|
|
|
|
|
|
|
#[test]
|
2021-07-16 08:46:29 -05:00
|
|
|
fn test_remove_dbg_prefix() {
|
|
|
|
check(r#"let res = $0dbg!(&result).foo();"#, r#"let res = (&result).foo();"#);
|
|
|
|
check(r#"let res = &$0dbg!(&result);"#, r#"let res = &&result;"#);
|
|
|
|
check(r#"let res = $0dbg!(!result) && true;"#, r#"let res = !result && true;"#);
|
2020-10-25 09:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-16 08:46:29 -05:00
|
|
|
fn test_remove_dbg_post_expr() {
|
2021-07-16 08:20:50 -05:00
|
|
|
check(r#"let res = $0dbg!(fut.await).foo();"#, r#"let res = fut.await.foo();"#);
|
2021-07-16 08:46:29 -05:00
|
|
|
check(r#"let res = $0dbg!(result?).foo();"#, r#"let res = result?.foo();"#);
|
|
|
|
check(r#"let res = $0dbg!(foo as u32).foo();"#, r#"let res = (foo as u32).foo();"#);
|
2021-07-16 08:20:50 -05:00
|
|
|
check(r#"let res = $0dbg!(array[3]).foo();"#, r#"let res = array[3].foo();"#);
|
|
|
|
check(r#"let res = $0dbg!(tuple.3).foo();"#, r#"let res = tuple.3.foo();"#);
|
2020-10-25 09:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_dbg_range_expr() {
|
2021-07-16 08:20:50 -05:00
|
|
|
check(r#"let res = $0dbg!(foo..bar).foo();"#, r#"let res = (foo..bar).foo();"#);
|
|
|
|
check(r#"let res = $0dbg!(foo..=bar).foo();"#, r#"let res = (foo..=bar).foo();"#);
|
2020-10-25 09:12:21 -05:00
|
|
|
}
|
2020-11-20 09:44:52 -06:00
|
|
|
|
2021-03-26 09:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_remove_empty_dbg() {
|
2021-03-27 04:10:20 -05:00
|
|
|
check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#);
|
|
|
|
check_assist(
|
|
|
|
remove_dbg,
|
2021-03-29 08:52:30 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
$0dbg!();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
}
|
|
|
|
"#,
|
2021-03-27 10:34:35 -05:00
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
remove_dbg,
|
2021-03-29 09:00:09 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let test = $0dbg!();
|
2021-03-27 10:34:35 -05:00
|
|
|
}"#,
|
2021-03-29 09:00:09 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let test = ();
|
2021-03-27 10:34:35 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
remove_dbg,
|
2021-03-29 09:00:09 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let t = {
|
|
|
|
println!("Hello, world");
|
|
|
|
$0dbg!()
|
|
|
|
};
|
2021-03-27 10:34:35 -05:00
|
|
|
}"#,
|
2021-03-29 09:00:09 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let t = {
|
|
|
|
println!("Hello, world");
|
|
|
|
};
|
2021-03-27 04:10:20 -05:00
|
|
|
}"#,
|
|
|
|
);
|
2021-03-26 09:15:26 -05:00
|
|
|
}
|
2021-07-16 08:46:29 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_multi_dbg() {
|
|
|
|
check(r#"$0dbg!(0, 1)"#, r#"(0, 1)"#);
|
|
|
|
check(r#"$0dbg!(0, (1, 2))"#, r#"(0, (1, 2))"#);
|
|
|
|
}
|
2022-11-17 10:39:31 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
dbg!(0) + $0dbg!(1);
|
|
|
|
dbg!(())$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
dbg!(0) + 1;
|
|
|
|
()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_partial() {
|
|
|
|
check_assist_not_applicable(remove_dbg, r#"$0dbg$0!(0)"#);
|
|
|
|
check_assist_not_applicable(remove_dbg, r#"$0dbg!(0$0)"#);
|
|
|
|
}
|
2023-03-01 03:43:50 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_dbg() {
|
|
|
|
check(
|
|
|
|
r#"$0let x = dbg!(dbg!(dbg!(dbg!(0 + 1)) * 2) + dbg!(3));$0"#,
|
|
|
|
r#"let x = ((0 + 1) * 2) + 3;"#,
|
|
|
|
);
|
|
|
|
check(r#"$0dbg!(10, dbg!(), dbg!(20, 30))$0"#, r#"(10, (), (20, 30))"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_nested_dbg() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
$0dbg!();
|
|
|
|
let x = dbg!(dbg!(dbg!(0 + 1)) + 2) + dbg!(3);
|
|
|
|
dbg!(10, dbg!(), dbg!(20, 30));$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let x = ((0 + 1) + 2) + 3;
|
|
|
|
(10, (), (20, 30));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|