Add surround with not
This commit is contained in:
@@ -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) {
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil;
|
||||
public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
|
||||
|
||||
private static final Surrounder[] SURROUNDERS = {
|
||||
|
||||
new KotlinNotSurrounder()
|
||||
};
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<selection>1 == 2</selection>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
!(1 == 2)<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<caret>1 == 2
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
!(1 == 2)<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
if (<selection>1 == 2</selection>) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
if (!(1 == 2)<caret>) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<selection>!(1 == 2)</selection>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
!(!(1 == 2))<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<selection>1 == 2 && 3 != 4</selection>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
!(1 == 2 && 3 != 4)<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<caret>1 == 2 && 3 != 4
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
!(1 == 2 && 3 != 4)<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val a = ""
|
||||
|
||||
<selection>a</selection>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val a = ""
|
||||
|
||||
!(a)<caret>
|
||||
}
|
||||
+11
-4
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
+74
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user