From a9a52379eeba2ca29aac291d6332157b7aac0bbb Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 4 Sep 2017 23:58:32 +0900 Subject: [PATCH] Quickfix for "variable initializer is redundant" (VARIABLE_WITH_REDUNDANT_INITIALIZER) #KT-5878 Fixed (#1281) --- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 + .../quickfix/RemovePartsFromPropertyFix.kt | 2 +- .../quickfix/RemoveRedundantInitializerFix.kt | 37 +++++++++++++++++++ .../removeRedundantInitializer/simple.kt | 7 ++++ .../simple.kt.after | 7 ++++ .../idea/quickfix/QuickFixTestGenerated.java | 15 ++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantInitializerFix.kt create mode 100644 idea/testData/quickfix/removeRedundantInitializer/simple.kt create mode 100644 idea/testData/quickfix/removeRedundantInitializer/simple.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index e2e80520c39..ec5106bef1d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -470,6 +470,8 @@ class QuickFixRegistrar : QuickFixContributor { INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.LATEINIT_FACTORY) INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemovePartsFromPropertyFix.LateInitFactory) + VARIABLE_WITH_REDUNDANT_INITIALIZER.registerFactory(RemoveRedundantInitializerFix) + OVERLOADS_ABSTRACT.registerFactory(RemoveAnnotationFix.JvmOverloads) OVERLOADS_INTERFACE.registerFactory(RemoveAnnotationFix.JvmOverloads) OVERLOADS_PRIVATE.registerFactory(RemoveAnnotationFix.JvmOverloads) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt index d831c9aa5e9..68832ca6d7b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt @@ -29,7 +29,7 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.isError -class RemovePartsFromPropertyFix( +open class RemovePartsFromPropertyFix( element: KtProperty, private val removeInitializer: Boolean, private val removeGetter: Boolean, diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantInitializerFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantInitializerFix.kt new file mode 100644 index 00000000000..514945a1528 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantInitializerFix.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2017 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.kotlin.idea.quickfix + +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.KtProperty + +class RemoveRedundantInitializerFix(element: KtProperty) : RemovePartsFromPropertyFix(element, true, false, false) { + + override fun getText(): String = "Remove redundant initializer" + + override fun getFamilyName(): String = "Remove redundant initializer" + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val element = diagnostic.psiElement + val property = PsiTreeUtil.getParentOfType(element, KtProperty::class.java) ?: return null + return RemoveRedundantInitializerFix(property) + } + } + +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantInitializer/simple.kt b/idea/testData/quickfix/removeRedundantInitializer/simple.kt new file mode 100644 index 00000000000..3999c53c15b --- /dev/null +++ b/idea/testData/quickfix/removeRedundantInitializer/simple.kt @@ -0,0 +1,7 @@ +// "Remove redundant initializer" "true" +// WITH_RUNTIME +fun foo() { + var bar = 1 + bar = 42 + println(bar) +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantInitializer/simple.kt.after b/idea/testData/quickfix/removeRedundantInitializer/simple.kt.after new file mode 100644 index 00000000000..0b4f7b667aa --- /dev/null +++ b/idea/testData/quickfix/removeRedundantInitializer/simple.kt.after @@ -0,0 +1,7 @@ +// "Remove redundant initializer" "true" +// WITH_RUNTIME +fun foo() { + var bar: Int + bar = 42 + println(bar) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 0647a06f7e9..e910029406a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -8655,6 +8655,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/removeRedundantInitializer") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveRedundantInitializer extends AbstractQuickFixTest { + public void testAllFilesPresentInRemoveRedundantInitializer() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeRedundantInitializer"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeRedundantInitializer/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)