aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-13 10:12:28 +0100
committerGitHub <[email protected]>2020-08-13 10:12:28 +0100
commit8f58d45bc4a8f647bc25faa092a30b0e44d27198 (patch)
tree6fa8fa4bf84db66c4ef5c167fa6af4e54379d3bb /crates
parent982b29925275403697b52dd88bd79868ea8b67d9 (diff)
parent26b98b07aa0e4430bc872b28eadbc822cfee7b6e (diff)
Merge #5739
5739: Cleanup **Move Guard** assist r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/move_guard.rs222
-rw-r--r--crates/ra_assists/src/tests/generated.rs4
-rw-r--r--crates/syntax/src/ast/edit.rs3
3 files changed, 112 insertions, 117 deletions
diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs
index c62ebc306..452115fe6 100644
--- a/crates/ra_assists/src/handlers/move_guard.rs
+++ b/crates/ra_assists/src/handlers/move_guard.rs
@@ -1,5 +1,5 @@
1use syntax::{ 1use syntax::{
2 ast::{AstNode, IfExpr, MatchArm}, 2 ast::{edit::AstNodeEdit, make, AstNode, IfExpr, MatchArm},
3 SyntaxKind::WHITESPACE, 3 SyntaxKind::WHITESPACE,
4}; 4};
5 5
@@ -25,7 +25,9 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
25// 25//
26// fn handle(action: Action) { 26// fn handle(action: Action) {
27// match action { 27// match action {
28// Action::Move { distance } => if distance > 10 { foo() }, 28// Action::Move { distance } => if distance > 10 {
29// foo()
30// },
29// _ => (), 31// _ => (),
30// } 32// }
31// } 33// }
@@ -35,9 +37,13 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
35 let guard = match_arm.guard()?; 37 let guard = match_arm.guard()?;
36 let space_before_guard = guard.syntax().prev_sibling_or_token(); 38 let space_before_guard = guard.syntax().prev_sibling_or_token();
37 39
38 let guard_conditions = guard.expr()?; 40 let guard_condition = guard.expr()?;
39 let arm_expr = match_arm.expr()?; 41 let arm_expr = match_arm.expr()?;
40 let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); 42 let if_expr = make::expr_if(
43 make::condition(guard_condition, None),
44 make::block_expr(None, Some(arm_expr.clone())),
45 )
46 .indent(arm_expr.indent_level());
41 47
42 let target = guard.syntax().text_range(); 48 let target = guard.syntax().text_range();
43 acc.add( 49 acc.add(
@@ -53,7 +59,7 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
53 }; 59 };
54 60
55 edit.delete(guard.syntax().text_range()); 61 edit.delete(guard.syntax().text_range());
56 edit.replace_node_and_indent(arm_expr.syntax(), buf); 62 edit.replace_ast(arm_expr, if_expr);
57 }, 63 },
58 ) 64 )
59} 65}
@@ -134,16 +140,14 @@ mod tests {
134 check_assist_target( 140 check_assist_target(
135 move_guard_to_arm_body, 141 move_guard_to_arm_body,
136 r#" 142 r#"
137 fn f() { 143fn main() {
138 let t = 'a'; 144 match 92 {
139 let chars = "abcd"; 145 x <|>if x > 10 => false,
140 match t { 146 _ => true
141 '\r' <|>if chars.clone().next() == Some('\n') => false, 147 }
142 _ => true 148}
143 } 149"#,
144 } 150 r#"if x > 10"#,
145 "#,
146 r#"if chars.clone().next() == Some('\n')"#,
147 ); 151 );
148 } 152 }
149 153
@@ -152,25 +156,23 @@ mod tests {
152 check_assist( 156 check_assist(
153 move_guard_to_arm_body, 157 move_guard_to_arm_body,
154 r#" 158 r#"
155 fn f() { 159fn main() {
156 let t = 'a'; 160 match 92 {
157 let chars = "abcd"; 161 x <|>if x > 10 => false,
158 match t { 162 _ => true
159 '\r' <|>if chars.clone().next() == Some('\n') => false, 163 }
160 _ => true 164}
161 } 165"#,
162 }
163 "#,
164 r#" 166 r#"
165 fn f() { 167fn main() {
166 let t = 'a'; 168 match 92 {
167 let chars = "abcd"; 169 x => if x > 10 {
168 match t { 170 false
169 '\r' => if chars.clone().next() == Some('\n') { false }, 171 },
170 _ => true 172 _ => true
171 } 173 }
172 } 174}
173 "#, 175"#,
174 ); 176 );
175 } 177 }
176 178
@@ -179,21 +181,23 @@ mod tests {
179 check_assist( 181 check_assist(
180 move_guard_to_arm_body, 182 move_guard_to_arm_body,
181 r#" 183 r#"
182 fn f() { 184fn main() {
183 match x { 185 match 92 {
184 <|>y @ 4 | y @ 5 if y > 5 => true, 186 <|>x @ 4 | x @ 5 if x > 5 => true,
185 _ => false 187 _ => false
186 } 188 }
187 } 189}
188 "#, 190"#,
189 r#" 191 r#"
190 fn f() { 192fn main() {
191 match x { 193 match 92 {
192 y @ 4 | y @ 5 => if y > 5 { true }, 194 x @ 4 | x @ 5 => if x > 5 {
193 _ => false 195 true
194 } 196 },
195 } 197 _ => false
196 "#, 198 }
199}
200"#,
197 ); 201 );
198 } 202 }
199 203
@@ -202,25 +206,21 @@ mod tests {
202 check_assist( 206 check_assist(
203 move_arm_cond_to_match_guard, 207 move_arm_cond_to_match_guard,
204 r#" 208 r#"
205 fn f() { 209fn main() {
206 let t = 'a'; 210 match 92 {
207 let chars = "abcd"; 211 x => if x > 10 { <|>false },
208 match t { 212 _ => true
209 '\r' => if chars.clone().next() == Some('\n') { <|>false }, 213 }
210 _ => true 214}
211 } 215"#,
212 }
213 "#,
214 r#" 216 r#"
215 fn f() { 217fn main() {
216 let t = 'a'; 218 match 92 {
217 let chars = "abcd"; 219 x if x > 10 => false,
218 match t { 220 _ => true
219 '\r' if chars.clone().next() == Some('\n') => false, 221 }
220 _ => true 222}
221 } 223"#,
222 }
223 "#,
224 ); 224 );
225 } 225 }
226 226
@@ -229,15 +229,13 @@ mod tests {
229 check_assist_not_applicable( 229 check_assist_not_applicable(
230 move_arm_cond_to_match_guard, 230 move_arm_cond_to_match_guard,
231 r#" 231 r#"
232 fn f() { 232fn main() {
233 let t = 'a'; 233 match 92 {
234 let chars = "abcd"; 234 x => if let 62 = x { <|>false },
235 match t { 235 _ => true
236 '\r' => if let Some(_) = chars.clone().next() { <|>false }, 236 }
237 _ => true 237}
238 } 238"#,
239 }
240 "#,
241 ); 239 );
242 } 240 }
243 241
@@ -246,25 +244,21 @@ mod tests {
246 check_assist( 244 check_assist(
247 move_arm_cond_to_match_guard, 245 move_arm_cond_to_match_guard,
248 r#" 246 r#"
249 fn f() { 247fn main() {
250 let t = 'a'; 248 match 92 {
251 let chars = "abcd"; 249 x => if x > 10 { <|> },
252 match t { 250 _ => true
253 '\r' => if chars.clone().next().is_some() { <|> }, 251 }
254 _ => true 252}
255 } 253"#,
256 }
257 "#,
258 r#" 254 r#"
259 fn f() { 255fn main() {
260 let t = 'a'; 256 match 92 {
261 let chars = "abcd"; 257 x if x > 10 => { },
262 match t { 258 _ => true
263 '\r' if chars.clone().next().is_some() => { }, 259 }
264 _ => true 260}
265 } 261"#,
266 }
267 "#,
268 ); 262 );
269 } 263 }
270 264
@@ -273,31 +267,27 @@ mod tests {
273 check_assist( 267 check_assist(
274 move_arm_cond_to_match_guard, 268 move_arm_cond_to_match_guard,
275 r#" 269 r#"
276 fn f() { 270fn main() {
277 let mut t = 'a'; 271 match 92 {
278 let chars = "abcd"; 272 x => if x > 10 {
279 match t { 273 92;<|>
280 '\r' => if chars.clone().next().is_some() { 274 false
281 t = 'e';<|> 275 },
282 false 276 _ => true
283 }, 277 }
284 _ => true 278}
285 } 279"#,
286 }
287 "#,
288 r#" 280 r#"
289 fn f() { 281fn main() {
290 let mut t = 'a'; 282 match 92 {
291 let chars = "abcd"; 283 x if x > 10 => {
292 match t { 284 92;
293 '\r' if chars.clone().next().is_some() => { 285 false
294 t = 'e'; 286 },
295 false 287 _ => true
296 }, 288 }
297 _ => true 289}
298 } 290"#,
299 }
300 "#,
301 ); 291 );
302 } 292 }
303} 293}
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs
index 97978e7a2..d16e6fb0a 100644
--- a/crates/ra_assists/src/tests/generated.rs
+++ b/crates/ra_assists/src/tests/generated.rs
@@ -690,7 +690,9 @@ enum Action { Move { distance: u32 }, Stop }
690 690
691fn handle(action: Action) { 691fn handle(action: Action) {
692 match action { 692 match action {
693 Action::Move { distance } => if distance > 10 { foo() }, 693 Action::Move { distance } => if distance > 10 {
694 foo()
695 },
694 _ => (), 696 _ => (),
695 } 697 }
696} 698}
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index 2667d9af4..190746e09 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -601,6 +601,9 @@ pub trait AstNodeEdit: AstNode + Clone + Sized {
601 } 601 }
602 rewriter.rewrite_ast(self) 602 rewriter.rewrite_ast(self)
603 } 603 }
604 fn indent_level(&self) -> IndentLevel {
605 IndentLevel::from_node(self.syntax())
606 }
604 #[must_use] 607 #[must_use]
605 fn indent(&self, level: IndentLevel) -> Self { 608 fn indent(&self, level: IndentLevel) -> Self {
606 Self::cast(level.increase_indent(self.syntax().clone())).unwrap() 609 Self::cast(level.increase_indent(self.syntax().clone())).unwrap()