1 use hir::db::HirDatabase;
6 L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL
9 use crate::{AssistCtx, Assist};
11 pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let macro_call = ctx.node_at_offset::<ast::MacroCall>()?;
14 if !is_valid_macrocall(macro_call, "dbg")? {
18 let macro_range = macro_call.syntax().range();
20 // If the cursor is inside the macrocall, we'll try to maintain
21 // the cursor position by subtracting the length of dbg!( from the start
22 // of the filerange, otherwise we'll default to using the start of the macrocall
24 let file_range = ctx.frange.range;
26 let offset_start = file_range
28 .checked_sub(macro_range.start())
29 .unwrap_or_else(|| TextUnit::from(0));
31 let dbg_size = TextUnit::of_str("dbg!(");
33 if offset_start > dbg_size {
34 file_range.start() - dbg_size
41 let macro_args = macro_call.token_tree()?.syntax();
42 let range = macro_args.range();
43 let start = range.start() + TextUnit::of_char('(');
44 let end = range.end() - TextUnit::of_char(')');
46 macro_args.text().slice(start..end).to_string()
49 ctx.add_action("remove dbg!()", |edit| {
50 edit.target(macro_call.syntax().range());
51 edit.replace(macro_range, macro_content);
52 edit.set_cursor(cursor_pos);
58 /// Verifies that the given macro_call actually matches the given name
59 /// and contains proper ending tokens
60 fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<bool> {
61 let path = macro_call.path()?;
62 let name_ref = path.segment()?.name_ref()?;
64 // Make sure it is actually a dbg-macrocall, dbg followed by !
65 let excl = path.syntax().next_sibling()?;
67 if name_ref.text() != macro_name || excl.kind() != EXCL {
71 let node = macro_call.token_tree()?.syntax();
72 let first_child = node.first_child()?;
73 let last_child = node.last_child()?;
75 match (first_child.kind(), last_child.kind()) {
76 (L_PAREN, R_PAREN) | (L_BRACK, R_BRACK) | (L_CURLY, R_CURLY) => Some(true),
84 use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
87 fn test_remove_dbg() {
88 check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1");
90 check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)");
92 check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1");
94 check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1");
100 if let Some(_) = dbg!(n.<|>checked_sub(4)) {
107 if let Some(_) = n.<|>checked_sub(4) {
115 fn test_remove_dbg_with_brackets_and_braces() {
116 check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1");
117 check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1");
121 fn test_remove_dbg_not_applicable() {
122 check_assist_not_applicable(remove_dbg, "<|>vec![1, 2, 3]");
123 check_assist_not_applicable(remove_dbg, "<|>dbg(5, 6, 7)");
124 check_assist_not_applicable(remove_dbg, "<|>dbg!(5, 6, 7");
128 fn remove_dbg_target() {
133 if let Some(_) = dbg!(n.<|>checked_sub(4)) {
138 "dbg!(n.checked_sub(4))",