From 1786c2c2565a3b3833df1804fc946775267138ad Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Mon, 11 Feb 2013 13:14:18 +0400 Subject: [PATCH] Add surround with if --- .../jet/generators/tests/GenerateTests.java | 8 ++ .../surroundWith/KotlinIfSurrounder.java | 86 +++++++++++++++++++ .../KotlinStatementSurroundDescriptor.java | 2 +- .../surroundWith/KotlinSurrounderUtils.java | 16 ++++ .../codeInsight/surroundWith/if/block.kt | 7 ++ .../surroundWith/if/block.kt.after | 9 ++ .../surroundWith/if/severalStatements.kt | 4 + .../if/severalStatements.kt.after | 6 ++ .../surroundWith/if/singleStatement.kt | 3 + .../surroundWith/if/singleStatement.kt.after | 5 ++ .../surroundWith/if/singleStatementAtCaret.kt | 3 + .../if/singleStatementAtCaret.kt.after | 5 ++ .../codeInsight/surroundWith/if/variable.kt | 5 ++ .../surroundWith/if/variable.kt.after | 7 ++ .../AbstractSurroundWithTest.java | 36 ++++++++ .../SurroundWithIfTestGenerated.java | 64 ++++++++++++++ 16 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinSurrounderUtils.java create mode 100644 idea/testData/codeInsight/surroundWith/if/block.kt create mode 100644 idea/testData/codeInsight/surroundWith/if/block.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/if/severalStatements.kt create mode 100644 idea/testData/codeInsight/surroundWith/if/severalStatements.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/if/singleStatement.kt create mode 100644 idea/testData/codeInsight/surroundWith/if/singleStatement.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt create mode 100644 idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/if/variable.kt create mode 100644 idea/testData/codeInsight/surroundWith/if/variable.kt.after create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfTestGenerated.java diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 23e29daad6a..f2edc1f3749 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -29,6 +29,7 @@ import org.jetbrains.jet.jvm.compiler.*; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveTest; +import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest; import org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest; import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest; import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest; @@ -221,6 +222,13 @@ public class GenerateTests { AbstractDeprecatedHighlightingTest.class, testModel("idea/testData/highlighter/deprecated") ); + + generateTest( + "idea/tests/", + "SurroundWithIfTestGenerated", + AbstractSurroundWithTest.class, + testModel("idea/testData/codeInsight/surroundWith/if", "doTestWithIfSurrounder") + ); } private static SimpleTestClassModel testModel(@NotNull String rootPath) { diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.java new file mode 100644 index 00000000000..28a4d153b85 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinIfSurrounder.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.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 String getTemplateDescription() { + return CodeInsightBundle.message("surround.with.if.template"); + } + + @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; + } +} \ 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 1c06498c291..1edd8ff9f97 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,7 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil; public class KotlinStatementSurroundDescriptor implements SurroundDescriptor { private static final Surrounder[] SURROUNDERS = { - + new KotlinIfSurrounder() }; @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinSurrounderUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinSurrounderUtils.java new file mode 100644 index 00000000000..559ac7ec40b --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinSurrounderUtils.java @@ -0,0 +1,16 @@ +package org.jetbrains.jet.plugin.codeInsight.surroundWith; + +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetBlockExpression; + +public class KotlinSurrounderUtils { + + public static void addStatementsInBlock( + @NotNull JetBlockExpression block, + @NotNull PsiElement[] statements + ) { + PsiElement lBrace = block.getFirstChild(); + block.addRangeAfter(statements[0], statements[statements.length - 1], lBrace); + } +} diff --git a/idea/testData/codeInsight/surroundWith/if/block.kt b/idea/testData/codeInsight/surroundWith/if/block.kt new file mode 100644 index 00000000000..6fc1b551ca2 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/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/if/block.kt.after b/idea/testData/codeInsight/surroundWith/if/block.kt.after new file mode 100644 index 00000000000..699c692c649 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/block.kt.after @@ -0,0 +1,9 @@ +fun foo() { + val a: String? = null + + if () { + if (a != null) { + a.length() + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/severalStatements.kt b/idea/testData/codeInsight/surroundWith/if/severalStatements.kt new file mode 100644 index 00000000000..5bbbf0887bf --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/severalStatements.kt @@ -0,0 +1,4 @@ +fun foo() { + "".capitalize() + "".capitalize() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/severalStatements.kt.after b/idea/testData/codeInsight/surroundWith/if/severalStatements.kt.after new file mode 100644 index 00000000000..8935c942f3c --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/severalStatements.kt.after @@ -0,0 +1,6 @@ +fun foo() { + if () { + "".capitalize() + "".capitalize() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/singleStatement.kt b/idea/testData/codeInsight/surroundWith/if/singleStatement.kt new file mode 100644 index 00000000000..d3e81e15b20 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/singleStatement.kt @@ -0,0 +1,3 @@ +fun foo() { + "".capitalize() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/singleStatement.kt.after b/idea/testData/codeInsight/surroundWith/if/singleStatement.kt.after new file mode 100644 index 00000000000..324baf2da94 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/singleStatement.kt.after @@ -0,0 +1,5 @@ +fun foo() { + if () { + "".capitalize() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt b/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt new file mode 100644 index 00000000000..c17ee77f60a --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt @@ -0,0 +1,3 @@ +fun foo() { + "".capitalize() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt.after b/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt.after new file mode 100644 index 00000000000..324baf2da94 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt.after @@ -0,0 +1,5 @@ +fun foo() { + if () { + "".capitalize() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/variable.kt b/idea/testData/codeInsight/surroundWith/if/variable.kt new file mode 100644 index 00000000000..f230c142192 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/variable.kt @@ -0,0 +1,5 @@ +fun foo() { + val a = "" + + a +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/if/variable.kt.after b/idea/testData/codeInsight/surroundWith/if/variable.kt.after new file mode 100644 index 00000000000..9036a291f26 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/variable.kt.after @@ -0,0 +1,7 @@ +fun foo() { + val a = "" + + if () { + a + } +} \ 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 new file mode 100644 index 00000000000..52d41fb2ace --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -0,0 +1,36 @@ +/* + * 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.generation.surroundWith.SurroundWithHandler; +import com.intellij.testFramework.LightCodeInsightTestCase; +import org.jetbrains.annotations.NotNull; + +public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase { + + public void doTestWithIfSurrounder(String path) throws Exception { + configureByFile(path); + SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), new KotlinIfSurrounder()); + checkResultByFile(path + ".after"); + } + + @NotNull + @Override + protected String getTestDataPath() { + return ""; + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfTestGenerated.java new file mode 100644 index 00000000000..9d2cc1f04fc --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithIfTestGenerated.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/if") +public class SurroundWithIfTestGenerated extends AbstractSurroundWithTest { + public void testAllFilesPresentInIf() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("block.kt") + public void testBlock() throws Exception { + doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/block.kt"); + } + + @TestMetadata("severalStatements.kt") + public void testSeveralStatements() throws Exception { + doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/severalStatements.kt"); + } + + @TestMetadata("singleStatement.kt") + public void testSingleStatement() throws Exception { + doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/singleStatement.kt"); + } + + @TestMetadata("singleStatementAtCaret.kt") + public void testSingleStatementAtCaret() throws Exception { + doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt"); + } + + @TestMetadata("variable.kt") + public void testVariable() throws Exception { + doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/variable.kt"); + } + +}