Skip to content

Commit 09d6e2e

Browse files
cypressiousmglukhikh
authored andcommitted
Add quick-fixes for lateinit-related errors #KT-14342 Fixed
1 parent 86cb7ee commit 09d6e2e

File tree

8 files changed

+71
-2
lines changed

8 files changed

+71
-2
lines changed

idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeVariableMutabilityFix.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
2626
import org.jetbrains.kotlin.diagnostics.Errors
2727
import org.jetbrains.kotlin.lexer.KtTokens
2828
import org.jetbrains.kotlin.psi.*
29+
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
2930
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
3031

3132
class ChangeVariableMutabilityFix(element: KtValVarKeywordOwner, private val makeVar: Boolean) : KotlinQuickFixAction<KtValVarKeywordOwner>(element) {
@@ -81,5 +82,14 @@ class ChangeVariableMutabilityFix(element: KtValVarKeywordOwner, private val mak
8182
return ChangeVariableMutabilityFix(element, false)
8283
}
8384
}
85+
86+
val LATEINIT_VAL_FACTORY = object: KotlinSingleIntentionActionFactory() {
87+
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
88+
val lateinitElement = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).psiElement
89+
val property = lateinitElement.getStrictParentOfType<KtProperty>() ?: return null
90+
if (property.valOrVarKeyword.text != "val") return null
91+
return ChangeVariableMutabilityFix(property, makeVar = true)
92+
}
93+
}
8494
}
8595
}

idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 JetBrains s.r.o.
2+
* Copyright 2010-2016 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -434,5 +434,8 @@ class QuickFixRegistrar : QuickFixContributor {
434434
UNUSED_VALUE.registerFactory(RemoveUnusedValueFix)
435435

436436
ANNOTATIONS_ON_BLOCK_LEVEL_EXPRESSION_ON_THE_SAME_LINE.registerFactory(AddNewLineAfterAnnotationsFix)
437+
438+
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(ChangeVariableMutabilityFix.LATEINIT_VAL_FACTORY)
439+
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.LATEINIT_FACTORY)
437440
}
438441
}

idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ package org.jetbrains.kotlin.idea.quickfix
1919
import com.intellij.openapi.editor.Editor
2020
import com.intellij.openapi.project.Project
2121
import org.jetbrains.kotlin.diagnostics.Diagnostic
22+
import org.jetbrains.kotlin.diagnostics.Errors
2223
import org.jetbrains.kotlin.psi.KtFile
2324
import org.jetbrains.kotlin.psi.KtNullableType
25+
import org.jetbrains.kotlin.psi.KtProperty
2426
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
27+
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
2528

2629
class RemoveNullableFix(element: KtNullableType,
2730
private val typeOfError: RemoveNullableFix.NullableKind) : KotlinQuickFixAction<KtNullableType>(element) {
2831
enum class NullableKind(val message: String) {
2932
REDUNDANT("Remove redundant '?'"),
3033
SUPERTYPE("Remove '?'"),
31-
USELESS("Remove useless '?'")
34+
USELESS("Remove useless '?'"),
35+
PROPERTY("Make not-nullable")
3236
}
3337

3438
override fun getFamilyName() = "Remove '?'"
@@ -47,4 +51,15 @@ class RemoveNullableFix(element: KtNullableType,
4751
return RemoveNullableFix(nullType, typeOfError)
4852
}
4953
}
54+
55+
object LATEINIT_FACTORY : KotlinSingleIntentionActionFactory() {
56+
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
57+
val lateinitElement = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).psiElement
58+
val property = lateinitElement.getStrictParentOfType<KtProperty>() ?: return null
59+
val typeReference = property.typeReference ?: return null
60+
val typeElement = (typeReference.typeElement ?: return null) as? KtNullableType ?: return null
61+
if (typeElement.innerType == null) return null
62+
return RemoveNullableFix(typeElement, NullableKind.PROPERTY)
63+
}
64+
}
5065
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "Make not-nullable" "true"
2+
3+
class A() {
4+
<caret>lateinit var foo: String?
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "Make not-nullable" "true"
2+
3+
class A() {
4+
<caret>lateinit var foo: String
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "Make variable mutable" "true"
2+
3+
class A() {
4+
<caret>lateinit val foo: String
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// "Make variable mutable" "true"
2+
3+
class A() {
4+
<caret>lateinit var foo: String
5+
}

idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5675,6 +5675,27 @@ public void testSimple() throws Exception {
56755675
}
56765676
}
56775677

5678+
@TestMetadata("idea/testData/quickfix/lateinit")
5679+
@TestDataPath("$PROJECT_ROOT")
5680+
@RunWith(JUnit3RunnerWithInners.class)
5681+
public static class Lateinit extends AbstractQuickFixTest {
5682+
public void testAllFilesPresentInLateinit() throws Exception {
5683+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/lateinit"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
5684+
}
5685+
5686+
@TestMetadata("nullable.kt")
5687+
public void testNullable() throws Exception {
5688+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/nullable.kt");
5689+
doTest(fileName);
5690+
}
5691+
5692+
@TestMetadata("val.kt")
5693+
public void testVal() throws Exception {
5694+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/lateinit/val.kt");
5695+
doTest(fileName);
5696+
}
5697+
}
5698+
56785699
@TestMetadata("idea/testData/quickfix/leakingThis")
56795700
@TestDataPath("$PROJECT_ROOT")
56805701
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)