diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
index eaffc046157..85a0e70ec87 100644
--- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
+++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
@@ -257,6 +257,13 @@ public class GenerateTests {
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/stringTemplate", "doTestWithStringTemplateSurrounder")
);
+
+ generateTest(
+ "idea/tests/",
+ "SurroundWithWhenTestGenerated",
+ AbstractSurroundWithTest.class,
+ testModel("idea/testData/codeInsight/surroundWith/when", "doTestWithWhenSurrounder")
+ );
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties
index a9416940fd6..b4f6d2d0e08 100644
--- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties
+++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties
@@ -117,3 +117,4 @@ change.to.property.name.family.name=Change to property name
change.to.property.name.action=Change ''{0}'' to ''{1}''
surround.with.string.template="${expr}"
+surround.with.when.template=when (expr) {}
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java
index 8627e6bf741..1ab0eefe7c2 100644
--- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java
@@ -29,7 +29,8 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
private static final Surrounder[] SURROUNDERS = {
new KotlinNotSurrounder(),
new KotlinStringTemplateSurrounder(),
- new KotlinParenthesesSurrounder()
+ new KotlinParenthesesSurrounder(),
+ new KotlinWhenSurrounder()
};
@NotNull
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinWhenSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinWhenSurrounder.java
new file mode 100644
index 00000000000..1ec72e2617f
--- /dev/null
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinWhenSurrounder.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2010-2013 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.jet.plugin.codeInsight.surroundWith.expression;
+
+import com.intellij.codeInsight.CodeInsightUtilBase;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.TextRange;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
+import org.jetbrains.jet.lang.descriptors.ClassKind;
+import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
+import org.jetbrains.jet.lang.psi.*;
+import org.jetbrains.jet.lang.types.JetType;
+import org.jetbrains.jet.plugin.JetBundle;
+import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
+
+public class KotlinWhenSurrounder extends KotlinExpressionSurrounder {
+ @Override
+ public String getTemplateDescription() {
+ return JetBundle.message("surround.with.when.template");
+ }
+
+ @Override
+ public boolean isApplicable(@NotNull JetExpression expression) {
+ return true;
+ }
+
+ @Nullable
+ @Override
+ public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) {
+ JetWhenExpression whenExpression = (JetWhenExpression)JetPsiFactory.createExpression(project, getCodeTemplate(expression));
+ JetExpression subjectExpression = whenExpression.getSubjectExpression();
+ assert subjectExpression != null : "JetExpression should exists for " + whenExpression.getText() + " expression";
+ subjectExpression.replace(expression);
+
+ expression = (JetExpression) expression.replace(whenExpression);
+
+ CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
+
+ JetWhenEntry whenEntry = ((JetWhenExpression) expression).getEntries().get(0);
+ JetWhenCondition whenEntryCondition = whenEntry.getConditions()[0];
+ assert whenEntryCondition != null : "JetExpression for first entry should exists: " + expression.getText();
+ TextRange whenRange = whenEntryCondition.getTextRange();
+ editor.getDocument().deleteString(whenRange.getStartOffset(), whenRange.getEndOffset());
+ int offset = whenRange.getStartOffset();
+ return new TextRange(offset, offset);
+ }
+
+ private String getCodeTemplate(JetExpression expression) {
+ JetType type = KotlinSurrounderUtils.getExpressionType(expression);
+ if (type != null) {
+ ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
+ if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).getKind() == ClassKind.ENUM_CLASS) {
+ return "when(a) { \nb -> {}\n}";
+ }
+ }
+ return "when(a) { \nb -> {}\n else -> {}\n}";
+ }
+
+
+}
diff --git a/idea/testData/codeInsight/surroundWith/when/enum.kt b/idea/testData/codeInsight/surroundWith/when/enum.kt
new file mode 100644
index 00000000000..6b3f2dcbd87
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/enum.kt
@@ -0,0 +1,9 @@
+enum class MyEnum {
+ FIRST
+ SECOND
+}
+
+fun foo() {
+ val a = MyEnum.FIRST
+ a
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/when/enum.kt.after b/idea/testData/codeInsight/surroundWith/when/enum.kt.after
new file mode 100644
index 00000000000..35cf8400787
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/enum.kt.after
@@ -0,0 +1,12 @@
+enum class MyEnum {
+ FIRST
+ SECOND
+}
+
+fun foo() {
+ val a = MyEnum.FIRST
+ when(a) {
+ -> {
+ }
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/when/multiExpression.kt b/idea/testData/codeInsight/surroundWith/when/multiExpression.kt
new file mode 100644
index 00000000000..2d57890c26c
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/multiExpression.kt
@@ -0,0 +1,4 @@
+fun foo() {
+ val a = "aa"
+ a.capitalize()
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/when/multiExpression.kt.after b/idea/testData/codeInsight/surroundWith/when/multiExpression.kt.after
new file mode 100644
index 00000000000..93d6e61e115
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/multiExpression.kt.after
@@ -0,0 +1,9 @@
+fun foo() {
+ val a = "aa"
+ when(a.capitalize()) {
+ -> {
+ }
+ else -> {
+ }
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/when/singleExpression.kt b/idea/testData/codeInsight/surroundWith/when/singleExpression.kt
new file mode 100644
index 00000000000..44f42a64059
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/singleExpression.kt
@@ -0,0 +1,4 @@
+fun foo() {
+ val a = "aa"
+ a
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/when/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/when/singleExpression.kt.after
new file mode 100644
index 00000000000..0985fea17c6
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/when/singleExpression.kt.after
@@ -0,0 +1,9 @@
+fun foo() {
+ val a = "aa"
+ when(a) {
+ -> {
+ }
+ else -> {
+ }
+ }
+}
\ No newline at end of file
diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithWhenTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithWhenTestGenerated.java
new file mode 100644
index 00000000000..6fcb10a7fdf
--- /dev/null
+++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithWhenTestGenerated.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2010-2013 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.jet.plugin.codeInsight.surroundWith;
+
+import junit.framework.Assert;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import java.io.File;
+import java.util.regex.Pattern;
+import org.jetbrains.jet.JetTestUtils;
+import org.jetbrains.jet.test.InnerTestClasses;
+import org.jetbrains.jet.test.TestMetadata;
+
+import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest;
+
+/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
+@SuppressWarnings("all")
+@TestMetadata("idea/testData/codeInsight/surroundWith/when")
+public class SurroundWithWhenTestGenerated extends AbstractSurroundWithTest {
+ public void testAllFilesPresentInWhen() throws Exception {
+ JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/when"), Pattern.compile("^(.+)\\.kt$"), true);
+ }
+
+ @TestMetadata("enum.kt")
+ public void testEnum() throws Exception {
+ doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/enum.kt");
+ }
+
+ @TestMetadata("multiExpression.kt")
+ public void testMultiExpression() throws Exception {
+ doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/multiExpression.kt");
+ }
+
+ @TestMetadata("singleExpression.kt")
+ public void testSingleExpression() throws Exception {
+ doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/singleExpression.kt");
+ }
+
+}