INAPPLICABLE_LATEINIT_MODIFIER should have a quickfix to remove initializer #KT-18826 Fixed
This commit is contained in:
committed by
Dmitry Jemerov
parent
a68312379e
commit
be64a6feeb
@@ -469,6 +469,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
|
||||
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(ChangeVariableMutabilityFix.LATEINIT_VAL_FACTORY)
|
||||
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.LATEINIT_FACTORY)
|
||||
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemovePartsFromPropertyFix.createLateInitFactory())
|
||||
|
||||
OVERLOADS_ABSTRACT.registerFactory(RemoveAnnotationFix.JvmOverloads)
|
||||
OVERLOADS_INTERFACE.registerFactory(RemoveAnnotationFix.JvmOverloads)
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
||||
import org.jetbrains.kotlin.diagnostics.Errors;
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil;
|
||||
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention;
|
||||
@@ -135,4 +136,23 @@ public class RemovePartsFromPropertyFix extends KotlinQuickFixAction<KtProperty>
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static KotlinSingleIntentionActionFactory createLateInitFactory() {
|
||||
return new KotlinSingleIntentionActionFactory() {
|
||||
@Override
|
||||
public KotlinQuickFixAction<KtProperty> createAction(@NotNull Diagnostic diagnostic) {
|
||||
PsiElement element = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).getPsiElement();
|
||||
KtProperty property = PsiTreeUtil.getParentOfType(element, KtProperty.class);
|
||||
if (property == null) return null;
|
||||
|
||||
boolean hasInitializer = property.hasInitializer();
|
||||
boolean hasGetter = property.getGetter() != null && property.getGetter().getBodyExpression() != null;
|
||||
boolean hasSetter = property.getSetter() != null && property.getSetter().getBodyExpression() != null;
|
||||
if (!hasInitializer && !hasGetter && !hasSetter) return null;
|
||||
|
||||
return new RemovePartsFromPropertyFix(property, hasInitializer, hasGetter, hasSetter);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove getter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
get() = ""
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove getter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Remove getter and setter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
get() = ""
|
||||
set(value) {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove getter and setter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove initializer from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str = ""
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove initializer from property" "true"
|
||||
|
||||
class A {
|
||||
lateinit var str: String<caret>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove setter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
set(value) {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove setter from property" "true"
|
||||
|
||||
class A {
|
||||
<caret>lateinit var str: String
|
||||
|
||||
}
|
||||
@@ -6580,6 +6580,30 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/val.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withGetter.kt")
|
||||
public void testWithGetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/withGetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withGetterSetter.kt")
|
||||
public void testWithGetterSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/withGetterSetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withInitializer.kt")
|
||||
public void testWithInitializer() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/withInitializer.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("withSetter.kt")
|
||||
public void testWithSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/withSetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/leakingThis")
|
||||
|
||||
Reference in New Issue
Block a user