Quickfix for UNSAFE_INFIX_CALL
This commit is contained in:
committed by
Evgeny Gerashchenko
parent
69de91aa88
commit
c2f5743cf0
@@ -157,6 +157,7 @@ public class QuickFixes {
|
||||
|
||||
actions.put(UNSAFE_CALL, ExclExclCallFix.introduceExclExclCall());
|
||||
actions.put(UNNECESSARY_NOT_NULL_ASSERTION, ExclExclCallFix.removeExclExclCall());
|
||||
factories.put(UNSAFE_INFIX_CALL, ReplaceInfixCallFix.createFactory());
|
||||
|
||||
JetIntentionActionFactory removeProtectedModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(PROTECTED_KEYWORD);
|
||||
factories.put(PACKAGE_MEMBER_CANNOT_BE_PROTECTED, removeProtectedModifierFactory);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
public class ReplaceInfixCallFix extends JetIntentionAction<JetBinaryExpression> {
|
||||
public ReplaceInfixCallFix(@NotNull JetBinaryExpression element) {
|
||||
super(element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return JetBundle.message("replace.with.safe.call");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
String newText = element.getLeft().getText() + "?." + element.getOperationReference().getText()
|
||||
+ "(" + element.getRight().getText() + ")";
|
||||
JetQualifiedExpression newElement = (JetQualifiedExpression) JetPsiFactory.createExpression(project, newText);
|
||||
element.replace(newElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static JetIntentionActionFactory createFactory() {
|
||||
return new JetIntentionActionFactory() {
|
||||
@Override
|
||||
public IntentionAction createAction(Diagnostic diagnostic) {
|
||||
JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class);
|
||||
return new ReplaceInfixCallFix(expression);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Replace with safe (?.) call" "true"
|
||||
fun test(a : Int?) : Int? {
|
||||
return a?.compareTo(6);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Replace with safe (?.) call" "true"
|
||||
fun test(a : Int?) : Int? {
|
||||
return a <caret>compareTo 6;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/quickfix")
|
||||
@InnerTestClasses({QuickFixMultiFileTestGenerated.AddStarProjections.class, QuickFixMultiFileTestGenerated.AutoImports.class, QuickFixMultiFileTestGenerated.Modifiers.class, QuickFixMultiFileTestGenerated.TypeImports.class, QuickFixMultiFileTestGenerated.Variables.class})
|
||||
@InnerTestClasses({QuickFixMultiFileTestGenerated.AddStarProjections.class, QuickFixMultiFileTestGenerated.AutoImports.class, QuickFixMultiFileTestGenerated.Modifiers.class, QuickFixMultiFileTestGenerated.Nullables.class, QuickFixMultiFileTestGenerated.TypeImports.class, QuickFixMultiFileTestGenerated.Variables.class})
|
||||
public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInQuickfix() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
@@ -178,6 +178,20 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/nullables")
|
||||
@InnerTestClasses({})
|
||||
public static class Nullables extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInNullables() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/nullables"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
TestSuite suite = new TestSuite("Nullables");
|
||||
suite.addTestSuite(Nullables.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/typeImports")
|
||||
public static class TypeImports extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInTypeImports() throws Exception {
|
||||
@@ -211,6 +225,7 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
suite.addTest(AddStarProjections.innerSuite());
|
||||
suite.addTestSuite(AutoImports.class);
|
||||
suite.addTest(Modifiers.innerSuite());
|
||||
suite.addTest(Nullables.innerSuite());
|
||||
suite.addTestSuite(TypeImports.class);
|
||||
suite.addTest(Variables.innerSuite());
|
||||
return suite;
|
||||
|
||||
@@ -561,6 +561,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/nullables")
|
||||
@InnerTestClasses({Nullables.UnsafeInfixCall.class})
|
||||
public static class Nullables extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInNullables() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/nullables"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
@@ -581,6 +582,25 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest("idea/testData/quickfix/nullables/beforeRemoveSupertypeNullable2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/nullables/unsafeInfixCall")
|
||||
public static class UnsafeInfixCall extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInUnsafeInfixCall() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/nullables/unsafeInfixCall"), Pattern.compile("^before(\\w+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeUnsafeInfixCall.kt")
|
||||
public void testUnsafeInfixCall() throws Exception {
|
||||
doTest("idea/testData/quickfix/nullables/unsafeInfixCall/beforeUnsafeInfixCall.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
TestSuite suite = new TestSuite("Nullables");
|
||||
suite.addTestSuite(Nullables.class);
|
||||
suite.addTestSuite(UnsafeInfixCall.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/override")
|
||||
@@ -890,7 +910,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
suite.addTestSuite(Expressions.class);
|
||||
suite.addTestSuite(Migration.class);
|
||||
suite.addTest(Modifiers.innerSuite());
|
||||
suite.addTestSuite(Nullables.class);
|
||||
suite.addTest(Nullables.innerSuite());
|
||||
suite.addTestSuite(Override.class);
|
||||
suite.addTestSuite(TypeAddition.class);
|
||||
suite.addTestSuite(TypeImports.class);
|
||||
|
||||
Reference in New Issue
Block a user