From 09ca0207805f670b3b60212a94ce311c92abcf9d Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Tue, 12 Feb 2013 15:37:58 +0400 Subject: [PATCH] Add surround with not --- .../jet/generators/tests/GenerateTests.java | 7 ++ .../KotlinExpressionSurroundDescriptor.java | 2 +- .../surroundWith/KotlinNotSurrounder.java | 59 +++++++++++++++ .../surroundWith/not/booleanExpr.kt | 3 + .../surroundWith/not/booleanExpr.kt.after | 3 + .../surroundWith/not/booleanExprAtCaret.kt | 3 + .../not/booleanExprAtCaret.kt.after | 3 + .../surroundWith/not/expressionInIf.kt | 3 + .../surroundWith/not/expressionInIf.kt.after | 3 + .../surroundWith/not/notExpression.kt | 3 + .../surroundWith/not/notExpression.kt.after | 3 + .../surroundWith/not/severalExpr.kt | 3 + .../surroundWith/not/severalExpr.kt.after | 3 + .../surroundWith/not/severalExprAtCaret.kt | 3 + .../not/severalExprAtCaret.kt.after | 3 + .../codeInsight/surroundWith/not/variable.kt | 5 ++ .../surroundWith/not/variable.kt.after | 5 ++ .../AbstractSurroundWithTest.java | 15 +++- .../SurroundWithNotTestGenerated.java | 74 +++++++++++++++++++ 19 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinNotSurrounder.java create mode 100644 idea/testData/codeInsight/surroundWith/not/booleanExpr.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/booleanExpr.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/expressionInIf.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/expressionInIf.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/notExpression.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/notExpression.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/severalExpr.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/severalExpr.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt.after create mode 100644 idea/testData/codeInsight/surroundWith/not/variable.kt create mode 100644 idea/testData/codeInsight/surroundWith/not/variable.kt.after create mode 100644 idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithNotTestGenerated.java diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 3cb4ab53219..cec06fe9464 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -236,6 +236,13 @@ public class GenerateTests { AbstractSurroundWithTest.class, testModel("idea/testData/codeInsight/surroundWith/ifElse", "doTestWithIfElseSurrounder") ); + + generateTest( + "idea/tests/", + "SurroundWithNotTestGenerated", + AbstractSurroundWithTest.class, + testModel("idea/testData/codeInsight/surroundWith/not", "doTestWithNotSurrounder") + ); } private static SimpleTestClassModel testModel(@NotNull String rootPath) { diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinExpressionSurroundDescriptor.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinExpressionSurroundDescriptor.java index 293189b94a3..5b24b5c945e 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinExpressionSurroundDescriptor.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinExpressionSurroundDescriptor.java @@ -27,7 +27,7 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil; public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { private static final Surrounder[] SURROUNDERS = { - + new KotlinNotSurrounder() }; @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinNotSurrounder.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinNotSurrounder.java new file mode 100644 index 00000000000..474ce92ca6c --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/KotlinNotSurrounder.java @@ -0,0 +1,59 @@ +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.psi.codeStyle.CodeStyleManager; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.lazy.ResolveSession; +import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; +import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade; + +public class KotlinNotSurrounder implements Surrounder { + @Override + public String getTemplateDescription() { + return CodeInsightBundle.message("surround.with.not.template"); + } + + @Override + public boolean isApplicable(@NotNull PsiElement[] elements) { + if (elements.length != 1 || !(elements[0] instanceof JetExpression)) { + return false; + } + JetExpression expression = (JetExpression) elements[0]; + ResolveSession resolveSession = WholeProjectAnalyzerFacade.getLazyResolveSessionForFile((JetFile) expression.getContainingFile()); + BindingContext expressionBindingContext = ResolveSessionUtils.resolveToExpression(resolveSession, expression); + JetType type = expressionBindingContext.get(BindingContext.EXPRESSION_TYPE, expression); + return KotlinBuiltIns.getInstance().getBooleanType().equals(type); + } + + @Nullable + @Override + public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException { + JetExpression expr = (JetExpression) elements[0]; + + JetPrefixExpression prefixExpr = (JetPrefixExpression)JetPsiFactory.createExpression(project, "!(a)"); + JetParenthesizedExpression parenthesizedExpression = (JetParenthesizedExpression) prefixExpr.getBaseExpression(); + assert parenthesizedExpression != null : "JetParenthesizedExpression should exists for " + prefixExpr.getText() + " expression"; + JetExpression expressionWithoutParentheses = parenthesizedExpression.getExpression(); + assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression"; + expressionWithoutParentheses.replace(expr); + + expr = (JetExpression) expr.replace(prefixExpr); + + CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expr); + + int offset = expr.getTextRange().getEndOffset(); + return new TextRange(offset, offset); + } +} diff --git a/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt b/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt new file mode 100644 index 00000000000..65834e8723c --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt @@ -0,0 +1,3 @@ +fun foo() { + 1 == 2 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt.after b/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt.after new file mode 100644 index 00000000000..a0da38db83a --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/booleanExpr.kt.after @@ -0,0 +1,3 @@ +fun foo() { + !(1 == 2) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt b/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt new file mode 100644 index 00000000000..5c78c79d10e --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt @@ -0,0 +1,3 @@ +fun foo() { + 1 == 2 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt.after b/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt.after new file mode 100644 index 00000000000..a0da38db83a --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt.after @@ -0,0 +1,3 @@ +fun foo() { + !(1 == 2) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt b/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt new file mode 100644 index 00000000000..583259b9fd0 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt @@ -0,0 +1,3 @@ +fun foo() { + if (1 == 2) {} +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt.after b/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt.after new file mode 100644 index 00000000000..7e8c6e93a12 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/expressionInIf.kt.after @@ -0,0 +1,3 @@ +fun foo() { + if (!(1 == 2)) {} +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/notExpression.kt b/idea/testData/codeInsight/surroundWith/not/notExpression.kt new file mode 100644 index 00000000000..8f7dc0839a4 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/notExpression.kt @@ -0,0 +1,3 @@ +fun foo() { + !(1 == 2) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/notExpression.kt.after b/idea/testData/codeInsight/surroundWith/not/notExpression.kt.after new file mode 100644 index 00000000000..48a3ed10fed --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/notExpression.kt.after @@ -0,0 +1,3 @@ +fun foo() { + !(!(1 == 2)) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/severalExpr.kt b/idea/testData/codeInsight/surroundWith/not/severalExpr.kt new file mode 100644 index 00000000000..63df42703d0 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/severalExpr.kt @@ -0,0 +1,3 @@ +fun foo() { + 1 == 2 && 3 != 4 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/severalExpr.kt.after b/idea/testData/codeInsight/surroundWith/not/severalExpr.kt.after new file mode 100644 index 00000000000..a59cd188780 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/severalExpr.kt.after @@ -0,0 +1,3 @@ +fun foo() { + !(1 == 2 && 3 != 4) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt b/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt new file mode 100644 index 00000000000..0a5dc497aa6 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt @@ -0,0 +1,3 @@ +fun foo() { + 1 == 2 && 3 != 4 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt.after b/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt.after new file mode 100644 index 00000000000..a59cd188780 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt.after @@ -0,0 +1,3 @@ +fun foo() { + !(1 == 2 && 3 != 4) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/variable.kt b/idea/testData/codeInsight/surroundWith/not/variable.kt new file mode 100644 index 00000000000..7a73b12d7c2 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/variable.kt @@ -0,0 +1,5 @@ +fun foo() { + val a = "" + + a +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/not/variable.kt.after b/idea/testData/codeInsight/surroundWith/not/variable.kt.after new file mode 100644 index 00000000000..14fae7047f6 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/not/variable.kt.after @@ -0,0 +1,5 @@ +fun foo() { + val a = "" + + !(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 6eb9a4219db..3487a54838a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -17,20 +17,27 @@ package org.jetbrains.jet.plugin.codeInsight.surroundWith; import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler; +import com.intellij.lang.surroundWith.Surrounder; 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"); + doTest(path, new KotlinIfSurrounder()); } public void doTestWithIfElseSurrounder(String path) throws Exception { + doTest(path, new KotlinIfElseSurrounder()); + } + + public void doTestWithNotSurrounder(String path) throws Exception { + doTest(path, new KotlinNotSurrounder()); + } + + private void doTest(String path, Surrounder surrounder) throws Exception{ configureByFile(path); - SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), new KotlinIfElseSurrounder()); + SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder); checkResultByFile(path + ".after"); } diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithNotTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithNotTestGenerated.java new file mode 100644 index 00000000000..390f6c0cc67 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/surroundWith/SurroundWithNotTestGenerated.java @@ -0,0 +1,74 @@ +/* + * 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/not") +public class SurroundWithNotTestGenerated extends AbstractSurroundWithTest { + public void testAllFilesPresentInNot() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/not"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("booleanExpr.kt") + public void testBooleanExpr() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/booleanExpr.kt"); + } + + @TestMetadata("booleanExprAtCaret.kt") + public void testBooleanExprAtCaret() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/booleanExprAtCaret.kt"); + } + + @TestMetadata("expressionInIf.kt") + public void testExpressionInIf() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/expressionInIf.kt"); + } + + @TestMetadata("notExpression.kt") + public void testNotExpression() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/notExpression.kt"); + } + + @TestMetadata("severalExpr.kt") + public void testSeveralExpr() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/severalExpr.kt"); + } + + @TestMetadata("severalExprAtCaret.kt") + public void testSeveralExprAtCaret() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/severalExprAtCaret.kt"); + } + + @TestMetadata("variable.kt") + public void testVariable() throws Exception { + doTestWithNotSurrounder("idea/testData/codeInsight/surroundWith/not/variable.kt"); + } + +}