diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index c8f56cfb045..0a5afe1c34e 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -29,6 +29,8 @@ remove.right.part.of.binary.expression=Remove right part of a binary expression remove.cast=Remove cast remove.elvis.operator=Remove elvis operator remove.redundant.nullable=Remove redundant '?' +remove.supertype=Remove supertype ''{0}'' +remove.supertype.family=Remove supertype remove.supertype.nullable=Remove '?' remove.type.arguments=Remove type arguments remove.useless.nullable=Remove useless '?' diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index a64153c0775..a1f32aa9acc 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -219,5 +219,7 @@ public class QuickFixes { factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch()); factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory()); + + factories.put(MANY_CLASSES_IN_SUPERTYPE_LIST, RemoveSupertypeFix.createFactory()); } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java new file mode 100644 index 00000000000..e6374d65aa4 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java @@ -0,0 +1,76 @@ +/* + * 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.lang.ASTNode; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.impl.PsiImplUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.JetDelegationSpecifier; +import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.plugin.JetBundle; + +public class RemoveSupertypeFix extends JetIntentionAction { + private final JetDelegationSpecifier superClass; + + public RemoveSupertypeFix(@NotNull JetDelegationSpecifier superClass) { + super(superClass); + this.superClass = superClass; + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("remove.supertype", superClass.getText()); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("remove.supertype.family"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + // Find the preceding comma and delete it as well. + // We must ignore whitespaces and comments when looking for the comma. + PsiElement prevSibling = superClass.getPrevSibling(); + assert prevSibling != null: "A PSI element should exist before supertype declaration"; + ASTNode prev = PsiImplUtil.skipWhitespaceAndCommentsBack(prevSibling.getNode()); + assert prev != null: "A non-whitespace element should exist before supertype declaration"; + if (prev.getElementType() == JetTokens.COMMA) { + prev.getPsi().delete(); + } + superClass.delete(); + } + + public static JetIntentionActionFactory createFactory() { + return new JetIntentionActionFactory() { + @Override + public JetIntentionAction createAction(Diagnostic diagnostic) { + JetDelegationSpecifier superClass = QuickFixUtil.getParentElementOfType(diagnostic, JetDelegationSpecifier.class); + if (superClass == null) return null; + return new RemoveSupertypeFix(superClass); + } + }; + } +} diff --git a/idea/testData/quickfix/modifiers/afterRemoveSupertype1.kt b/idea/testData/quickfix/modifiers/afterRemoveSupertype1.kt new file mode 100644 index 00000000000..03a240e4187 --- /dev/null +++ b/idea/testData/quickfix/modifiers/afterRemoveSupertype1.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1() {} diff --git a/idea/testData/quickfix/modifiers/afterRemoveSupertype2.kt b/idea/testData/quickfix/modifiers/afterRemoveSupertype2.kt new file mode 100644 index 00000000000..03a240e4187 --- /dev/null +++ b/idea/testData/quickfix/modifiers/afterRemoveSupertype2.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1() {} diff --git a/idea/testData/quickfix/modifiers/afterRemoveSupertype3.kt b/idea/testData/quickfix/modifiers/afterRemoveSupertype3.kt new file mode 100644 index 00000000000..71fdbd3e13c --- /dev/null +++ b/idea/testData/quickfix/modifiers/afterRemoveSupertype3.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2'" "true" +open class C1 {} +open class C2 {} +class C3: C1() {} diff --git a/idea/testData/quickfix/modifiers/afterRemoveSupertype4.kt b/idea/testData/quickfix/modifiers/afterRemoveSupertype4.kt new file mode 100644 index 00000000000..b53974f5811 --- /dev/null +++ b/idea/testData/quickfix/modifiers/afterRemoveSupertype4.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1() /* Hello, world! */ {} diff --git a/idea/testData/quickfix/modifiers/beforeRemoveSupertype1.kt b/idea/testData/quickfix/modifiers/beforeRemoveSupertype1.kt new file mode 100644 index 00000000000..3cc206805b7 --- /dev/null +++ b/idea/testData/quickfix/modifiers/beforeRemoveSupertype1.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1(), C2() {} diff --git a/idea/testData/quickfix/modifiers/beforeRemoveSupertype2.kt b/idea/testData/quickfix/modifiers/beforeRemoveSupertype2.kt new file mode 100644 index 00000000000..fbe4a8cee99 --- /dev/null +++ b/idea/testData/quickfix/modifiers/beforeRemoveSupertype2.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1(),C2() {} diff --git a/idea/testData/quickfix/modifiers/beforeRemoveSupertype3.kt b/idea/testData/quickfix/modifiers/beforeRemoveSupertype3.kt new file mode 100644 index 00000000000..3090283f8c2 --- /dev/null +++ b/idea/testData/quickfix/modifiers/beforeRemoveSupertype3.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2'" "true" +open class C1 {} +open class C2 {} +class C3: C1(), C2 {} diff --git a/idea/testData/quickfix/modifiers/beforeRemoveSupertype4.kt b/idea/testData/quickfix/modifiers/beforeRemoveSupertype4.kt new file mode 100644 index 00000000000..a05843d7dc0 --- /dev/null +++ b/idea/testData/quickfix/modifiers/beforeRemoveSupertype4.kt @@ -0,0 +1,4 @@ +// "Remove supertype 'C2()'" "true" +open class C1 {} +open class C2 {} +class C3: C1(), /* Hello, world! */ C2() {} diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 930ff054b97..dc8dd6053a8 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -534,6 +534,26 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/modifiers/beforeRemoveRedundantModifier3.kt"); } + @TestMetadata("beforeRemoveSupertype1.kt") + public void testRemoveSupertype1() throws Exception { + doTest("idea/testData/quickfix/modifiers/beforeRemoveSupertype1.kt"); + } + + @TestMetadata("beforeRemoveSupertype2.kt") + public void testRemoveSupertype2() throws Exception { + doTest("idea/testData/quickfix/modifiers/beforeRemoveSupertype2.kt"); + } + + @TestMetadata("beforeRemoveSupertype3.kt") + public void testRemoveSupertype3() throws Exception { + doTest("idea/testData/quickfix/modifiers/beforeRemoveSupertype3.kt"); + } + + @TestMetadata("beforeRemoveSupertype4.kt") + public void testRemoveSupertype4() throws Exception { + doTest("idea/testData/quickfix/modifiers/beforeRemoveSupertype4.kt"); + } + @TestMetadata("beforeVisibilityModifer1.kt") public void testVisibilityModifer1() throws Exception { doTest("idea/testData/quickfix/modifiers/beforeVisibilityModifer1.kt");