diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
index f2edc1f3749..3cb4ab53219 100644
--- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
+++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java
@@ -229,6 +229,13 @@ public class GenerateTests {
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/if", "doTestWithIfSurrounder")
);
+
+ generateTest(
+ "idea/tests/",
+ "SurroundWithIfElseTestGenerated",
+ AbstractSurroundWithTest.class,
+ testModel("idea/testData/codeInsight/surroundWith/ifElse", "doTestWithIfElseSurrounder")
+ );
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfElseSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfElseSurrounder.java
new file mode 100644
index 00000000000..4839b1ba814
--- /dev/null
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfElseSurrounder.java
@@ -0,0 +1,35 @@
+/*
+ * 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 com.intellij.codeInsight.CodeInsightBundle;
+import org.jetbrains.annotations.NotNull;
+
+public class KotlinIfElseSurrounder extends KotlinIfSurrounderBase {
+
+ @Override
+ public String getTemplateDescription() {
+ return CodeInsightBundle.message("surround.with.ifelse.template");
+ }
+
+ @NotNull
+ @Override
+ protected String getCodeTemplate() {
+ return "if (a) { \n} else { \n}";
+ }
+}
\ No newline at end of file
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java
index 28a4d153b85..a3f60ed7710 100644
--- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java
@@ -18,69 +18,18 @@ package org.jetbrains.jet.plugin.codeInsight.surroundWith;
import com.intellij.codeInsight.CodeInsightBundle;
-import com.intellij.codeInsight.CodeInsightUtilBase;
-import com.intellij.lang.surroundWith.Surrounder;
-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.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import org.jetbrains.jet.lang.psi.JetBlockExpression;
-import org.jetbrains.jet.lang.psi.JetExpression;
-import org.jetbrains.jet.lang.psi.JetIfExpression;
-import org.jetbrains.jet.lang.psi.JetPsiFactory;
-public class KotlinIfSurrounder implements Surrounder {
+public class KotlinIfSurrounder extends KotlinIfSurrounderBase {
+
+ @Override
public String getTemplateDescription() {
return CodeInsightBundle.message("surround.with.if.template");
}
+ @NotNull
@Override
- public boolean isApplicable(@NotNull PsiElement[] elements) {
- return true;
- }
-
- @Nullable
- @Override
- public TextRange surroundElements(
- @NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements
- ) throws IncorrectOperationException {
- PsiElement container = elements[0].getParent();
- if (container == null) return null;
- return surroundStatements (project, editor, container, elements);
- }
-
- public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
- // TODO extract variables declaration
-
- if (statements.length == 0){
- return null;
- }
-
- String text = "if (a) { \n}";
- JetIfExpression ifExpression = (JetIfExpression) JetPsiFactory.createExpression(project, text);
- ifExpression = (JetIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
-
- // TODO move a comment for first statement
-
- final JetBlockExpression thenBranch = (JetBlockExpression) ifExpression.getThen();
- assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
- // Add statements in then branch of created if
- KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
-
- // Delete statements from original code
- container.deleteChildRange(statements[0], statements[statements.length - 1]);
-
- ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
-
- JetExpression condition = ifExpression.getCondition();
- assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
- // Delete condition from created if
- TextRange range = condition.getTextRange();
- TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
- editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
- return textRange;
+ protected String getCodeTemplate() {
+ return "if (a) { \n}";
}
}
\ No newline at end of file
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounderBase.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounderBase.java
new file mode 100644
index 00000000000..7c0719e868b
--- /dev/null
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounderBase.java
@@ -0,0 +1,86 @@
+/*
+ * 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 com.intellij.codeInsight.CodeInsightUtilBase;
+import com.intellij.lang.surroundWith.Surrounder;
+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.util.IncorrectOperationException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.jet.lang.psi.JetBlockExpression;
+import org.jetbrains.jet.lang.psi.JetExpression;
+import org.jetbrains.jet.lang.psi.JetIfExpression;
+import org.jetbrains.jet.lang.psi.JetPsiFactory;
+
+import java.lang.String;
+
+public abstract class KotlinIfSurrounderBase implements Surrounder {
+
+ @Override
+ public boolean isApplicable(@NotNull PsiElement[] elements) {
+ return true;
+ }
+
+ @Nullable
+ @Override
+ public TextRange surroundElements(
+ @NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements
+ ) throws IncorrectOperationException {
+ PsiElement container = elements[0].getParent();
+ if (container == null) return null;
+ return surroundStatements (project, editor, container, elements);
+ }
+
+ public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
+ // TODO extract variables declaration
+
+ if (statements.length == 0){
+ return null;
+ }
+
+ JetIfExpression ifExpression = (JetIfExpression) JetPsiFactory.createExpression(project, getCodeTemplate());
+ ifExpression = (JetIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
+
+ // TODO move a comment for first statement
+
+ final JetBlockExpression thenBranch = (JetBlockExpression) ifExpression.getThen();
+ assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
+ // Add statements in then branch of created if
+ KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
+
+ // Delete statements from original code
+ container.deleteChildRange(statements[0], statements[statements.length - 1]);
+
+ ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
+
+ JetExpression condition = ifExpression.getCondition();
+ assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
+ // Delete condition from created if
+ TextRange range = condition.getTextRange();
+ TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
+ editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
+ return textRange;
+ }
+
+ @NotNull
+ protected abstract String getCodeTemplate();
+}
\ No newline at end of file
diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinStatementSurroundDescriptor.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinStatementSurroundDescriptor.java
index 1edd8ff9f97..48690de6e63 100644
--- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinStatementSurroundDescriptor.java
+++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinStatementSurroundDescriptor.java
@@ -26,7 +26,8 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil;
public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
private static final Surrounder[] SURROUNDERS = {
- new KotlinIfSurrounder()
+ new KotlinIfSurrounder(),
+ new KotlinIfElseSurrounder()
};
@NotNull
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/block.kt b/idea/testData/codeInsight/surroundWith/ifElse/block.kt
new file mode 100644
index 00000000000..6fc1b551ca2
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/block.kt
@@ -0,0 +1,7 @@
+fun foo() {
+ val a: String? = null
+
+ if (a != null) {
+ a.length()
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/block.kt.after b/idea/testData/codeInsight/surroundWith/ifElse/block.kt.after
new file mode 100644
index 00000000000..a7f1778ee6c
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/block.kt.after
@@ -0,0 +1,10 @@
+fun foo() {
+ val a: String? = null
+
+ if () {
+ if (a != null) {
+ a.length()
+ }
+ } else {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt b/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt
new file mode 100644
index 00000000000..5bbbf0887bf
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt
@@ -0,0 +1,4 @@
+fun foo() {
+ "".capitalize()
+ "".capitalize()
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt.after b/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt.after
new file mode 100644
index 00000000000..bba7abdb63a
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt.after
@@ -0,0 +1,7 @@
+fun foo() {
+ if () {
+ "".capitalize()
+ "".capitalize()
+ } else {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt b/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt
new file mode 100644
index 00000000000..d3e81e15b20
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt
@@ -0,0 +1,3 @@
+fun foo() {
+ "".capitalize()
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt.after b/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt.after
new file mode 100644
index 00000000000..62a7a34e118
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt.after
@@ -0,0 +1,6 @@
+fun foo() {
+ if () {
+ "".capitalize()
+ } else {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt b/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt
new file mode 100644
index 00000000000..c17ee77f60a
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt
@@ -0,0 +1,3 @@
+fun foo() {
+ "".capitalize()
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt.after b/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt.after
new file mode 100644
index 00000000000..62a7a34e118
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt.after
@@ -0,0 +1,6 @@
+fun foo() {
+ if () {
+ "".capitalize()
+ } else {
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/variable.kt b/idea/testData/codeInsight/surroundWith/ifElse/variable.kt
new file mode 100644
index 00000000000..f230c142192
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/variable.kt
@@ -0,0 +1,5 @@
+fun foo() {
+ val a = ""
+
+ a
+}
\ No newline at end of file
diff --git a/idea/testData/codeInsight/surroundWith/ifElse/variable.kt.after b/idea/testData/codeInsight/surroundWith/ifElse/variable.kt.after
new file mode 100644
index 00000000000..37ef89af9a2
--- /dev/null
+++ b/idea/testData/codeInsight/surroundWith/ifElse/variable.kt.after
@@ -0,0 +1,8 @@
+fun foo() {
+ val a = ""
+
+ if () {
+ a
+ } else {
+ }
+}
\ No newline at end of file
diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java
index 52d41fb2ace..6eb9a4219db 100644
--- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java
+++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java
@@ -28,6 +28,12 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
checkResultByFile(path + ".after");
}
+ public void doTestWithIfElseSurrounder(String path) throws Exception {
+ configureByFile(path);
+ SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), new KotlinIfElseSurrounder());
+ checkResultByFile(path + ".after");
+ }
+
@NotNull
@Override
protected String getTestDataPath() {
diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfElseTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfElseTestGenerated.java
new file mode 100644
index 00000000000..14770caabf7
--- /dev/null
+++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfElseTestGenerated.java
@@ -0,0 +1,64 @@
+/*
+ * 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/ifElse")
+public class SurroundWithIfElseTestGenerated extends AbstractSurroundWithTest {
+ public void testAllFilesPresentInIfElse() throws Exception {
+ JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/ifElse"), Pattern.compile("^(.+)\\.kt$"), true);
+ }
+
+ @TestMetadata("block.kt")
+ public void testBlock() throws Exception {
+ doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/block.kt");
+ }
+
+ @TestMetadata("severalStatements.kt")
+ public void testSeveralStatements() throws Exception {
+ doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt");
+ }
+
+ @TestMetadata("singleStatement.kt")
+ public void testSingleStatement() throws Exception {
+ doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt");
+ }
+
+ @TestMetadata("singleStatementAtCaret.kt")
+ public void testSingleStatementAtCaret() throws Exception {
+ doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt");
+ }
+
+ @TestMetadata("variable.kt")
+ public void testVariable() throws Exception {
+ doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/variable.kt");
+ }
+
+}