diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_function.rs | 10 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/generate_function.rs | 56 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/pull_assignment_up.rs | 43 |
3 files changed, 103 insertions, 6 deletions
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 9f34cc725..8779d8bd1 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs | |||
@@ -112,7 +112,10 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
112 | 112 | ||
113 | let fn_def = format_function(ctx, module, &fun, old_indent, new_indent); | 113 | let fn_def = format_function(ctx, module, &fun, old_indent, new_indent); |
114 | let insert_offset = insert_after.text_range().end(); | 114 | let insert_offset = insert_after.text_range().end(); |
115 | builder.insert(insert_offset, fn_def); | 115 | match ctx.config.snippet_cap { |
116 | Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), | ||
117 | None => builder.insert(insert_offset, fn_def), | ||
118 | } | ||
116 | }, | 119 | }, |
117 | ) | 120 | ) |
118 | } | 121 | } |
@@ -1079,7 +1082,10 @@ fn format_function( | |||
1079 | let params = make_param_list(ctx, module, fun); | 1082 | let params = make_param_list(ctx, module, fun); |
1080 | let ret_ty = make_ret_ty(ctx, module, fun); | 1083 | let ret_ty = make_ret_ty(ctx, module, fun); |
1081 | let body = make_body(ctx, old_indent, new_indent, fun); | 1084 | let body = make_body(ctx, old_indent, new_indent, fun); |
1082 | format_to!(fn_def, "\n\n{}fn $0{}{}", new_indent, fun.name, params); | 1085 | match ctx.config.snippet_cap { |
1086 | Some(_) => format_to!(fn_def, "\n\n{}fn $0{}{}", new_indent, fun.name, params), | ||
1087 | None => format_to!(fn_def, "\n\n{}fn {}{}", new_indent, fun.name, params), | ||
1088 | } | ||
1083 | if let Some(ret_ty) = ret_ty { | 1089 | if let Some(ret_ty) = ret_ty { |
1084 | format_to!(fn_def, " {}", ret_ty); | 1090 | format_to!(fn_def, " {}", ret_ty); |
1085 | } | 1091 | } |
diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index 959824981..3870b7e75 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | use hir::HirDisplay; | 1 | use hir::HirDisplay; |
2 | use ide_db::{base_db::FileId, helpers::SnippetCap}; | 2 | use ide_db::{base_db::FileId, helpers::SnippetCap}; |
3 | use rustc_hash::{FxHashMap, FxHashSet}; | 3 | use rustc_hash::{FxHashMap, FxHashSet}; |
4 | use stdx::to_lower_snake_case; | ||
4 | use syntax::{ | 5 | use syntax::{ |
5 | ast::{ | 6 | ast::{ |
6 | self, | 7 | self, |
@@ -257,14 +258,15 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) { | |||
257 | fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> { | 258 | fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> { |
258 | match fn_arg { | 259 | match fn_arg { |
259 | ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?), | 260 | ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?), |
260 | _ => Some( | 261 | _ => { |
261 | fn_arg | 262 | let s = fn_arg |
262 | .syntax() | 263 | .syntax() |
263 | .descendants() | 264 | .descendants() |
264 | .filter(|d| ast::NameRef::can_cast(d.kind())) | 265 | .filter(|d| ast::NameRef::can_cast(d.kind())) |
265 | .last()? | 266 | .last()? |
266 | .to_string(), | 267 | .to_string(); |
267 | ), | 268 | Some(to_lower_snake_case(&s)) |
269 | } | ||
268 | } | 270 | } |
269 | } | 271 | } |
270 | 272 | ||
@@ -448,6 +450,52 @@ mod baz { | |||
448 | } | 450 | } |
449 | 451 | ||
450 | #[test] | 452 | #[test] |
453 | fn add_function_with_upper_camel_case_arg() { | ||
454 | check_assist( | ||
455 | generate_function, | ||
456 | r" | ||
457 | struct BazBaz; | ||
458 | fn foo() { | ||
459 | bar$0(BazBaz); | ||
460 | } | ||
461 | ", | ||
462 | r" | ||
463 | struct BazBaz; | ||
464 | fn foo() { | ||
465 | bar(BazBaz); | ||
466 | } | ||
467 | |||
468 | fn bar(baz_baz: BazBaz) ${0:-> ()} { | ||
469 | todo!() | ||
470 | } | ||
471 | ", | ||
472 | ); | ||
473 | } | ||
474 | |||
475 | #[test] | ||
476 | fn add_function_with_upper_camel_case_arg_as_cast() { | ||
477 | check_assist( | ||
478 | generate_function, | ||
479 | r" | ||
480 | struct BazBaz; | ||
481 | fn foo() { | ||
482 | bar$0(&BazBaz as *const BazBaz); | ||
483 | } | ||
484 | ", | ||
485 | r" | ||
486 | struct BazBaz; | ||
487 | fn foo() { | ||
488 | bar(&BazBaz as *const BazBaz); | ||
489 | } | ||
490 | |||
491 | fn bar(baz_baz: *const BazBaz) ${0:-> ()} { | ||
492 | todo!() | ||
493 | } | ||
494 | ", | ||
495 | ); | ||
496 | } | ||
497 | |||
498 | #[test] | ||
451 | fn add_function_with_function_call_arg() { | 499 | fn add_function_with_function_call_arg() { |
452 | check_assist( | 500 | check_assist( |
453 | generate_function, | 501 | generate_function, |
diff --git a/crates/ide_assists/src/handlers/pull_assignment_up.rs b/crates/ide_assists/src/handlers/pull_assignment_up.rs index 13e1cb754..377ed4f2f 100644 --- a/crates/ide_assists/src/handlers/pull_assignment_up.rs +++ b/crates/ide_assists/src/handlers/pull_assignment_up.rs | |||
@@ -156,6 +156,17 @@ fn is_equivalent( | |||
156 | false | 156 | false |
157 | } | 157 | } |
158 | } | 158 | } |
159 | (ast::Expr::PrefixExpr(prefix0), ast::Expr::PrefixExpr(prefix1)) | ||
160 | if prefix0.op_kind() == Some(ast::PrefixOp::Deref) | ||
161 | && prefix1.op_kind() == Some(ast::PrefixOp::Deref) => | ||
162 | { | ||
163 | mark::hit!(test_pull_assignment_up_deref); | ||
164 | if let (Some(prefix0), Some(prefix1)) = (prefix0.expr(), prefix1.expr()) { | ||
165 | is_equivalent(sema, &prefix0, &prefix1) | ||
166 | } else { | ||
167 | false | ||
168 | } | ||
169 | } | ||
159 | _ => false, | 170 | _ => false, |
160 | } | 171 | } |
161 | } | 172 | } |
@@ -397,4 +408,36 @@ fn foo() { | |||
397 | }"#, | 408 | }"#, |
398 | ) | 409 | ) |
399 | } | 410 | } |
411 | |||
412 | #[test] | ||
413 | fn test_pull_assignment_up_deref() { | ||
414 | mark::check!(test_pull_assignment_up_deref); | ||
415 | check_assist( | ||
416 | pull_assignment_up, | ||
417 | r#" | ||
418 | fn foo() { | ||
419 | let mut a = 1; | ||
420 | let b = &mut a; | ||
421 | |||
422 | if true { | ||
423 | $0*b = 2; | ||
424 | } else { | ||
425 | *b = 3; | ||
426 | } | ||
427 | } | ||
428 | "#, | ||
429 | r#" | ||
430 | fn foo() { | ||
431 | let mut a = 1; | ||
432 | let b = &mut a; | ||
433 | |||
434 | *b = if true { | ||
435 | 2 | ||
436 | } else { | ||
437 | 3 | ||
438 | }; | ||
439 | } | ||
440 | "#, | ||
441 | ) | ||
442 | } | ||
400 | } | 443 | } |