From 7c360a0ede67ea3a711c221d8fc21cbb9d7da0e6 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Thu, 14 Feb 2013 19:18:17 +0400 Subject: [PATCH] Add surround with function literal --- .../jet/generators/tests/GenerateTests.java | 7 +++ .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../KotlinFunctionLiteralSurrounder.java | 60 +++++++++++++++++++ .../KotlinStatementSurroundDescriptor.java | 1 + .../functionLiteral/multiStatement.kt | 4 ++ .../functionLiteral/multiStatement.kt.after | 6 ++ .../functionLiteral/singleStatement.kt | 3 + .../functionLiteral/singleStatement.kt.after | 5 ++ .../AbstractSurroundWithTest.java | 6 +- ...roundWithFunctionLiteralTestGenerated.java | 49 +++++++++++++++ 10 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java create mode 100644 idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt create mode 100644 idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt create mode 100644 idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt.after create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithFunctionLiteralTestGenerated.java diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 409552aab29..bc22864dd9a 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -285,6 +285,13 @@ public class GenerateTests { AbstractSurroundWithTest.class, testModel("idea/testData/codeInsight/surroundWith/tryFinally", "doTestWithTryFinallySurrounder") ); + + generateTest( + "idea/tests/", + "SurroundWithFunctionLiteralTestGenerated", + AbstractSurroundWithTest.class, + testModel("idea/testData/codeInsight/surroundWith/functionLiteral", "doTestWithFunctionLiteralSurrounder") + ); } 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 b4f6d2d0e08..d346ad0a2ee 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -118,3 +118,4 @@ change.to.property.name.action=Change ''{0}'' to ''{1}'' surround.with.string.template="${expr}" surround.with.when.template=when (expr) {} +surround.with.function.template={ } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java new file mode 100644 index 00000000000..a69f4abdb42 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java @@ -0,0 +1,60 @@ +/* + * 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.statement; + +import com.intellij.codeInsight.CodeInsightUtilBase; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils; + +public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder { + @Nullable + @Override + protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) { + // TODO extract variables declaration + + JetCallExpression callExpression = (JetCallExpression) JetPsiFactory.createExpression(project, "run {\n}"); + callExpression = (JetCallExpression) container.addAfter(callExpression, statements[statements.length - 1]); + container.addBefore(JetPsiFactory.createWhiteSpace(project), callExpression); + + JetFunctionLiteralExpression bodyExpression = (JetFunctionLiteralExpression) callExpression.getFunctionLiteralArguments().get(0); + assert bodyExpression != null : "Body expression should exists for " + callExpression.getText(); + JetBlockExpression blockExpression = bodyExpression.getBodyExpression(); + assert blockExpression != null : "JetBlockExpression should exists for " + callExpression.getText(); + //Add statements in function literal block + KotlinSurrounderUtils.addStatementsInBlock(blockExpression, statements); + + //Delete statements from original code + container.deleteChildRange(statements[0], statements[statements.length - 1]); + + callExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(callExpression); + + JetExpression literalName = callExpression.getCalleeExpression(); + assert literalName != null : "Run expression should have callee expression " + callExpression.getText(); + return literalName.getTextRange(); + } + + @Override + public String getTemplateDescription() { + return JetBundle.message("surround.with.function.template"); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinStatementSurroundDescriptor.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinStatementSurroundDescriptor.java index 166e215be09..b3eff3d4f0a 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinStatementSurroundDescriptor.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/statement/KotlinStatementSurroundDescriptor.java @@ -28,6 +28,7 @@ public class KotlinStatementSurroundDescriptor implements SurroundDescriptor { private static final Surrounder[] SURROUNDERS = { new KotlinIfSurrounder(), new KotlinIfElseSurrounder(), + new KotlinFunctionLiteralSurrounder(), new KotlinTryFinallySurrounder(), new KotlinTryCatchFinallySurrounder(), new KotlinTryCatchSurrounder() diff --git a/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt b/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt new file mode 100644 index 00000000000..9d25e44e722 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt @@ -0,0 +1,4 @@ +fun foo() { + "aaa" + "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt.after b/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt.after new file mode 100644 index 00000000000..b6ef35989c6 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt.after @@ -0,0 +1,6 @@ +fun foo() { + run { + "aaa" + "aaa" + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt b/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt new file mode 100644 index 00000000000..3a0c30ba309 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt @@ -0,0 +1,3 @@ +fun foo() { + "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt.after b/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt.after new file mode 100644 index 00000000000..88495195665 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt.after @@ -0,0 +1,5 @@ +fun foo() { + run { + "aaa" + } +} \ 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 ddff38650e5..cbb81d09e40 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -64,7 +64,11 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase doTest(path, new KotlinTryFinallySurrounder()); } - private void doTest(String path, Surrounder surrounder) throws Exception{ + public void doTestWithFunctionLiteralSurrounder(String path) throws Exception { + doTest(path, new KotlinFunctionLiteralSurrounder()); + } + + private void doTest(String path, Surrounder surrounder) throws Exception { configureByFile(path); SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder); checkResultByFile(path + ".after"); diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithFunctionLiteralTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithFunctionLiteralTestGenerated.java new file mode 100644 index 00000000000..ebc6762768e --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithFunctionLiteralTestGenerated.java @@ -0,0 +1,49 @@ +/* + * 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/functionLiteral") +public class SurroundWithFunctionLiteralTestGenerated extends AbstractSurroundWithTest { + public void testAllFilesPresentInFunctionLiteral() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/functionLiteral"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("multiStatement.kt") + public void testMultiStatement() throws Exception { + doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt"); + } + + @TestMetadata("singleStatement.kt") + public void testSingleStatement() throws Exception { + doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt"); + } + +}