@@ -97,6 +97,10 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
97
97
98
98
let params = extracted_function_params ( ctx, & body, locals_used. iter ( ) . copied ( ) ) ;
99
99
100
+ let insert_comma = body
101
+ . parent ( )
102
+ . and_then ( ast:: MatchArm :: cast)
103
+ . map_or ( false , |it| it. comma_token ( ) . is_none ( ) ) ;
100
104
let fun = Function {
101
105
name : "fun_name" . to_string ( ) ,
102
106
self_param,
@@ -110,7 +114,10 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
110
114
let new_indent = IndentLevel :: from_node ( & insert_after) ;
111
115
let old_indent = fun. body . indent_level ( ) ;
112
116
113
- builder. replace ( target_range, format_replacement ( ctx, & fun, old_indent, has_await) ) ;
117
+ builder. replace ( target_range, make_call ( ctx, & fun, old_indent, has_await) ) ;
118
+ if insert_comma {
119
+ builder. insert ( target_range. end ( ) , "," ) ;
120
+ }
114
121
115
122
let fn_def = format_function ( ctx, module, & fun, old_indent, new_indent, has_await) ;
116
123
let insert_offset = insert_after. text_range ( ) . end ( ) ;
@@ -364,6 +371,13 @@ fn try_kind_of_ty(ty: hir::Type, ctx: &AssistContext) -> Option<TryKind> {
364
371
}
365
372
366
373
impl FunctionBody {
374
+ fn parent ( & self ) -> Option < SyntaxNode > {
375
+ match self {
376
+ FunctionBody :: Expr ( expr) => expr. syntax ( ) . parent ( ) ,
377
+ FunctionBody :: Span { parent, .. } => Some ( parent. syntax ( ) . clone ( ) ) ,
378
+ }
379
+ }
380
+
367
381
fn from_expr ( expr : ast:: Expr ) -> Option < Self > {
368
382
match expr {
369
383
ast:: Expr :: BreakExpr ( it) => it. expr ( ) . map ( Self :: Expr ) ,
@@ -978,7 +992,7 @@ fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option<SyntaxNod
978
992
last_ancestor
979
993
}
980
994
981
- fn format_replacement (
995
+ fn make_call (
982
996
ctx : & AssistContext ,
983
997
fun : & Function ,
984
998
indent : IndentLevel ,
@@ -3767,6 +3781,56 @@ async fn some_function() {
3767
3781
extract_function,
3768
3782
r#"
3769
3783
fn main() $0{}$0
3784
+ "# ,
3785
+ ) ;
3786
+ }
3787
+
3788
+ #[ test]
3789
+ fn extract_adds_comma_for_match_arm ( ) {
3790
+ check_assist (
3791
+ extract_function,
3792
+ r#"
3793
+ fn main() {
3794
+ match 6 {
3795
+ 100 => $0{ 100 }$0
3796
+ _ => 0,
3797
+ }
3798
+ }
3799
+ "# ,
3800
+ r#"
3801
+ fn main() {
3802
+ match 6 {
3803
+ 100 => fun_name(),
3804
+ _ => 0,
3805
+ }
3806
+ }
3807
+
3808
+ fn $0fun_name() -> i32 {
3809
+ 100
3810
+ }
3811
+ "# ,
3812
+ ) ;
3813
+ check_assist (
3814
+ extract_function,
3815
+ r#"
3816
+ fn main() {
3817
+ match 6 {
3818
+ 100 => $0{ 100 }$0,
3819
+ _ => 0,
3820
+ }
3821
+ }
3822
+ "# ,
3823
+ r#"
3824
+ fn main() {
3825
+ match 6 {
3826
+ 100 => fun_name(),
3827
+ _ => 0,
3828
+ }
3829
+ }
3830
+
3831
+ fn $0fun_name() -> i32 {
3832
+ 100
3833
+ }
3770
3834
"# ,
3771
3835
) ;
3772
3836
}
0 commit comments