diff options
Diffstat (limited to 'crates/assists/src/handlers/remove_dbg.rs')
-rw-r--r-- | crates/assists/src/handlers/remove_dbg.rs | 144 |
1 files changed, 108 insertions, 36 deletions
diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index 4e252edf0..a8ab2aecc 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use syntax::{ | 1 | use syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | TextRange, TextSize, T, | 3 | SyntaxElement, TextRange, TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -22,62 +22,108 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; | |||
22 | // ``` | 22 | // ``` |
23 | pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 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 | let new_contents = adjusted_macro_contents(¯o_call)?; | ||
25 | 26 | ||
26 | if !is_valid_macrocall(¯o_call, "dbg")? { | 27 | let macro_text_range = macro_call.syntax().text_range(); |
27 | return None; | ||
28 | } | ||
29 | |||
30 | let is_leaf = macro_call.syntax().next_sibling().is_none(); | ||
31 | |||
32 | let macro_end = if macro_call.semicolon_token().is_some() { | 28 | let macro_end = if macro_call.semicolon_token().is_some() { |
33 | macro_call.syntax().text_range().end() - TextSize::of(';') | 29 | macro_text_range.end() - TextSize::of(';') |
34 | } else { | 30 | } else { |
35 | macro_call.syntax().text_range().end() | 31 | macro_text_range.end() |
36 | }; | 32 | }; |
37 | 33 | ||
38 | // macro_range determines what will be deleted and replaced with macro_content | 34 | acc.add( |
39 | let macro_range = TextRange::new(macro_call.syntax().text_range().start(), macro_end); | 35 | AssistId("remove_dbg", AssistKind::Refactor), |
40 | let paste_instead_of_dbg = { | 36 | "Remove dbg!()", |
41 | let text = macro_call.token_tree()?.syntax().text(); | 37 | macro_text_range, |
42 | 38 | |builder| { | |
43 | // leafiness determines if we should include the parenthesis or not | 39 | builder.replace(TextRange::new(macro_text_range.start(), macro_end), new_contents); |
44 | let slice_index: TextRange = if is_leaf { | 40 | }, |
45 | // leaf means - we can extract the contents of the dbg! in text | 41 | ) |
46 | TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')) | 42 | } |
47 | } else { | ||
48 | // not leaf - means we should keep the parens | ||
49 | TextRange::up_to(text.len()) | ||
50 | }; | ||
51 | text.slice(slice_index).to_string() | ||
52 | }; | ||
53 | 43 | ||
54 | let target = macro_call.syntax().text_range(); | 44 | fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> { |
55 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { | 45 | let contents = get_valid_macrocall_contents(¯o_call, "dbg")?; |
56 | builder.replace(macro_range, paste_instead_of_dbg); | 46 | let macro_text_with_brackets = macro_call.token_tree()?.syntax().text(); |
47 | let macro_text_in_brackets = macro_text_with_brackets.slice(TextRange::new( | ||
48 | TextSize::of('('), | ||
49 | macro_text_with_brackets.len() - TextSize::of(')'), | ||
50 | )); | ||
51 | |||
52 | let is_leaf = macro_call.syntax().next_sibling().is_none(); | ||
53 | Some(if !is_leaf && needs_parentheses_around_macro_contents(contents) { | ||
54 | format!("({})", macro_text_in_brackets) | ||
55 | } else { | ||
56 | macro_text_in_brackets.to_string() | ||
57 | }) | 57 | }) |
58 | } | 58 | } |
59 | 59 | ||
60 | /// Verifies that the given macro_call actually matches the given name | 60 | /// Verifies that the given macro_call actually matches the given name |
61 | /// and contains proper ending tokens | 61 | /// and contains proper ending tokens, then returns the contents between the ending tokens |
62 | fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<bool> { | 62 | fn get_valid_macrocall_contents( |
63 | macro_call: &ast::MacroCall, | ||
64 | macro_name: &str, | ||
65 | ) -> Option<Vec<SyntaxElement>> { | ||
63 | let path = macro_call.path()?; | 66 | let path = macro_call.path()?; |
64 | let name_ref = path.segment()?.name_ref()?; | 67 | let name_ref = path.segment()?.name_ref()?; |
65 | 68 | ||
66 | // Make sure it is actually a dbg-macro call, dbg followed by ! | 69 | // Make sure it is actually a dbg-macro call, dbg followed by ! |
67 | let excl = path.syntax().next_sibling_or_token()?; | 70 | let excl = path.syntax().next_sibling_or_token()?; |
68 | |||
69 | if name_ref.text() != macro_name || excl.kind() != T![!] { | 71 | if name_ref.text() != macro_name || excl.kind() != T![!] { |
70 | return None; | 72 | return None; |
71 | } | 73 | } |
72 | 74 | ||
73 | let node = macro_call.token_tree()?.syntax().clone(); | 75 | let mut children_with_tokens = macro_call.token_tree()?.syntax().children_with_tokens(); |
74 | let first_child = node.first_child_or_token()?; | 76 | let first_child = children_with_tokens.next()?; |
75 | let last_child = node.last_child_or_token()?; | 77 | let mut contents_between_brackets = children_with_tokens.collect::<Vec<_>>(); |
78 | let last_child = contents_between_brackets.pop()?; | ||
79 | |||
80 | if contents_between_brackets.is_empty() { | ||
81 | None | ||
82 | } else { | ||
83 | match (first_child.kind(), last_child.kind()) { | ||
84 | (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => { | ||
85 | Some(contents_between_brackets) | ||
86 | } | ||
87 | _ => None, | ||
88 | } | ||
89 | } | ||
90 | } | ||
76 | 91 | ||
77 | match (first_child.kind(), last_child.kind()) { | 92 | fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -> bool { |
78 | (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true), | 93 | if macro_contents.len() < 2 { |
79 | _ => Some(false), | 94 | return false; |
80 | } | 95 | } |
96 | let mut unpaired_brackets_in_contents = Vec::new(); | ||
97 | for element in macro_contents { | ||
98 | match element.kind() { | ||
99 | T!['('] | T!['['] | T!['{'] => unpaired_brackets_in_contents.push(element), | ||
100 | T![')'] => { | ||
101 | if !matches!(unpaired_brackets_in_contents.pop(), Some(correct_bracket) if correct_bracket.kind() == T!['(']) | ||
102 | { | ||
103 | return true; | ||
104 | } | ||
105 | } | ||
106 | T![']'] => { | ||
107 | if !matches!(unpaired_brackets_in_contents.pop(), Some(correct_bracket) if correct_bracket.kind() == T!['[']) | ||
108 | { | ||
109 | return true; | ||
110 | } | ||
111 | } | ||
112 | T!['}'] => { | ||
113 | if !matches!(unpaired_brackets_in_contents.pop(), Some(correct_bracket) if correct_bracket.kind() == T!['{']) | ||
114 | { | ||
115 | return true; | ||
116 | } | ||
117 | } | ||
118 | symbol_kind => { | ||
119 | let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); | ||
120 | if symbol_not_in_bracket && symbol_kind.is_punct() { | ||
121 | return true; | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | !unpaired_brackets_in_contents.is_empty() | ||
81 | } | 127 | } |
82 | 128 | ||
83 | #[cfg(test)] | 129 | #[cfg(test)] |
@@ -157,12 +203,38 @@ fn foo(n: usize) { | |||
157 | } | 203 | } |
158 | 204 | ||
159 | #[test] | 205 | #[test] |
206 | fn remove_dbg_from_non_leaf_simple_expression() { | ||
207 | check_assist( | ||
208 | remove_dbg, | ||
209 | " | ||
210 | fn main() { | ||
211 | let mut a = 1; | ||
212 | while dbg!<|>(a) < 10000 { | ||
213 | a += 1; | ||
214 | } | ||
215 | } | ||
216 | ", | ||
217 | " | ||
218 | fn main() { | ||
219 | let mut a = 1; | ||
220 | while a < 10000 { | ||
221 | a += 1; | ||
222 | } | ||
223 | } | ||
224 | ", | ||
225 | ); | ||
226 | } | ||
227 | |||
228 | #[test] | ||
160 | fn test_remove_dbg_keep_expression() { | 229 | fn test_remove_dbg_keep_expression() { |
161 | check_assist( | 230 | check_assist( |
162 | remove_dbg, | 231 | remove_dbg, |
163 | r#"let res = <|>dbg!(a + b).foo();"#, | 232 | r#"let res = <|>dbg!(a + b).foo();"#, |
164 | r#"let res = (a + b).foo();"#, | 233 | r#"let res = (a + b).foo();"#, |
165 | ); | 234 | ); |
235 | |||
236 | check_assist(remove_dbg, r#"let res = <|>dbg!(2 + 2) * 5"#, r#"let res = (2 + 2) * 5"#); | ||
237 | check_assist(remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"#, r#"let res = (2 + 2) * 5"#); | ||
166 | } | 238 | } |
167 | 239 | ||
168 | #[test] | 240 | #[test] |