diff options
-rw-r--r-- | crates/ra_assists/src/handlers/remove_dbg.rs | 94 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 5 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/main.rs | 8 |
3 files changed, 92 insertions, 15 deletions
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index a616cca57..9430ce1b5 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,33 @@ 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 is_leaf = macro_call.syntax().next_sibling().is_none(); |
31 | 31 | ||
32 | let macro_content = { | 32 | let macro_end = if macro_call.semicolon_token().is_some() { |
33 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 33 | macro_call.syntax().text_range().end() - TextSize::of(';') |
34 | } else { | ||
35 | 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 | // leafiness determines if we should include the parenthesis or not | ||
44 | let slice_index: TextRange = if is_leaf { | ||
45 | // leaf means - we can extract the contents of the dbg! in text | ||
46 | TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')) | ||
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() | ||
38 | }; | 52 | }; |
39 | 53 | ||
40 | let target = macro_call.syntax().text_range(); | 54 | let target = macro_call.syntax().text_range(); |
41 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { | 55 | acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| { |
42 | builder.replace(macro_range, macro_content); | 56 | builder.replace(macro_range, paste_instead_of_dbg); |
43 | }) | 57 | }) |
44 | } | 58 | } |
45 | 59 | ||
@@ -99,6 +113,7 @@ fn foo(n: usize) { | |||
99 | ", | 113 | ", |
100 | ); | 114 | ); |
101 | } | 115 | } |
116 | |||
102 | #[test] | 117 | #[test] |
103 | fn test_remove_dbg_with_brackets_and_braces() { | 118 | fn test_remove_dbg_with_brackets_and_braces() { |
104 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); | 119 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); |
@@ -113,7 +128,7 @@ fn foo(n: usize) { | |||
113 | } | 128 | } |
114 | 129 | ||
115 | #[test] | 130 | #[test] |
116 | fn remove_dbg_target() { | 131 | fn test_remove_dbg_target() { |
117 | check_assist_target( | 132 | check_assist_target( |
118 | remove_dbg, | 133 | remove_dbg, |
119 | " | 134 | " |
@@ -126,4 +141,65 @@ fn foo(n: usize) { | |||
126 | "dbg!(n.checked_sub(4))", | 141 | "dbg!(n.checked_sub(4))", |
127 | ); | 142 | ); |
128 | } | 143 | } |
144 | |||
145 | #[test] | ||
146 | fn test_remove_dbg_keep_semicolon() { | ||
147 | // https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779 | ||
148 | // not quite though | ||
149 | // adding a comment at the end of the line makes | ||
150 | // the ast::MacroCall to include the semicolon at the end | ||
151 | check_assist( | ||
152 | remove_dbg, | ||
153 | r#"let res = <|>dbg!(1 * 20); // needless comment"#, | ||
154 | r#"let res = 1 * 20; // needless comment"#, | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | #[test] | ||
159 | fn test_remove_dbg_keep_expression() { | ||
160 | check_assist( | ||
161 | remove_dbg, | ||
162 | r#"let res = <|>dbg!(a + b).foo();"#, | ||
163 | r#"let res = (a + b).foo();"#, | ||
164 | ); | ||
165 | } | ||
166 | |||
167 | #[test] | ||
168 | fn test_remove_dbg_from_inside_fn() { | ||
169 | check_assist_target( | ||
170 | remove_dbg, | ||
171 | r#" | ||
172 | fn square(x: u32) -> u32 { | ||
173 | x * x | ||
174 | } | ||
175 | |||
176 | fn main() { | ||
177 | let x = square(dbg<|>!(5 + 10)); | ||
178 | println!("{}", x); | ||
179 | }"#, | ||
180 | "dbg!(5 + 10)", | ||
181 | ); | ||
182 | |||
183 | check_assist( | ||
184 | remove_dbg, | ||
185 | r#" | ||
186 | fn square(x: u32) -> u32 { | ||
187 | x * x | ||
188 | } | ||
189 | |||
190 | fn main() { | ||
191 | let x = square(dbg<|>!(5 + 10)); | ||
192 | println!("{}", x); | ||
193 | }"#, | ||
194 | r#" | ||
195 | fn square(x: u32) -> u32 { | ||
196 | x * x | ||
197 | } | ||
198 | |||
199 | fn main() { | ||
200 | let x = square(5 + 10); | ||
201 | println!("{}", x); | ||
202 | }"#, | ||
203 | ); | ||
204 | } | ||
129 | } | 205 | } |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 1350bd400..87a1367ac 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -469,12 +469,12 @@ pub(crate) fn handle_runnables( | |||
469 | res.push(runnable); | 469 | res.push(runnable); |
470 | } | 470 | } |
471 | 471 | ||
472 | // Add `cargo check` and `cargo test` for the whole package | 472 | // Add `cargo check` and `cargo test` for all targets of the whole package |
473 | match cargo_spec { | 473 | match cargo_spec { |
474 | Some(spec) => { | 474 | Some(spec) => { |
475 | for &cmd in ["check", "test"].iter() { | 475 | for &cmd in ["check", "test"].iter() { |
476 | res.push(lsp_ext::Runnable { | 476 | res.push(lsp_ext::Runnable { |
477 | label: format!("cargo {} -p {}", cmd, spec.package), | 477 | label: format!("cargo {} -p {} --all-targets", cmd, spec.package), |
478 | location: None, | 478 | location: None, |
479 | kind: lsp_ext::RunnableKind::Cargo, | 479 | kind: lsp_ext::RunnableKind::Cargo, |
480 | args: lsp_ext::CargoRunnable { | 480 | args: lsp_ext::CargoRunnable { |
@@ -483,6 +483,7 @@ pub(crate) fn handle_runnables( | |||
483 | cmd.to_string(), | 483 | cmd.to_string(), |
484 | "--package".to_string(), | 484 | "--package".to_string(), |
485 | spec.package.clone(), | 485 | spec.package.clone(), |
486 | "--all-targets".to_string(), | ||
486 | ], | 487 | ], |
487 | executable_args: Vec::new(), | 488 | executable_args: Vec::new(), |
488 | expect_test: None, | 489 | expect_test: None, |
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 28e896648..7370505f8 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs | |||
@@ -115,21 +115,21 @@ fn main() {} | |||
115 | }, | 115 | }, |
116 | { | 116 | { |
117 | "args": { | 117 | "args": { |
118 | "cargoArgs": ["check", "--package", "foo"], | 118 | "cargoArgs": ["check", "--package", "foo", "--all-targets"], |
119 | "executableArgs": [], | 119 | "executableArgs": [], |
120 | "workspaceRoot": server.path().join("foo") | 120 | "workspaceRoot": server.path().join("foo") |
121 | }, | 121 | }, |
122 | "kind": "cargo", | 122 | "kind": "cargo", |
123 | "label": "cargo check -p foo" | 123 | "label": "cargo check -p foo --all-targets" |
124 | }, | 124 | }, |
125 | { | 125 | { |
126 | "args": { | 126 | "args": { |
127 | "cargoArgs": ["test", "--package", "foo"], | 127 | "cargoArgs": ["test", "--package", "foo", "--all-targets"], |
128 | "executableArgs": [], | 128 | "executableArgs": [], |
129 | "workspaceRoot": server.path().join("foo") | 129 | "workspaceRoot": server.path().join("foo") |
130 | }, | 130 | }, |
131 | "kind": "cargo", | 131 | "kind": "cargo", |
132 | "label": "cargo test -p foo" | 132 | "label": "cargo test -p foo --all-targets" |
133 | } | 133 | } |
134 | ]), | 134 | ]), |
135 | ); | 135 | ); |