Quick Fixes: Implement 'Initialize property' quick-fix
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
object InitializePropertyQuickFixFactory : KotlinIntentionActionsFactory() {
|
||||
class AddInitializerFix(property: KtProperty) : KotlinQuickFixAction<KtProperty>(property) {
|
||||
override fun getText() = "Add initializer"
|
||||
override fun getFamilyName() = text
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val descriptor = element.resolveToDescriptorIfAny() as? PropertyDescriptor ?: return
|
||||
val initializerText = CodeInsightUtils.defaultInitializer(descriptor.type) ?: "null"
|
||||
val initializer = element.setInitializer(KtPsiFactory(project).createExpression(initializerText))!!
|
||||
if (editor != null) {
|
||||
PsiDocumentManager.getInstance(project).commitDocument(editor.document)
|
||||
editor.selectionModel.setSelection(initializer.startOffset, initializer.endOffset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
|
||||
val property = diagnostic.psiElement as? KtProperty ?: return emptyList()
|
||||
|
||||
if (property.receiverTypeReference == null) {
|
||||
return listOf(AddInitializerFix(property))
|
||||
}
|
||||
|
||||
return emptyList()
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,9 @@ public class QuickFixRegistrar : QuickFixContributor {
|
||||
ABSTRACT_MEMBER_NOT_IMPLEMENTED.registerFactory(addAbstractModifierFactory)
|
||||
ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED.registerFactory(addAbstractModifierFactory)
|
||||
|
||||
MUST_BE_INITIALIZED_OR_BE_ABSTRACT.registerFactory(InitializePropertyQuickFixFactory)
|
||||
MUST_BE_INITIALIZED.registerFactory(InitializePropertyQuickFixFactory)
|
||||
|
||||
val removeFinalModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(FINAL_KEYWORD)
|
||||
val addAbstractToClassFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD, javaClass<KtClass>())
|
||||
ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS.registerFactory(removeAbstractModifierFactory, addAbstractToClassFactory)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
fun test() {
|
||||
<caret>val n: Int
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ACTION: Make protected
|
||||
// ACTION: Make 'n' abstract
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
class A {
|
||||
<caret>val Int.n: Int
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ACTION: Make protected
|
||||
// ERROR: Property must be initialized
|
||||
class A {
|
||||
<caret>var Int.n: Int
|
||||
get() = 1
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ACTION: Make protected
|
||||
// ERROR: Property must be initialized
|
||||
class A {
|
||||
<caret>var Int.n: Int
|
||||
set(value: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
<caret>val n: Int
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
<caret>val n: Int = <selection>0</selection>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
<caret>var n: Int
|
||||
get() = 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
var n: Int = <selection>0</selection>
|
||||
get() = 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
<caret>var n: Int
|
||||
set(value: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add initializer" "true"
|
||||
class A {
|
||||
var n: Int = <selection>0</selection>
|
||||
set(value: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ACTION: Make protected
|
||||
class A {
|
||||
<caret>val n: Int
|
||||
get() = 1
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ACTION: Make protected
|
||||
class A {
|
||||
<caret>val n: Int by lazy { 0 }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ERROR: Property must be initialized
|
||||
<caret>val Int.n: Int
|
||||
@@ -0,0 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
// ERROR: Property must be initialized
|
||||
<caret>val Int.n: Int
|
||||
@@ -0,0 +1,2 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>val n: Int
|
||||
@@ -0,0 +1,2 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>val n: Int = <selection>0</selection>
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>var n: Int
|
||||
get() = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>var n: Int = <selection>0</selection>
|
||||
get() = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>var n: Int
|
||||
set(value: Int) {}
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Add initializer" "true"
|
||||
<caret>var n: Int = <selection>0</selection>
|
||||
set(value: Int) {}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// WITH_RUNTIME
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
<caret>val n: Int by lazy { 0 }
|
||||
@@ -0,0 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.InitializePropertyQuickFixFactory$AddInitializerFix" "false"
|
||||
// ACTION: Make internal
|
||||
// ACTION: Make private
|
||||
<caret>val n: Int
|
||||
get() = 1
|
||||
@@ -233,6 +233,111 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/addInitializer")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class AddInitializer extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInAddInitializer() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addInitializer"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("localVar.kt")
|
||||
public void testLocalVar() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/localVar.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberExtensionProperty.kt")
|
||||
public void testMemberExtensionProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberExtensionProperty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberExtensionPropertyVarGetterOnly.kt")
|
||||
public void testMemberExtensionPropertyVarGetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberExtensionPropertyVarGetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberExtensionPropertyVarSetterOnly.kt")
|
||||
public void testMemberExtensionPropertyVarSetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberExtensionPropertyVarSetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberProperty.kt")
|
||||
public void testMemberProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberProperty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberPropertyVarGetterOnly.kt")
|
||||
public void testMemberPropertyVarGetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberPropertyVarGetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberPropertyVarSetterOnly.kt")
|
||||
public void testMemberPropertyVarSetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberPropertyVarSetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberPropertyWithAccessor.kt")
|
||||
public void testMemberPropertyWithAccessor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberPropertyWithAccessor.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("memberPropertyWithDelegateRuntime.kt")
|
||||
public void testMemberPropertyWithDelegateRuntime() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/memberPropertyWithDelegateRuntime.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelExtensionProperty.kt")
|
||||
public void testTopLevelExtensionProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelExtensionProperty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelExtensionPropertySetterOnly.kt")
|
||||
public void testTopLevelExtensionPropertySetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelExtensionPropertySetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelProperty.kt")
|
||||
public void testTopLevelProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelProperty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelPropertyVarGetterOnly.kt")
|
||||
public void testTopLevelPropertyVarGetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelPropertyVarGetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelPropertyVarSetterOnly.kt")
|
||||
public void testTopLevelPropertyVarSetterOnly() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelPropertyVarSetterOnly.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelPropertyWithDelegateRuntime.kt")
|
||||
public void testTopLevelPropertyWithDelegateRuntime() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelPropertyWithDelegateRuntime.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelPropertyWithGetter.kt")
|
||||
public void testTopLevelPropertyWithGetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInitializer/topLevelPropertyWithGetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/addStarProjections")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user