diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml
index 1b6d627b282b..5c364fa72c2d 100644
--- a/plugins/groovy/src/META-INF/plugin.xml
+++ b/plugins/groovy/src/META-INF/plugin.xml
@@ -174,7 +174,7 @@
+ implementationClass="org.jetbrains.plugins.groovy.lang.surroundWith.descriptors.GroovySurroundDescriptor"/>
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovyStmtsSurroundDescriptor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovyStmtsSurroundDescriptor.java
deleted file mode 100644
index 89148f2576a8..000000000000
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovyStmtsSurroundDescriptor.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright 2000-2009 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jetbrains.plugins.groovy.lang.surroundWith.descriptors;
-
-import com.intellij.lang.surroundWith.Surrounder;
-import com.intellij.util.containers.ContainerUtil;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovySurrounderByClosure;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.blocks.open.*;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithParenthesisExprSurrounder;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithTypeCastSurrounder;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithWithExprSurrounder;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfElseExprSurrounder;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfExprSurrounder;
-import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithWhileExprSurrounder;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * User: Dmitry.Krasilschikov
- * Date: 22.05.2007
- */
-public class GroovyStmtsSurroundDescriptor extends GroovySurroundDescriptor {
- private static final Surrounder[] stmtsSurrounders = new Surrounder[]{
- new GroovyWithWithStatementsSurrounder(),
-
- new GroovyWithIfSurrounder(),
- new GroovyWithIfElseSurrounder(),
-
- new GroovyWithTryCatchSurrounder(),
- new GroovyWithTryFinallySurrounder(),
- new GroovyWithTryCatchFinallySurrounder(),
-
- new GroovyWithWhileSurrounder(),
-
- new GroovySurrounderByClosure(),
- new GroovyWithShouldFailWithTypeStatementsSurrounder(),
- };
-
- /********** ***********/
- private static final Surrounder[] exprSurrounders = new Surrounder[]{
- new GroovyWithParenthesisExprSurrounder(),
- new GroovyWithTypeCastSurrounder(),
-
- new GroovyWithWithExprSurrounder(),
-
- new GroovyWithIfExprSurrounder(),
- new GroovyWithIfElseExprSurrounder(),
-
- new GroovyWithWhileExprSurrounder()
- };
-
- public static Surrounder[] getStmtsSurrounders() {
- return stmtsSurrounders;
- }
-
- public static Surrounder[] getExprSurrounders() {
- return exprSurrounders;
- }
-
- @NotNull
- public Surrounder[] getSurrounders() {
- List surroundersList = new ArrayList();
- ContainerUtil.addAll(surroundersList, exprSurrounders);
- ContainerUtil.addAll(surroundersList, stmtsSurrounders);
- return surroundersList.toArray(new Surrounder[0]);
- }
-}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovySurroundDescriptor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovySurroundDescriptor.java
index 00cde41dd62c..2e24431dbe6b 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovySurroundDescriptor.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/descriptors/GroovySurroundDescriptor.java
@@ -17,6 +17,7 @@ package org.jetbrains.plugins.groovy.lang.surroundWith.descriptors;
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;
@@ -26,6 +27,14 @@ 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 org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovySurrounderByClosure;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.blocks.open.*;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithParenthesisExprSurrounder;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithTypeCastSurrounder;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithWithExprSurrounder;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfElseExprSurrounder;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfExprSurrounder;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithWhileExprSurrounder;
import java.util.ArrayList;
import java.util.List;
@@ -34,7 +43,40 @@ import java.util.List;
* User: Dmitry.Krasilschikov
* Date: 22.05.2007
*/
-public abstract class GroovySurroundDescriptor implements SurroundDescriptor {
+public class GroovySurroundDescriptor implements SurroundDescriptor {
+ private static final Surrounder[] ourSurrounders = new Surrounder[]{
+ //statements: like in java
+ new GroovyWithIfSurrounder(),
+ new GroovyWithIfElseSurrounder(),
+ new GroovyWithWhileSurrounder(),
+ //there's no do-while in Groovy
+ new GroovySurrounderByClosure(),
+ //like in Java
+ new GroovyWithForSurrounder(),
+ new GroovyWithTryCatchSurrounder(),
+ new GroovyWithTryFinallySurrounder(),
+ new GroovyWithTryCatchFinallySurrounder(),
+ //groovy-specific statements
+ new GroovyWithShouldFailWithTypeStatementsSurrounder(),
+ //expressions: like in java
+ new GroovyWithParenthesisExprSurrounder(),
+ new GroovyWithTypeCastSurrounder(),
+
+ //groovy-specific
+ new GroovyWithWithExprSurrounder(),
+ new GroovyWithWithStatementsSurrounder(),
+
+ new GroovyWithIfExprSurrounder(),
+ new GroovyWithIfElseExprSurrounder(),
+
+ new GroovyWithWhileExprSurrounder()
+ };
+
+ @NotNull
+ public Surrounder[] getSurrounders() {
+ return ourSurrounders;
+ }
+
@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
GrStatement[] statements = findStatementsInRange(file, startOffset, endOffset);
@@ -44,7 +86,7 @@ public abstract class GroovySurroundDescriptor implements SurroundDescriptor {
}
@Nullable
- private GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
+ private static GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
GrStatement statement;
int endOffsetLocal = endOffset;
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithForSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithForSurrounder.java
new file mode 100644
index 000000000000..31b19afb850d
--- /dev/null
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithForSurrounder.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.blocks.open;
+
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiElement;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause;
+import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovyManyStatementsSurrounder;
+
+/**
+ * User: Dmitry.Krasilschikov
+ * Date: 25.05.2007
+ */
+public class GroovyWithForSurrounder extends GroovyManyStatementsSurrounder {
+ protected GroovyPsiElement doSurroundElements(PsiElement[] elements) throws IncorrectOperationException {
+ GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
+ GrForStatement whileStatement = (GrForStatement) factory.createTopElementFromText("for(a in b){\n}");
+ addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
+ return whileStatement;
+ }
+
+ protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
+ assert element instanceof GrForStatement;
+ GrForClause clause = ((GrForStatement) element).getClause();
+
+ int endOffset = element.getTextRange().getEndOffset();
+ if (clause != null) {
+ endOffset = clause.getTextRange().getStartOffset();
+ clause.getParent().getNode().removeChild(clause.getNode());
+ }
+ return new TextRange(endOffset, endOffset);
+ }
+
+ public String getTemplateDescription() {
+ return "for";
+ }
+}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfElseSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfElseSurrounder.java
index 673ecabdfc99..a5b09b6c11fd 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfElseSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfElseSurrounder.java
@@ -37,6 +37,6 @@ public class GroovyWithIfElseSurrounder extends GroovyWithIfSurrounder {
@Override
public String getTemplateDescription() {
- return super.getTemplateDescription() + " / else";
+ return "if / else";
}
}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfSurrounder.java
index 1f7b95284159..0e2ff2cba9e5 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithIfSurrounder.java
@@ -56,7 +56,7 @@ public class GroovyWithIfSurrounder extends GroovyManyStatementsSurrounder {
}
public String getTemplateDescription() {
- return "if () {...}";
+ return "if";
}
public boolean isApplicable(@NotNull PsiElement[] elements) {
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithWhileSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithWhileSurrounder.java
index d45167d6d542..520005fea88b 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithWhileSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/blocks/open/GroovyWithWhileSurrounder.java
@@ -17,14 +17,15 @@ package org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersIm
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.*;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovyManyStatementsSurrounder;
@@ -57,13 +58,13 @@ public class GroovyWithWhileSurrounder extends GroovyManyStatementsSurrounder {
if (elements.length == 1 && elements[0] instanceof GrStatement) {
if (elements[0] instanceof GrExpression) {
PsiType type = ((GrExpression) elements[0]).getType();
- return type == null || !((PsiPrimitiveType) PsiType.BOOLEAN).getBoxedTypeName().equals(type.getCanonicalText());
+ return type == null || !PsiType.BOOLEAN.getBoxedTypeName().equals(type.getCanonicalText());
}
}
return true;
}
public String getTemplateDescription() {
- return "while () {...}";
+ return "while";
}
}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithParenthesisExprSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithParenthesisExprSurrounder.java
index d2c315b1dec8..68665ac11662 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithParenthesisExprSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithParenthesisExprSurrounder.java
@@ -34,6 +34,6 @@ public class GroovyWithParenthesisExprSurrounder extends GroovyExpressionSurroun
}
public String getTemplateDescription() {
- return "(...)";
+ return "(expr)";
}
}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithTypeCastSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithTypeCastSurrounder.java
index 27825d5635b5..8770d3f7497f 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithTypeCastSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithTypeCastSurrounder.java
@@ -42,6 +42,6 @@ public class GroovyWithTypeCastSurrounder extends GroovyExpressionSurrounder {
}
public String getTemplateDescription() {
- return "((Type) ...)";
+ return "((Type) expr)";
}
}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithWithExprSurrounder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithWithExprSurrounder.java
index cb9789fecff1..97d1a9fdae8f 100644
--- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithWithExprSurrounder.java
+++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/surroundWith/surrounders/surroundersImpl/expressions/GroovyWithWithExprSurrounder.java
@@ -42,6 +42,6 @@ public class GroovyWithWithExprSurrounder extends GroovyExpressionSurrounder {
}
public String getTemplateDescription() {
- return "with (...)";
+ return "with (expr)";
}
}
\ No newline at end of file
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 b5d70c46ec7e..c3f4d131099f 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 testTry_finally1() throws Exception { doTest(new GroovyWithTryFinallySurrounder()); }
public void testWhile1() throws Exception { doTest(new GroovyWithWhileSurrounder()); }
public void testWith2() throws Exception { doTest(new GroovyWithWithStatementsSurrounder()); }
+ public void testFor1() throws Exception { doTest(new GroovyWithForSurrounder()); }
}
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderOrderTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderOrderTest.groovy
new file mode 100644
index 000000000000..35a4ba227a7b
--- /dev/null
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurrounderOrderTest.groovy
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.surroundWith
+
+import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
+import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler
+import com.intellij.openapi.actionSystem.Separator
+
+/**
+ * @author peter
+ */
+class SurrounderOrderTest extends LightCodeInsightFixtureTestCase {
+
+ public void testStatementSurrounders() {
+ myFixture.configureByText("a.groovy", "println a")
+
+ def actions = SurroundWithHandler.buildSurroundActions(project, myFixture.editor, myFixture.file, null)
+ def names = []
+ for (action in actions) {
+ if (action instanceof Separator) {
+ break
+ }
+
+ def text = action.templatePresentation.text
+ names << text.substring(text.indexOf('. ') + 2)
+ }
+ assertOrderedEquals names,
+ "if", "if / else", "while",
+ "{ -> ... }.call()",
+ "for", "try / catch", "try / finally", "try / catch / finally",
+ "shouldFail () {...}",
+ "(expr)", "((Type) expr)", "with (expr)",
+ "with () {...}"
+ }
+
+}
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/surround/statements/for1.test b/plugins/groovy/testdata/groovy/surround/statements/for1.test
new file mode 100644
index 000000000000..8b3c47b86a4b
--- /dev/null
+++ b/plugins/groovy/testdata/groovy/surround/statements/for1.test
@@ -0,0 +1,5 @@
+println "foo"
+-----
+for () {
+ println "foo"
+}
\ No newline at end of file