Merge branch 'KT-5115' of https://github.com/mcgee/kotlin into mcgee-KT-5115
This commit is contained in:
@@ -180,6 +180,15 @@ public class KtPsiFactory(private val project: Project) {
|
||||
return createDeclaration(text)
|
||||
}
|
||||
|
||||
public fun createPropertyGetter(expression: KtExpression): KtPropertyAccessor {
|
||||
val property = createProperty("val x get() = 1")
|
||||
val getter = property.getter!!
|
||||
val bodyExpression = getter.bodyExpression!!
|
||||
|
||||
bodyExpression.replace(expression)
|
||||
return getter
|
||||
}
|
||||
|
||||
public fun <TDeclaration : KtDeclaration> createDeclaration(text: String): TDeclaration {
|
||||
val file = createFile(text)
|
||||
val declarations = file.getDeclarations()
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
|
||||
class ConvertExtensionPropertyInitializerToGetterFix(element: KtExpression) : KotlinQuickFixAction<KtExpression>(element) {
|
||||
override fun getText(): String = "Convert extension property initializer to getter"
|
||||
|
||||
override fun getFamilyName(): String = getText()
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val property = element.getParentOfType<KtProperty>(true) ?: return
|
||||
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(property)
|
||||
SpecifyTypeExplicitlyIntention.addTypeAnnotation(editor, property, type)
|
||||
|
||||
val getter = KtPsiFactory(element).createPropertyGetter(element)
|
||||
val setter = property.setter
|
||||
|
||||
if (setter != null) {
|
||||
property.addBefore(getter, setter)
|
||||
}
|
||||
else {
|
||||
property.add(getter)
|
||||
}
|
||||
|
||||
property.setInitializer(null)
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val expression = diagnostic.psiElement as? KtExpression ?: return null
|
||||
val property = expression.getParentOfType<KtProperty>(true)!!
|
||||
|
||||
if (property.getter != null) {
|
||||
return null
|
||||
}
|
||||
|
||||
return ConvertExtensionPropertyInitializerToGetterFix(expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,6 +235,8 @@ public class QuickFixRegistrar : QuickFixContributor {
|
||||
|
||||
EXPECTED_PARAMETER_TYPE_MISMATCH.registerFactory(ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch())
|
||||
|
||||
EXTENSION_PROPERTY_WITH_BACKING_FIELD.registerFactory(ConvertExtensionPropertyInitializerToGetterFix)
|
||||
|
||||
val changeFunctionLiteralReturnTypeFix = ChangeFunctionLiteralReturnTypeFix.createFactoryForExpectedOrAssignmentTypeMismatch()
|
||||
EXPECTED_TYPE_MISMATCH.registerFactory(changeFunctionLiteralReturnTypeFix)
|
||||
ASSIGNMENT_TYPE_MISMATCH.registerFactory(changeFunctionLiteralReturnTypeFix)
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
val String.foo: Int = 0<caret>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
val String.foo: Int
|
||||
get() = 0
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
val String.foo = 0<caret>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
val String.foo: Int
|
||||
get() = 0
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Convert extension property initializer to getter" "false"
|
||||
// ERROR: Extension property cannot be initialized because it has no backing field
|
||||
var String.foo: Int = 0<caret>
|
||||
get() = 1
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
var String.foo: Int = 0<caret>
|
||||
set(value) {}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// "Convert extension property initializer to getter" "true"
|
||||
var String.foo: Int
|
||||
get() = 0
|
||||
set(value) {}
|
||||
@@ -1506,6 +1506,16 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/properties")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Properties extends AbstractQuickFixMultiFileTest {
|
||||
public void testAllFilesPresentInProperties() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/properties"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/suppress")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -5605,6 +5605,48 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/properties")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Properties extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInProperties() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/properties"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ExtensionPropertyInitializerToGetter extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInExtensionPropertyInitializerToGetter() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("baseCase.kt")
|
||||
public void testBaseCase() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter/baseCase.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("baseCaseWithoutTypeAnnotation.kt")
|
||||
public void testBaseCaseWithoutTypeAnnotation() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter/baseCaseWithoutTypeAnnotation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("dontShowQuickfixOnExistingAccessors.kt")
|
||||
public void testDontShowQuickfixOnExistingAccessors() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter/dontShowQuickfixOnExistingAccessors.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("insertGetterBeforeExistingSetter.kt")
|
||||
public void testInsertGetterBeforeExistingSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/properties/extensionPropertyInitializerToGetter/insertGetterBeforeExistingSetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeUnused")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user