Repository: intellij-rust/intellij-rust
Issue: Extract function refactoring removes all empty lines

## Environment

* **IntelliJ Rust plugin version:** 0.2.119.2201-201
* **Rust toolchain version:** 1.42.0 (b8cedc004 2020-03-09) x86_64-unknown-linux-gnu
* **IDE name and version:** IntelliJ IDEA 2020.1 Ultimate Edition (IU-201.6668.121)
* **Operating system:** Linux 5.6.3-arch1-1

## Problem description
Extract function refactoring removes all empty lines from extracted code

## Steps to reproduce
```rust
fn main() {
    /* selection start */
    println!("a");

    println!("b");
    /* selection end */
}
```

After invoking "Extract function":
```rust
fn main() {
    foo();
}

fn foo() {
    /* selection start */
    println!("a");
    println!("b");
    /* selection end */
}
```

Changed files:
src/test/kotlin/org/rust/ide/refactoring/RsExtractFunctionTest.kt
src/main/kotlin/org/rust/ide/refactoring/extractFunction/RsExtractFunctionConfig.kt

diff --git a/src/main/kotlin/org/rust/ide/refactoring/extractFunction/RsExtractFunctionConfig.kt b/src/main/kotlin/org/rust/ide/refactoring/extractFunction/RsExtractFunctionConfig.kt
index f33e6ce4b..3f391a915 100644
--- a/src/main/kotlin/org/rust/ide/refactoring/extractFunction/RsExtractFunctionConfig.kt
+++ b/src/main/kotlin/org/rust/ide/refactoring/extractFunction/RsExtractFunctionConfig.kt
@@ -10,12 +10,16 @@ import com.intellij.psi.PsiFile
 import com.intellij.psi.PsiReference
 import com.intellij.psi.search.LocalSearchScope
 import com.intellij.psi.search.searches.ReferencesSearch
+import com.intellij.psi.util.siblings
 import org.rust.ide.presentation.insertionSafeText
 import org.rust.ide.utils.findStatementsOrExprInRange
 import org.rust.lang.core.psi.*
 import org.rust.lang.core.psi.ext.*
 import org.rust.lang.core.resolve.ImplLookup
-import org.rust.lang.core.types.ty.*
+import org.rust.lang.core.types.ty.Ty
+import org.rust.lang.core.types.ty.TyReference
+import org.rust.lang.core.types.ty.TyTuple
+import org.rust.lang.core.types.ty.TyUnit
 import org.rust.lang.core.types.type
 import org.rust.stdext.buildList
 
@@ -155,12 +159,18 @@ class RsExtractFunctionConfig private constructor(
                 val unselectedParamsTexts = parameters
                     .filter { !it.isSelected }
                     .map { "let ${it.name}: ${it.type} ;\\n" }
-                val elementsTexts = elements.map { it.text }
+                val elementsText = if (elements.isNotEmpty()) {
+                    val last = elements.last()
+                    val allElements = elements.first().siblings().takeWhile { it != last } + last
+                    allElements.joinToString(separator = "") { it.text }
+                } else {
+                    ""
+                }
                 val returnExprText = returnValue?.exprText.orEmpty()
 
                 val bodyContent = buildList<String> {
                     addAll(unselectedParamsTexts)
-                    addAll(elementsTexts)
+                    add(elementsText)
                     if (returnExprText.isNotEmpty()) {
                         add(returnExprText)
                     }
diff --git a/src/test/kotlin/org/rust/ide/refactoring/RsExtractFunctionTest.kt b/src/test/kotlin/org/rust/ide/refactoring/RsExtractFunctionTest.kt
index 1018ff3d8..b7e9205f1 100644
--- a/src/test/kotlin/org/rust/ide/refactoring/RsExtractFunctionTest.kt
+++ b/src/test/kotlin/org/rust/ide/refactoring/RsExtractFunctionTest.kt
@@ -1086,6 +1086,30 @@ class RsExtractFunctionTest : RsTestBase() {
         false,
         "bar")
 
+    fun `test extract a function with empty lines`() = doTest("""
+        fn main() {
+            <selection>println!("a");
+
+
+            // comment
+            println!("b");</selection>
+        }
+    """, """
+        fn main() {
+            foo();
+        }
+
+        fn foo() {
+            println!("a");
+
+
+            // comment
+            println!("b");
+        }
+    """,
+        false,
+        "foo")
+
     private fun doTest(@Language("Rust") code: String,
                        @Language("Rust") excepted: String,
                        pub: Boolean,