diff options
author | petr-tik <[email protected]> | 2020-07-27 22:17:15 +0100 |
---|---|---|
committer | petr-tik <[email protected]> | 2020-07-27 22:17:15 +0100 |
commit | 6ea28c377947b7e26efcb107d0dbbd804ed5e27f (patch) | |
tree | c3fce05f13e463ebcf7d9721f29dc5dcf2b84540 | |
parent | 01bdeaad71ea87e2e989fc9ded06f69805929e42 (diff) |
Fixed #5129
Addresses two issues:
- keep the parens from dbg!() in case the call is chained or there is
semantic difference if parens are excluded
- Exclude the semicolon after the dbg!(); by checking if it was
accidentally included in the macro_call
investigated, but decided against:
fix ast::MacroCall extraction to never include semicolons at the end -
this logic lives in rowan.
Defensively shorten the macro_range if there is a semicolon token.
Deleted unneccessary temp variable macro_args
Renamed macro_content to "paste_instead_of_dbg", because it isn't a
simple extraction of text inside dbg!() anymore
-rw-r--r-- | crates/ra_assists/src/handlers/remove_dbg.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 1116d2a2c..c43b89661 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | TextSize, T, | 3 | TextRange, TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -27,19 +27,32 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | 29 | ||
30 | let macro_range = macro_call.syntax().text_range(); | 30 | let semicolon_on_end = macro_call.semicolon_token().is_some(); |
31 | let is_leaf = macro_call.syntax().next_sibling().is_none(); | ||
31 | 32 | ||
32 | let macro_content = { | 33 | let macro_end = match semicolon_on_end { |
33 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 34 | true => macro_call.syntax().text_range().end() - TextSize::of(';'), |
35 | false => macro_call.syntax().text_range().end(), | ||
36 | }; | ||
34 | 37 | ||
35 | let text = macro_args.text(); | 38 | // macro_range determines what will be deleted and replaced with macro_content |
36 | let without_parens = TextSize::of('(')..text.len() - TextSize::of(')'); | 39 | let macro_range = TextRange::new(macro_call.syntax().text_range().start(), macro_end); |
37 | text.slice(without_parens).to_string() | 40 | let paste_instead_of_dbg = { |
41 | let text = macro_call.token_tree()?.syntax().text(); | ||
42 | |||
43 | // leafines determines if we should include the parenthesis or not | ||
44 | let slice_index: TextRange = match is_leaf { | ||
45 | // leaf means - we can extract the contents of the dbg! in text | ||
46 | true => TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')), | ||
47 | // not leaf - means we should keep the parens | ||
48 | false => TextRange::new(TextSize::from(0 as u32), text.len()), | ||
49 | }; | ||
50 | text.slice(slice_index).to_string() | ||
38 | }; | 51 | }; |
39 | 52 | ||
40 | let target = macro_call.syntax().text_range(); | 53 | let target = macro_call.syntax().text_range(); |
41 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { | 54 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { |
42 | builder.replace(macro_range, macro_content); | 55 | builder.replace(macro_range, paste_instead_of_dbg); |
43 | }) | 56 | }) |
44 | } | 57 | } |
45 | 58 | ||
@@ -132,6 +145,8 @@ fn foo(n: usize) { | |||
132 | fn test_remove_dbg_keep_semicolon() { | 145 | fn test_remove_dbg_keep_semicolon() { |
133 | // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779 | 146 | // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779 |
134 | // not quite though | 147 | // not quite though |
148 | // adding a comment at the end of the line makes | ||
149 | // the ast::MacroCall to include the semicolon at the end | ||
135 | let code = " | 150 | let code = " |
136 | let res = <|>dbg!(1 * 20); // needless comment | 151 | let res = <|>dbg!(1 * 20); // needless comment |
137 | "; | 152 | "; |