2019-02-07 12:00:58 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode},
|
2020-04-24 16:40:41 -05:00
|
|
|
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() {
|
|
|
|
// <|>dbg!(92);
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// 92;
|
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2019-07-19 03:24:41 -05:00
|
|
|
if !is_valid_macrocall(¯o_call, "dbg")? {
|
2019-02-07 12:00:58 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-07-20 04:58:27 -05:00
|
|
|
let macro_range = macro_call.syntax().text_range();
|
2019-02-07 12:00:58 -06:00
|
|
|
|
|
|
|
let macro_content = {
|
2019-07-19 03:24:41 -05:00
|
|
|
let macro_args = macro_call.token_tree()?.syntax().clone();
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2019-07-19 12:55:32 -05:00
|
|
|
let text = macro_args.text();
|
2020-04-24 16:40:41 -05:00
|
|
|
let without_parens = TextSize::of('(')..text.len() - TextSize::of(')');
|
2019-07-19 12:55:32 -05:00
|
|
|
text.slice(without_parens).to_string()
|
2019-02-07 12:00:58 -06:00
|
|
|
};
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = macro_call.syntax().text_range();
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| {
|
2020-05-20 16:14:31 -05:00
|
|
|
builder.replace(macro_range, macro_content);
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verifies that the given macro_call actually matches the given name
|
|
|
|
/// and contains proper ending tokens
|
|
|
|
fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<bool> {
|
|
|
|
let path = macro_call.path()?;
|
|
|
|
let name_ref = path.segment()?.name_ref()?;
|
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
// Make sure it is actually a dbg-macro call, dbg followed by !
|
2019-03-30 05:25:53 -05:00
|
|
|
let excl = path.syntax().next_sibling_or_token()?;
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2019-05-15 07:35:47 -05:00
|
|
|
if name_ref.text() != macro_name || excl.kind() != T![!] {
|
2019-02-07 12:00:58 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-07-19 03:24:41 -05:00
|
|
|
let node = macro_call.token_tree()?.syntax().clone();
|
2019-03-30 05:25:53 -05:00
|
|
|
let first_child = node.first_child_or_token()?;
|
|
|
|
let last_child = node.last_child_or_token()?;
|
2019-02-07 12:00:58 -06:00
|
|
|
|
|
|
|
match (first_child.kind(), last_child.kind()) {
|
2019-05-15 07:35:47 -05:00
|
|
|
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true),
|
2019-02-07 12:00:58 -06:00
|
|
|
_ => Some(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
2019-02-07 12:00:58 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_dbg() {
|
2020-05-20 16:14:31 -05:00
|
|
|
check_assist(remove_dbg, "<|>dbg!(1 + 1)", "1 + 1");
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2020-05-20 16:14:31 -05:00
|
|
|
check_assist(remove_dbg, "dbg!<|>((1 + 1))", "(1 + 1)");
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2020-05-20 16:14:31 -05:00
|
|
|
check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 + 1");
|
2019-02-07 12:00:58 -06:00
|
|
|
|
2020-05-20 16:14:31 -05:00
|
|
|
check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = 1 + 1");
|
2019-02-07 12:00:58 -06:00
|
|
|
|
|
|
|
check_assist(
|
|
|
|
remove_dbg,
|
|
|
|
"
|
|
|
|
fn foo(n: usize) {
|
|
|
|
if let Some(_) = dbg!(n.<|>checked_sub(4)) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn foo(n: usize) {
|
2020-05-20 16:14:31 -05:00
|
|
|
if let Some(_) = n.checked_sub(4) {
|
2019-02-07 12:00:58 -06:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_remove_dbg_with_brackets_and_braces() {
|
2020-05-20 16:14:31 -05:00
|
|
|
check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1");
|
|
|
|
check_assist(remove_dbg, "dbg!{<|>1 + 1}", "1 + 1");
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_dbg_not_applicable() {
|
|
|
|
check_assist_not_applicable(remove_dbg, "<|>vec![1, 2, 3]");
|
|
|
|
check_assist_not_applicable(remove_dbg, "<|>dbg(5, 6, 7)");
|
|
|
|
check_assist_not_applicable(remove_dbg, "<|>dbg!(5, 6, 7");
|
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_dbg_target() {
|
|
|
|
check_assist_target(
|
|
|
|
remove_dbg,
|
|
|
|
"
|
|
|
|
fn foo(n: usize) {
|
|
|
|
if let Some(_) = dbg!(n.<|>checked_sub(4)) {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"dbg!(n.checked_sub(4))",
|
|
|
|
);
|
|
|
|
}
|
2020-07-24 12:57:40 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_dbg_leave_semicolon() {
|
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779
|
|
|
|
// not quite though
|
|
|
|
let code = "
|
|
|
|
let res = <|>dbg!(1 * 20); // needless comment
|
|
|
|
";
|
|
|
|
let expected = "
|
|
|
|
let res = 1 * 20; // needless comment
|
|
|
|
";
|
|
|
|
check_assist(remove_dbg, code, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_dbg_keep_expression() {
|
|
|
|
let code = "
|
|
|
|
let res = <|>dbg!(a + b).foo();";
|
|
|
|
let expected = "let res = (a + b).foo();";
|
|
|
|
check_assist(remove_dbg, code, expected);
|
|
|
|
}
|
2019-02-07 12:00:58 -06:00
|
|
|
}
|