diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovyManyStatementsSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovyManyStatementsSurrounder.java
index 93919ef409ce..aca759721c9c 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovyManyStatementsSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovyManyStatementsSurrounder.java
@@ -21,11 +21,9 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
@@ -72,31 +70,10 @@ public abstract class GroovyManyStatementsSurrounder implements Surrounder {
assert newStmt != null;
ASTNode parentNode = element1.getParent().getNode();
-
- for (int i = 0; i < elements.length; i++) {
- PsiElement element = elements[i];
-
- if (i == 0) {
- parentNode.replaceChild(element1.getNode(), newStmt.getNode());
- } else {
- if (parentNode != element.getParent().getNode()) return null;
-
- final int endOffset = element.getTextRange().getEndOffset();
- final PsiElement semicolon = PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), endOffset, PsiElement.class, false);
- if (semicolon != null && ";".equals(semicolon.getText())) {
- assert parentNode == semicolon.getParent().getNode();
- parentNode.removeChild(semicolon.getNode());
- }
-
- final PsiElement newLine = PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), endOffset, PsiElement.class, false);
- if (newLine != null && GroovyElementTypes.mNLS.equals(newLine.getNode().getElementType())) {
- assert parentNode == newLine.getParent().getNode();
- parentNode.removeChild(newLine.getNode());
- }
-
- parentNode.removeChild(element.getNode());
- }
+ if (elements.length > 1) {
+ parentNode.removeRange(element1.getNode().getTreeNext(), elements[elements.length - 1].getNode().getTreeNext());
}
+ parentNode.replaceChild(element1.getNode(), newStmt.getNode());
return getSurroundSelectionRange(newStmt);
}
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundStatementsTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundStatementsTest.java
index f65df8676e70..bab89af3f2ae 100644
--- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundStatementsTest.java
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundStatementsTest.java
@@ -24,6 +24,7 @@ public class SurroundStatementsTest extends SurroundTestCase {
public void testTry_catch1() throws Exception { doTest(new TryCatchSurrounder()); }
public void testTry_catch_finally() throws Exception { doTest(new TryCatchFinallySurrounder()); }
public void testTry_finally1() throws Exception { doTest(new TryFinallySurrounder()); }
+ public void testTry_finallyFormatting() throws Exception { doTest(new TryFinallySurrounder()); }
public void testWhile1() throws Exception { doTest(new WhileSurrounder()); }
public void testWith2() throws Exception { doTest(new WithStatementsSurrounder()); }
public void testFor1() throws Exception { doTest(new ForSurrounder()); }
diff --git a/plugins/groovy/testdata/groovy/surround/statements/try_finallyFormatting.test b/plugins/groovy/testdata/groovy/surround/statements/try_finallyFormatting.test
new file mode 100644
index 000000000000..0e7babfb4ffe
--- /dev/null
+++ b/plugins/groovy/testdata/groovy/surround/statements/try_finallyFormatting.test
@@ -0,0 +1,12 @@
+def foo() {
+ println hello
+ println hello
+}
+-----
+def foo() {
+ try {
+ println hello
+ println hello
+ } finally {
+ }
+}
\ No newline at end of file