diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/remove_dbg.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/remove_dbg.rs | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 4e5eb4350..961ee1731 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs | |||
@@ -3,7 +3,7 @@ use ra_syntax::{ | |||
3 | TextSize, T, | 3 | TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{Assist, AssistCtx, AssistId}; | 6 | use crate::{AssistContext, AssistId, Assists}; |
7 | 7 | ||
8 | // Assist: remove_dbg | 8 | // Assist: remove_dbg |
9 | // | 9 | // |
@@ -20,7 +20,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
20 | // 92; | 20 | // 92; |
21 | // } | 21 | // } |
22 | // ``` | 22 | // ``` |
23 | pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option<Assist> { | 23 | pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
24 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; | 24 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; |
25 | 25 | ||
26 | if !is_valid_macrocall(¯o_call, "dbg")? { | 26 | if !is_valid_macrocall(¯o_call, "dbg")? { |
@@ -29,26 +29,6 @@ pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option<Assist> { | |||
29 | 29 | ||
30 | let macro_range = macro_call.syntax().text_range(); | 30 | let macro_range = macro_call.syntax().text_range(); |
31 | 31 | ||
32 | // If the cursor is inside the macro call, we'll try to maintain the cursor | ||
33 | // position by subtracting the length of dbg!( from the start of the file | ||
34 | // range, otherwise we'll default to using the start of the macro call | ||
35 | let cursor_pos = { | ||
36 | let file_range = ctx.frange.range; | ||
37 | |||
38 | let offset_start = file_range | ||
39 | .start() | ||
40 | .checked_sub(macro_range.start()) | ||
41 | .unwrap_or_else(|| TextSize::from(0)); | ||
42 | |||
43 | let dbg_size = TextSize::of("dbg!("); | ||
44 | |||
45 | if offset_start > dbg_size { | ||
46 | file_range.start() - dbg_size | ||
47 | } else { | ||
48 | macro_range.start() | ||
49 | } | ||
50 | }; | ||
51 | |||
52 | let macro_content = { | 32 | let macro_content = { |
53 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 33 | let macro_args = macro_call.token_tree()?.syntax().clone(); |
54 | 34 | ||
@@ -57,10 +37,9 @@ pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option<Assist> { | |||
57 | text.slice(without_parens).to_string() | 37 | text.slice(without_parens).to_string() |
58 | }; | 38 | }; |
59 | 39 | ||
60 | ctx.add_assist(AssistId("remove_dbg"), "Remove dbg!()", |edit| { | 40 | let target = macro_call.syntax().text_range(); |
61 | edit.target(macro_call.syntax().text_range()); | 41 | acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |builder| { |
62 | edit.replace(macro_range, macro_content); | 42 | builder.replace(macro_range, macro_content); |
63 | edit.set_cursor(cursor_pos); | ||
64 | }) | 43 | }) |
65 | } | 44 | } |
66 | 45 | ||
@@ -90,17 +69,17 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<b | |||
90 | #[cfg(test)] | 69 | #[cfg(test)] |
91 | mod tests { | 70 | mod tests { |
92 | use super::*; | 71 | use super::*; |
93 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; | 72 | use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; |
94 | 73 | ||
95 | #[test] | 74 | #[test] |
96 | fn test_remove_dbg() { | 75 | fn test_remove_dbg() { |
97 | check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1"); | 76 | check_assist(remove_dbg, "<|>dbg!(1 + 1)", "1 + 1"); |
98 | 77 | ||
99 | check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)"); | 78 | check_assist(remove_dbg, "dbg!<|>((1 + 1))", "(1 + 1)"); |
100 | 79 | ||
101 | check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1"); | 80 | check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 + 1"); |
102 | 81 | ||
103 | check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1"); | 82 | check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = 1 + 1"); |
104 | 83 | ||
105 | check_assist( | 84 | check_assist( |
106 | remove_dbg, | 85 | remove_dbg, |
@@ -113,7 +92,7 @@ fn foo(n: usize) { | |||
113 | ", | 92 | ", |
114 | " | 93 | " |
115 | fn foo(n: usize) { | 94 | fn foo(n: usize) { |
116 | if let Some(_) = n.<|>checked_sub(4) { | 95 | if let Some(_) = n.checked_sub(4) { |
117 | // ... | 96 | // ... |
118 | } | 97 | } |
119 | } | 98 | } |
@@ -122,8 +101,8 @@ fn foo(n: usize) { | |||
122 | } | 101 | } |
123 | #[test] | 102 | #[test] |
124 | fn test_remove_dbg_with_brackets_and_braces() { | 103 | fn test_remove_dbg_with_brackets_and_braces() { |
125 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1"); | 104 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); |
126 | check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1"); | 105 | check_assist(remove_dbg, "dbg!{<|>1 + 1}", "1 + 1"); |
127 | } | 106 | } |
128 | 107 | ||
129 | #[test] | 108 | #[test] |