Quickfix for "variable initializer is redundant" (VARIABLE_WITH_REDUNDANT_INITIALIZER) #KT-5878 Fixed (#1281)

This commit is contained in:
Toshiaki Kameyama
2017-09-04 23:58:32 +09:00
committed by Dmitry Jemerov
parent 653314e671
commit a9a52379ee
6 changed files with 69 additions and 1 deletions
@@ -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)
@@ -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,
@@ -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<KtProperty>? {
val element = diagnostic.psiElement
val property = PsiTreeUtil.getParentOfType(element, KtProperty::class.java) ?: return null
return RemoveRedundantInitializerFix(property)
}
}
}
@@ -0,0 +1,7 @@
// "Remove redundant initializer" "true"
// WITH_RUNTIME
fun foo() {
var bar = 1<caret>
bar = 42
println(bar)
}
@@ -0,0 +1,7 @@
// "Remove redundant initializer" "true"
// WITH_RUNTIME
fun foo() {
var bar: Int
bar = 42
println(bar)
}
@@ -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)