From 814ca0385a02cf0250a3dbb55e4b2cddcb146700 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Thu, 14 Feb 2013 14:43:36 +0400 Subject: [PATCH] Add string template surrounder --- .../jet/generators/tests/GenerateTests.java | 7 +++ .../jetbrains/jet/plugin/JetBundle.properties | 2 + .../KotlinExpressionSurroundDescriptor.java | 1 + .../KotlinStringTemplateSurrounder.java | 63 +++++++++++++++++++ .../stringTemplate/multiExpression.kt | 6 ++ .../stringTemplate/multiExpression.kt.after | 6 ++ .../stringTemplate/multiExpressionConstant.kt | 3 + .../multiExpressionConstant.kt.after | 3 + .../stringTemplate/singleConstant.kt | 3 + .../stringTemplate/singleConstant.kt.after | 3 + .../stringTemplate/singleExpression.kt | 4 ++ .../stringTemplate/singleExpression.kt.after | 4 ++ .../AbstractSurroundWithTest.java | 5 ++ ...rroundWithStringTemplateTestGenerated.java | 59 +++++++++++++++++ 14 files changed, 169 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt create mode 100644 idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt.after create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithStringTemplateTestGenerated.java diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 5a01260effd..eaffc046157 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -250,6 +250,13 @@ public class GenerateTests { AbstractSurroundWithTest.class, testModel("idea/testData/codeInsight/surroundWith/parentheses", "doTestWithParenthesesSurrounder") ); + + generateTest( + "idea/tests/", + "SurroundWithStringTemplateTestGenerated", + AbstractSurroundWithTest.class, + testModel("idea/testData/codeInsight/surroundWith/stringTemplate", "doTestWithStringTemplateSurrounder") + ); } 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 a7b31a008af..a9416940fd6 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -115,3 +115,5 @@ move.when.else.branch.to.the.end.action=Move else branch to the end move.when.else.branch.to.the.end.family.name=Move Else Branch to the End change.to.property.name.family.name=Change to property name change.to.property.name.action=Change ''{0}'' to ''{1}'' + +surround.with.string.template="${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 0b57b9fac57..8627e6bf741 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 @@ -28,6 +28,7 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { private static final Surrounder[] SURROUNDERS = { new KotlinNotSurrounder(), + new KotlinStringTemplateSurrounder(), new KotlinParenthesesSurrounder() }; diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java new file mode 100644 index 00000000000..19e64c7429d --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java @@ -0,0 +1,63 @@ +/* + * 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.psi.*; +import org.jetbrains.jet.plugin.JetBundle; + +public class KotlinStringTemplateSurrounder extends KotlinExpressionSurrounder { + @Override + public String getTemplateDescription() { + return JetBundle.message("surround.with.string.template"); + } + + @Override + public boolean isApplicable(@NotNull JetExpression expression) { + return !(expression instanceof JetStringTemplateExpression); + } + + @Nullable + @Override + public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) { + JetStringTemplateExpression stringTemplateExpression = (JetStringTemplateExpression)JetPsiFactory.createExpression(project, getCodeTemplate(expression)); + JetStringTemplateEntry templateEntry = stringTemplateExpression.getEntries()[0]; + JetExpression innerExpression = templateEntry.getExpression(); + assert innerExpression != null : "JetExpression should exists for " + stringTemplateExpression.toString(); + innerExpression.replace(expression); + + expression = (JetExpression) expression.replace(stringTemplateExpression); + + CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression); + + int offset = expression.getTextRange().getEndOffset(); + return new TextRange(offset, offset); + } + + private String getCodeTemplate(JetExpression expression) { + if (expression.getChildren().length > 0 || + expression instanceof JetConstantExpression) { + return "\"${a}\""; + } + return "\"$a\""; + } +} diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt new file mode 100644 index 00000000000..cf136fd065c --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt @@ -0,0 +1,6 @@ +fun foo() { + val a = 1 + val b = 2 + + a + b +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt.after b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt.after new file mode 100644 index 00000000000..e9381353948 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt.after @@ -0,0 +1,6 @@ +fun foo() { + val a = 1 + val b = 2 + + "${a + b}" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt new file mode 100644 index 00000000000..b22c6d868c2 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = 1 * 3 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt.after b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt.after new file mode 100644 index 00000000000..cadf68042ce --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt.after @@ -0,0 +1,3 @@ +fun foo() { + val a = "${1 * 3}" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt b/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt new file mode 100644 index 00000000000..1cf8bd1a539 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = 1 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt.after b/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt.after new file mode 100644 index 00000000000..9323ef5d823 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt.after @@ -0,0 +1,3 @@ +fun foo() { + val a = "${1}" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt b/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt new file mode 100644 index 00000000000..34d5b4c8fe8 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt @@ -0,0 +1,4 @@ +fun foo() { + val a = "ddd" + a +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt.after new file mode 100644 index 00000000000..13608198607 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt.after @@ -0,0 +1,4 @@ +fun foo() { + val a = "ddd" + "$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 index 263ebce424a..c85e05516fb 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -22,6 +22,7 @@ import com.intellij.testFramework.LightCodeInsightTestCase; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinNotSurrounder; import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinParenthesesSurrounder; +import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinStringTemplateSurrounder; import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfElseSurrounder; import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfSurrounder; @@ -43,6 +44,10 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase doTest(path, new KotlinParenthesesSurrounder()); } + public void doTestWithStringTemplateSurrounder(String path) throws Exception { + doTest(path, new KotlinStringTemplateSurrounder()); + } + private void doTest(String path, Surrounder surrounder) throws Exception{ configureByFile(path); SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder); diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithStringTemplateTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithStringTemplateTestGenerated.java new file mode 100644 index 00000000000..498c491bce5 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithStringTemplateTestGenerated.java @@ -0,0 +1,59 @@ +/* + * 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/stringTemplate") +public class SurroundWithStringTemplateTestGenerated extends AbstractSurroundWithTest { + public void testAllFilesPresentInStringTemplate() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/stringTemplate"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("multiExpression.kt") + public void testMultiExpression() throws Exception { + doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt"); + } + + @TestMetadata("multiExpressionConstant.kt") + public void testMultiExpressionConstant() throws Exception { + doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt"); + } + + @TestMetadata("singleConstant.kt") + public void testSingleConstant() throws Exception { + doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt"); + } + + @TestMetadata("singleExpression.kt") + public void testSingleExpression() throws Exception { + doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt"); + } + +}