From 5e13e4eba17fe0d55afa76c8d5ff495228283c4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 23:14:31 +0200 Subject: More snippets --- crates/ra_assists/src/handlers/remove_dbg.rs | 39 +++++++--------------------- 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'crates/ra_assists/src/handlers/remove_dbg.rs') diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 8eef578cf..961ee1731 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs @@ -29,26 +29,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let macro_range = macro_call.syntax().text_range(); - // If the cursor is inside the macro call, we'll try to maintain the cursor - // position by subtracting the length of dbg!( from the start of the file - // range, otherwise we'll default to using the start of the macro call - let cursor_pos = { - let file_range = ctx.frange.range; - - let offset_start = file_range - .start() - .checked_sub(macro_range.start()) - .unwrap_or_else(|| TextSize::from(0)); - - let dbg_size = TextSize::of("dbg!("); - - if offset_start > dbg_size { - file_range.start() - dbg_size - } else { - macro_range.start() - } - }; - let macro_content = { let macro_args = macro_call.token_tree()?.syntax().clone(); @@ -58,9 +38,8 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { }; let target = macro_call.syntax().text_range(); - acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |edit| { - edit.replace(macro_range, macro_content); - edit.set_cursor(cursor_pos); + acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |builder| { + builder.replace(macro_range, macro_content); }) } @@ -94,13 +73,13 @@ mod tests { #[test] fn test_remove_dbg() { - check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1"); + check_assist(remove_dbg, "<|>dbg!(1 + 1)", "1 + 1"); - check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)"); + check_assist(remove_dbg, "dbg!<|>((1 + 1))", "(1 + 1)"); - check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1"); + check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 + 1"); - check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1"); + check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = 1 + 1"); check_assist( remove_dbg, @@ -113,7 +92,7 @@ fn foo(n: usize) { ", " fn foo(n: usize) { - if let Some(_) = n.<|>checked_sub(4) { + if let Some(_) = n.checked_sub(4) { // ... } } @@ -122,8 +101,8 @@ fn foo(n: usize) { } #[test] fn test_remove_dbg_with_brackets_and_braces() { - check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1"); - check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1"); + check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); + check_assist(remove_dbg, "dbg!{<|>1 + 1}", "1 + 1"); } #[test] -- cgit v1.2.3