From ffefbf2ba4365c6d00775c63bb9da9d30d082e10 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 13 Apr 2021 21:07:52 -0700 Subject: Fix extract function with partial block selection --- .../ide_assists/src/handlers/extract_function.rs | 64 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 059414274..78a57fbdc 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -599,7 +599,12 @@ fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option Option i32 { ); } + #[test] + fn extract_partial_block_single_line() { + check_assist( + extract_function, + r#" +fn foo() { + let n = 1; + let mut v = $0n * n;$0 + v += 1; +}"#, + r#" +fn foo() { + let n = 1; + let mut v = fun_name(n); + v += 1; +} + +fn $0fun_name(n: i32) -> i32 { + let mut v = n * n; + v +}"#, + ); + } + + #[test] + fn extract_partial_block() { + check_assist( + extract_function, + r#" +fn foo() { + let m = 2; + let n = 1; + let mut v = m $0* n; + let mut w = 3;$0 + v += 1; + w += 1; +}"#, + r#" +fn foo() { + let m = 2; + let n = 1; + let (mut v, mut w) = fun_name(m, n); + v += 1; + w += 1; +} + +fn $0fun_name(m: i32, n: i32) -> (i32, i32) { + let mut v = m * n; + let mut w = 3; + (v, w) +}"#, + ); + } + #[test] fn argument_form_expr() { check_assist( -- cgit v1.2.3