From b5a6cbd188df836c86f992bf6818a4c284fdb91e Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 14 Nov 2016 14:26:29 +0300 Subject: [PATCH] Adjust "Remove explicit type" intention to properties with getters #KT-14794 Fixed --- .../idea/intentions/RemoveExplicitTypeIntention.kt | 11 +++++++---- .../removeExplicitType/propertyTypeFromGetter.kt | 1 + .../propertyTypeFromGetter.kt.after | 1 + .../migration/conflictingExtension/varInsteadOfVal.kt | 3 ++- .../conflictingExtension/wrongExplicitThis.kt | 1 + .../conflictingExtension/wrongExplicitThis2.kt | 1 + .../migration/conflictingExtension/wrongGetter.kt | 3 ++- .../migration/conflictingExtension/wrongGetter2.kt | 1 + .../migration/conflictingExtension/wrongSetter.kt | 1 + .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 10 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt create mode 100644 idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index 63d33daa8d2..31f26933a0c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -22,7 +22,6 @@ import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset -import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn import org.jetbrains.kotlin.psi.psiUtil.startOffset class RemoveSetterParameterTypeInspection() @@ -48,10 +47,14 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention TextRange(element.startOffset, initializer.startOffset - 1) + element is KtProperty && element.getter != null -> TextRange(element.startOffset, element.typeReference!!.endOffset) + else -> null + } } override fun applyTo(element: KtCallableDeclaration, editor: Editor?) { @@ -64,4 +67,4 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention get() = 1 diff --git a/idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt.after b/idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt.after new file mode 100644 index 00000000000..5531476003c --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt.after @@ -0,0 +1 @@ +val x get() = 1 diff --git a/idea/testData/quickfix/migration/conflictingExtension/varInsteadOfVal.kt b/idea/testData/quickfix/migration/conflictingExtension/varInsteadOfVal.kt index c559083cca8..8b3870a6783 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/varInsteadOfVal.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/varInsteadOfVal.kt @@ -1,7 +1,8 @@ // "Delete redundant extension property" "false" // ACTION: Create test +// ACTION: Remove explicit type specification import java.io.File var File.name: String get() = getName() - set(value) {} \ No newline at end of file + set(value) {} diff --git a/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis.kt b/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis.kt index 1a1f6d3ce5d..bd61670a9d9 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis.kt @@ -1,6 +1,7 @@ // "Delete redundant extension property" "false" // ACTION: Convert property to function // ACTION: Move to companion object +// ACTION: Remove explicit type specification class C : Thread() { val Thread.priority: Int diff --git a/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis2.kt b/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis2.kt index 260867bd4c0..2f77a17b8d8 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis2.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/wrongExplicitThis2.kt @@ -1,5 +1,6 @@ // "Delete redundant extension property" "false" // ACTION: Move to companion object +// ACTION: Remove explicit type specification class C : Thread() { var Thread.priority: Int diff --git a/idea/testData/quickfix/migration/conflictingExtension/wrongGetter.kt b/idea/testData/quickfix/migration/conflictingExtension/wrongGetter.kt index 4fdb0e10b57..81e1575e554 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/wrongGetter.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/wrongGetter.kt @@ -1,8 +1,9 @@ // "Delete redundant extension property" "false" // ACTION: Create test +// ACTION: Remove explicit type specification var Thread.priority: Int get() = getPriority() + 1 set(value) { setPriority(value) - } \ No newline at end of file + } diff --git a/idea/testData/quickfix/migration/conflictingExtension/wrongGetter2.kt b/idea/testData/quickfix/migration/conflictingExtension/wrongGetter2.kt index efb1405c2af..6eaf5f56bda 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/wrongGetter2.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/wrongGetter2.kt @@ -1,6 +1,7 @@ // "Delete redundant extension property" "false" // ACTION: Convert property to function // ACTION: Create test +// ACTION: Remove explicit type specification import java.io.File public val File.parent: File? diff --git a/idea/testData/quickfix/migration/conflictingExtension/wrongSetter.kt b/idea/testData/quickfix/migration/conflictingExtension/wrongSetter.kt index da5fc66bc37..14ce3fe1d2a 100644 --- a/idea/testData/quickfix/migration/conflictingExtension/wrongSetter.kt +++ b/idea/testData/quickfix/migration/conflictingExtension/wrongSetter.kt @@ -1,5 +1,6 @@ // "Delete redundant extension property" "false" // ACTION: Create test +// ACTION: Remove explicit type specification var Thread.priority: Int get() = this.getPriority() diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 8438426dbe2..c8e54b05dc7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -11030,6 +11030,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("propertyTypeFromGetter.kt") + public void testPropertyTypeFromGetter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt"); + doTest(fileName); + } + @TestMetadata("removeUnresolvedType.kt") public void testRemoveUnresolvedType() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/removeUnresolvedType.kt");