Add quick-fixes for lateinit-related errors #KT-14342 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
86cb7eea4a
commit
09d6e2e0c0
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
|
||||
class ChangeVariableMutabilityFix(element: KtValVarKeywordOwner, private val makeVar: Boolean) : KotlinQuickFixAction<KtValVarKeywordOwner>(element) {
|
||||
@@ -81,5 +82,14 @@ class ChangeVariableMutabilityFix(element: KtValVarKeywordOwner, private val mak
|
||||
return ChangeVariableMutabilityFix(element, false)
|
||||
}
|
||||
}
|
||||
|
||||
val LATEINIT_VAL_FACTORY = object: KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val lateinitElement = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).psiElement
|
||||
val property = lateinitElement.getStrictParentOfType<KtProperty>() ?: return null
|
||||
if (property.valOrVarKeyword.text != "val") return null
|
||||
return ChangeVariableMutabilityFix(property, makeVar = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
* Copyright 2010-2016 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.
|
||||
@@ -434,5 +434,8 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
UNUSED_VALUE.registerFactory(RemoveUnusedValueFix)
|
||||
|
||||
ANNOTATIONS_ON_BLOCK_LEVEL_EXPRESSION_ON_THE_SAME_LINE.registerFactory(AddNewLineAfterAnnotationsFix)
|
||||
|
||||
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(ChangeVariableMutabilityFix.LATEINIT_VAL_FACTORY)
|
||||
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.LATEINIT_FACTORY)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,16 +19,20 @@ package org.jetbrains.kotlin.idea.quickfix
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtNullableType
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
|
||||
class RemoveNullableFix(element: KtNullableType,
|
||||
private val typeOfError: RemoveNullableFix.NullableKind) : KotlinQuickFixAction<KtNullableType>(element) {
|
||||
enum class NullableKind(val message: String) {
|
||||
REDUNDANT("Remove redundant '?'"),
|
||||
SUPERTYPE("Remove '?'"),
|
||||
USELESS("Remove useless '?'")
|
||||
USELESS("Remove useless '?'"),
|
||||
PROPERTY("Make not-nullable")
|
||||
}
|
||||
|
||||
override fun getFamilyName() = "Remove '?'"
|
||||
@@ -47,4 +51,15 @@ class RemoveNullableFix(element: KtNullableType,
|
||||
return RemoveNullableFix(nullType, typeOfError)
|
||||
}
|
||||
}
|
||||
|
||||
object LATEINIT_FACTORY : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
|
||||
val lateinitElement = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).psiElement
|
||||
val property = lateinitElement.getStrictParentOfType<KtProperty>() ?: return null
|
||||
val typeReference = property.typeReference ?: return null
|
||||
val typeElement = (typeReference.typeElement ?: return null) as? KtNullableType ?: return null
|
||||
if (typeElement.innerType == null) return null
|
||||
return RemoveNullableFix(typeElement, NullableKind.PROPERTY)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Make not-nullable" "true"
|
||||
|
||||
class A() {
|
||||
<caret>lateinit var foo: String?
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Make not-nullable" "true"
|
||||
|
||||
class A() {
|
||||
<caret>lateinit var foo: String
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Make variable mutable" "true"
|
||||
|
||||
class A() {
|
||||
<caret>lateinit val foo: String
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Make variable mutable" "true"
|
||||
|
||||
class A() {
|
||||
<caret>lateinit var foo: String
|
||||
}
|
||||
@@ -5675,6 +5675,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/lateinit")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Lateinit extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInLateinit() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/lateinit"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("nullable.kt")
|
||||
public void testNullable() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/nullable.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("val.kt")
|
||||
public void testVal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/val.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/leakingThis")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user