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 3e2253e15705..8b8a52a39f78 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 @@ -79,10 +79,7 @@ public abstract class GroovyManyStatementsSurrounder implements Surrounder { } protected static void addStatements(GrCodeBlock block, PsiElement[] elements) throws IncorrectOperationException { - for (PsiElement element : elements) { - final GrStatement statement = (GrStatement)element; - block.addStatementBefore(statement, null); - } + block.addRangeBefore(elements[0], elements[elements.length - 1], block.getRBrace()); } protected abstract GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovySurroundDescriptor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovySurroundDescriptor.java index a7b6a1b14ba6..22bf1d80ee16 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovySurroundDescriptor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/GroovySurroundDescriptor.java @@ -15,21 +15,12 @@ */ package org.jetbrains.plugins.groovy.lang.surroundWith; -import com.intellij.lang.ASTNode; import com.intellij.lang.surroundWith.SurroundDescriptor; import com.intellij.lang.surroundWith.Surrounder; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiWhiteSpace; -import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; -import org.jetbrains.plugins.groovy.lang.lexer.TokenSets; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; - -import java.util.ArrayList; -import java.util.List; +import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil; /** * User: Dmitry.Krasilschikov @@ -70,51 +61,7 @@ public class GroovySurroundDescriptor implements SurroundDescriptor { @NotNull public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { - GrStatement[] statements = findStatementsInRange(file, startOffset, endOffset); - - if (statements == null) return PsiElement.EMPTY_ARRAY; - return statements; + return GroovyRefactoringUtil.findStatementsInRange(file, startOffset, endOffset, true); } - @Nullable - private static GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) { - - GrStatement statement; - int endOffsetLocal = endOffset; - int startOffsetLocal = startOffset; - - List statements = new ArrayList(); - do { - PsiElement element1 = file.findElementAt(startOffsetLocal); - PsiElement element2 = file.findElementAt(endOffsetLocal - 1); - - if (element1 == null) break; - ASTNode node1 = element1.getNode(); - assert node1 != null; - if (element1 instanceof PsiWhiteSpace || TokenSets.WHITE_SPACE_TOKEN_SET.contains(node1.getElementType()) || GroovyTokenTypes.mNLS.equals(node1.getElementType())) { - startOffsetLocal = element1.getTextRange().getEndOffset(); - } - - if (element2 == null) break; - ASTNode node2 = element2.getNode(); - assert node2 != null; - if (element2 instanceof PsiWhiteSpace || TokenSets.WHITE_SPACE_TOKEN_SET.contains(node2.getElementType()) || GroovyTokenTypes.mNLS.equals(node2.getElementType())) { - endOffsetLocal = element2.getTextRange().getStartOffset(); - } - - if (";".equals(element2.getText())) endOffsetLocal = endOffsetLocal - 1; - - statement = PsiTreeUtil.findElementOfClassAtRange(file, startOffsetLocal, endOffsetLocal, GrStatement.class); - - if (statement == null) break; - statements.add(statement); - - startOffsetLocal = statement.getTextRange().getEndOffset(); - final PsiElement endSemicolon = file.findElementAt(startOffsetLocal); - - if (endSemicolon != null && ";".equals(endSemicolon.getText())) startOffsetLocal = startOffsetLocal + 1; - } while (true); - - return statements.toArray(new GrStatement[0]); - } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderByClosure.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderByClosure.java index b31e4cd9b9b9..43b6260822df 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderByClosure.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderByClosure.java @@ -46,7 +46,7 @@ public class SurrounderByClosure extends GroovyManyStatementsSurrounder { } GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject()); - final GrMethodCallExpression call = (GrMethodCallExpression) factory.createExpressionFromText("{ -> }.call()", context); + final GrMethodCallExpression call = (GrMethodCallExpression) factory.createExpressionFromText("{ -> \n}.call()", context); final GrClosableBlock closure = (GrClosableBlock) ((GrReferenceExpression) call.getInvokedExpression()).getQualifierExpression(); addStatements(closure, elements); return call; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java index 4534bfb12509..37c3dc6fff9d 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java @@ -267,7 +267,7 @@ public abstract class GroovyRefactoringUtil { editor.getSelectionModel().setSelection(start, end); } - public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) { + @NotNull public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) { if (!(file instanceof GroovyFileBase)) return PsiElement.EMPTY_ARRAY; Language language = GroovyFileType.GROOVY_FILE_TYPE.getLanguage(); PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language); 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 bab89af3f2ae..233551dee72f 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 @@ -28,5 +28,6 @@ public class SurroundStatementsTest extends SurroundTestCase { public void testWhile1() throws Exception { doTest(new WhileSurrounder()); } public void testWith2() throws Exception { doTest(new WithStatementsSurrounder()); } public void testFor1() throws Exception { doTest(new ForSurrounder()); } + public void testIfComments() throws Exception { doTest(new IfSurrounder()); } } diff --git a/plugins/groovy/testdata/groovy/surround/statements/ifComments.test b/plugins/groovy/testdata/groovy/surround/statements/ifComments.test new file mode 100644 index 000000000000..d7114957bc8e --- /dev/null +++ b/plugins/groovy/testdata/groovy/surround/statements/ifComments.test @@ -0,0 +1,11 @@ +def foo() { + println 'hi' //foo + println 'hi' //foo +} +----- +def foo() { + if () { + println 'hi' //foo + println 'hi' //foo + } +} \ No newline at end of file