aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-10 17:22:04 +0100
committerAleksey Kladov <[email protected]>2020-07-10 17:22:04 +0100
commita482eb0cd85fa389a2c5188c52c9621ac1ed27c5 (patch)
tree06e3eda4397d6ab7e7de5286301ef7f36ee62d5b /crates/ra_assists
parenta6ac5ac7424687b0418463f27bf0c032fbf48bb8 (diff)
Better caret placement when filling match arms
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs210
1 files changed, 86 insertions, 124 deletions
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index 511355e07..708e1bc6c 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -115,11 +115,19 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
115 let old_range = match_arm_list.syntax().text_range(); 115 let old_range = match_arm_list.syntax().text_range();
116 match (first_new_arm, ctx.config.snippet_cap) { 116 match (first_new_arm, ctx.config.snippet_cap) {
117 (Some(first_new_arm), Some(cap)) => { 117 (Some(first_new_arm), Some(cap)) => {
118 let snippet = render_snippet( 118 let extend_lifetime;
119 cap, 119 let cursor = match first_new_arm
120 new_arm_list.syntax(), 120 .syntax()
121 Cursor::Before(first_new_arm.syntax()), 121 .descendants()
122 ); 122 .find_map(ast::PlaceholderPat::cast)
123 {
124 Some(it) => {
125 extend_lifetime = it.syntax().clone();
126 Cursor::Replace(&extend_lifetime)
127 }
128 None => Cursor::Before(first_new_arm.syntax()),
129 };
130 let snippet = render_snippet(cap, new_arm_list.syntax(), cursor);
123 builder.replace_snippet(cap, old_range, snippet); 131 builder.replace_snippet(cap, old_range, snippet);
124 } 132 }
125 _ => builder.replace(old_range, new_arm_list.to_string()), 133 _ => builder.replace(old_range, new_arm_list.to_string()),
@@ -291,30 +299,22 @@ mod tests {
291 check_assist( 299 check_assist(
292 fill_match_arms, 300 fill_match_arms,
293 r#" 301 r#"
294 enum A { 302enum A { As, Bs, Cs(Option<i32>) }
295 As, 303fn main() {
296 Bs, 304 match A::As<|> {
297 Cs(Option<i32>), 305 A::Cs(_) | A::Bs => {}
298 } 306 }
299 fn main() { 307}
300 match A::As<|> { 308"#,
301 A::Cs(_) | A::Bs => {}
302 }
303 }
304 "#,
305 r#" 309 r#"
306 enum A { 310enum A { As, Bs, Cs(Option<i32>) }
307 As, 311fn main() {
308 Bs, 312 match A::As {
309 Cs(Option<i32>), 313 A::Cs(_) | A::Bs => {}
310 } 314 $0A::As => {}
311 fn main() { 315 }
312 match A::As { 316}
313 A::Cs(_) | A::Bs => {} 317"#,
314 $0A::As => {}
315 }
316 }
317 "#,
318 ); 318 );
319 } 319 }
320 320
@@ -323,47 +323,29 @@ mod tests {
323 check_assist( 323 check_assist(
324 fill_match_arms, 324 fill_match_arms,
325 r#" 325 r#"
326 enum A { 326enum A { As, Bs, Cs, Ds(String), Es(B) }
327 As, 327enum B { Xs, Ys }
328 Bs, 328fn main() {
329 Cs, 329 match A::As<|> {
330 Ds(String), 330 A::Bs if 0 < 1 => {}
331 Es(B), 331 A::Ds(_value) => { let x = 1; }
332 } 332 A::Es(B::Xs) => (),
333 enum B { 333 }
334 Xs, 334}
335 Ys, 335"#,
336 }
337 fn main() {
338 match A::As<|> {
339 A::Bs if 0 < 1 => {}
340 A::Ds(_value) => { let x = 1; }
341 A::Es(B::Xs) => (),
342 }
343 }
344 "#,
345 r#" 336 r#"
346 enum A { 337enum A { As, Bs, Cs, Ds(String), Es(B) }
347 As, 338enum B { Xs, Ys }
348 Bs, 339fn main() {
349 Cs, 340 match A::As {
350 Ds(String), 341 A::Bs if 0 < 1 => {}
351 Es(B), 342 A::Ds(_value) => { let x = 1; }
352 } 343 A::Es(B::Xs) => (),
353 enum B { 344 $0A::As => {}
354 Xs, 345 A::Cs => {}
355 Ys, 346 }
356 } 347}
357 fn main() { 348"#,
358 match A::As {
359 A::Bs if 0 < 1 => {}
360 A::Ds(_value) => { let x = 1; }
361 A::Es(B::Xs) => (),
362 $0A::As => {}
363 A::Cs => {}
364 }
365 }
366 "#,
367 ); 349 );
368 } 350 }
369 351
@@ -372,32 +354,24 @@ mod tests {
372 check_assist( 354 check_assist(
373 fill_match_arms, 355 fill_match_arms,
374 r#" 356 r#"
375 enum A { 357enum A { As, Bs, Cs(Option<i32>) }
376 As, 358fn main() {
377 Bs, 359 match A::As<|> {
378 Cs(Option<i32>), 360 A::As(_) => {}
379 } 361 a @ A::Bs(_) => {}
380 fn main() { 362 }
381 match A::As<|> { 363}
382 A::As(_) => {} 364"#,
383 a @ A::Bs(_) => {}
384 }
385 }
386 "#,
387 r#" 365 r#"
388 enum A { 366enum A { As, Bs, Cs(Option<i32>) }
389 As, 367fn main() {
390 Bs, 368 match A::As {
391 Cs(Option<i32>), 369 A::As(_) => {}
392 } 370 a @ A::Bs(_) => {}
393 fn main() { 371 A::Cs(${0:_}) => {}
394 match A::As { 372 }
395 A::As(_) => {} 373}
396 a @ A::Bs(_) => {} 374"#,
397 $0A::Cs(_) => {}
398 }
399 }
400 "#,
401 ); 375 );
402 } 376 }
403 377
@@ -406,39 +380,27 @@ mod tests {
406 check_assist( 380 check_assist(
407 fill_match_arms, 381 fill_match_arms,
408 r#" 382 r#"
409 enum A { 383enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
410 As,
411 Bs,
412 Cs(String),
413 Ds(String, String),
414 Es { x: usize, y: usize }
415 }
416 384
417 fn main() { 385fn main() {
418 let a = A::As; 386 let a = A::As;
419 match a<|> {} 387 match a<|> {}
420 } 388}
421 "#, 389"#,
422 r#" 390 r#"
423 enum A { 391enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
424 As,
425 Bs,
426 Cs(String),
427 Ds(String, String),
428 Es { x: usize, y: usize }
429 }
430 392
431 fn main() { 393fn main() {
432 let a = A::As; 394 let a = A::As;
433 match a { 395 match a {
434 $0A::As => {} 396 $0A::As => {}
435 A::Bs => {} 397 A::Bs => {}
436 A::Cs(_) => {} 398 A::Cs(_) => {}
437 A::Ds(_, _) => {} 399 A::Ds(_, _) => {}
438 A::Es { x, y } => {} 400 A::Es { x, y } => {}
439 } 401 }
440 } 402}
441 "#, 403"#,
442 ); 404 );
443 } 405 }
444 406
@@ -778,7 +740,7 @@ fn foo(opt: Option<i32>) {
778 r#" 740 r#"
779fn foo(opt: Option<i32>) { 741fn foo(opt: Option<i32>) {
780 match opt { 742 match opt {
781 $0Some(_) => {} 743 Some(${0:_}) => {}
782 None => {} 744 None => {}
783 } 745 }
784} 746}