From 856bffb3245aaf18558bf977e981110cb327dc04 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Thu, 14 Feb 2013 13:37:20 +0400 Subject: [PATCH] Add parentheses surrounder --- .../jet/generators/tests/GenerateTests.java | 7 +++ .../KotlinExpressionSurroundDescriptor.java | 3 +- .../KotlinParenthesesSurrounder.java | 40 ++++++++++++++ .../surroundWith/parentheses/expr.kt | 3 ++ .../surroundWith/parentheses/expr.kt.after | 3 ++ .../surroundWith/parentheses/inIf.kt | 5 ++ .../surroundWith/parentheses/inIf.kt.after | 5 ++ .../surroundWith/parentheses/partOfExpr.kt | 3 ++ .../parentheses/partOfExpr.kt.after | 3 ++ .../AbstractSurroundWithTest.java | 5 ++ .../SurroundWithParenthesesTestGenerated.java | 54 +++++++++++++++++++ 11 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/expr.kt create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/expr.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/inIf.kt create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/inIf.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt create mode 100644 idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt.after create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithParenthesesTestGenerated.java diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index cec06fe9464..5a01260effd 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -243,6 +243,13 @@ public class GenerateTests { AbstractSurroundWithTest.class, testModel("idea/testData/codeInsight/surroundWith/not", "doTestWithNotSurrounder") ); + + generateTest( + "idea/tests/", + "SurroundWithParenthesesTestGenerated", + AbstractSurroundWithTest.class, + testModel("idea/testData/codeInsight/surroundWith/parentheses", "doTestWithParenthesesSurrounder") + ); } private static SimpleTestClassModel testModel(@NotNull String rootPath) { 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 28a3e99df83..0b57b9fac57 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 @@ -27,7 +27,8 @@ import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils; public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { private static final Surrounder[] SURROUNDERS = { - new KotlinNotSurrounder() + new KotlinNotSurrounder(), + new KotlinParenthesesSurrounder() }; @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java new file mode 100644 index 00000000000..94195364fee --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java @@ -0,0 +1,40 @@ +package org.jetbrains.jet.plugin.codeInsight.surroundWith.expression; + +import com.intellij.codeInsight.CodeInsightBundle; +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.JetExpression; +import org.jetbrains.jet.lang.psi.JetParenthesizedExpression; +import org.jetbrains.jet.lang.psi.JetPsiFactory; + +public class KotlinParenthesesSurrounder extends KotlinExpressionSurrounder { + @Override + public String getTemplateDescription() { + return CodeInsightBundle.message("surround.with.parenthesis.template"); + } + + @Override + public boolean isApplicable(@NotNull JetExpression expression) { + return true; + } + + @Nullable + @Override + public TextRange surroundExpression( @NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) { + JetParenthesizedExpression parenthesizedExpression = (JetParenthesizedExpression)JetPsiFactory.createExpression(project, "(a)"); + JetExpression expressionWithoutParentheses = parenthesizedExpression.getExpression(); + assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression"; + expressionWithoutParentheses.replace(expression); + + expression = (JetExpression) expression.replace(parenthesizedExpression); + + CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression); + + int offset = expression.getTextRange().getEndOffset(); + return new TextRange(offset, offset); + } +} diff --git a/idea/testData/codeInsight/surroundWith/parentheses/expr.kt b/idea/testData/codeInsight/surroundWith/parentheses/expr.kt new file mode 100644 index 00000000000..c6b7b33c6c7 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/expr.kt @@ -0,0 +1,3 @@ +fun foo() { + true +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/parentheses/expr.kt.after b/idea/testData/codeInsight/surroundWith/parentheses/expr.kt.after new file mode 100644 index 00000000000..54f770bb28e --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/expr.kt.after @@ -0,0 +1,3 @@ +fun foo() { + (true) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt b/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt new file mode 100644 index 00000000000..cec8cb551a9 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt @@ -0,0 +1,5 @@ +fun foo() { + val a = "test" + + if (a == "t") {} +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt.after b/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt.after new file mode 100644 index 00000000000..bcaf7b7f8f4 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/inIf.kt.after @@ -0,0 +1,5 @@ +fun foo() { + val a = "test" + + if ((a == "t")) {} +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt b/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt new file mode 100644 index 00000000000..5f7aab60013 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = 1 + 2 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt.after b/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt.after new file mode 100644 index 00000000000..804e27e7689 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt.after @@ -0,0 +1,3 @@ +fun foo() { + val a = (1 + 2) +} \ 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 26623ab6dbe..263ebce424a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -21,6 +21,7 @@ import com.intellij.lang.surroundWith.Surrounder; 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.statement.KotlinIfElseSurrounder; import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfSurrounder; @@ -38,6 +39,10 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase doTest(path, new KotlinNotSurrounder()); } + public void doTestWithParenthesesSurrounder(String path) throws Exception { + doTest(path, new KotlinParenthesesSurrounder()); + } + 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/SurroundWithParenthesesTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithParenthesesTestGenerated.java new file mode 100644 index 00000000000..476e49838ee --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithParenthesesTestGenerated.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/parentheses") +public class SurroundWithParenthesesTestGenerated extends AbstractSurroundWithTest { + public void testAllFilesPresentInParentheses() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/parentheses"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("expr.kt") + public void testExpr() throws Exception { + doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/expr.kt"); + } + + @TestMetadata("inIf.kt") + public void testInIf() throws Exception { + doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/inIf.kt"); + } + + @TestMetadata("partOfExpr.kt") + public void testPartOfExpr() throws Exception { + doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt"); + } + +}