From b4962a18802dc257ff9a04c781aa3a1f5ad6e2ad Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 29 May 2018 16:13:33 +0300 Subject: [PATCH] Add getter: don't suggest when property has no type or initializer So #KT-24632 Fixed --- .../idea/intentions/AddAccessorIntentions.kt | 9 +++++---- .../addPropertyAccessors/both/noType.kt | 4 ++++ .../addPropertyAccessors/getter/noType.kt | 4 ++++ .../addPropertyAccessors/setter/noType.kt | 3 +++ .../addPropertyAccessors/setter/noType.kt.after | 6 ++++++ .../idea/intentions/IntentionTestGenerated.java | 15 +++++++++++++++ 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 idea/testData/intentions/addPropertyAccessors/both/noType.kt create mode 100644 idea/testData/intentions/addPropertyAccessors/getter/noType.kt create mode 100644 idea/testData/intentions/addPropertyAccessors/setter/noType.kt create mode 100644 idea/testData/intentions/addPropertyAccessors/setter/noType.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddAccessorIntentions.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddAccessorIntentions.kt index 1199b8003f0..a530764a6c1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddAccessorIntentions.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddAccessorIntentions.kt @@ -27,21 +27,22 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPsiFactory abstract class AbstractAddAccessorsIntention( - private val addGetter: Boolean, - private val addSetter: Boolean + private val addGetter: Boolean, + private val addSetter: Boolean ) : SelfTargetingRangeIntention(KtProperty::class.java, createFamilyName(addGetter, addSetter)) { override fun applicabilityRange(element: KtProperty): TextRange? { if (element.isLocal || element.isAbstract() || element.hasDelegate() || element.hasModifier(KtTokens.LATEINIT_KEYWORD) || - element.hasModifier(KtTokens.CONST_KEYWORD)) { + element.hasModifier(KtTokens.CONST_KEYWORD) + ) { return null } val descriptor = element.resolveToDescriptorIfAny() as? CallableMemberDescriptor ?: return null if (descriptor.isExpect) return null if (addSetter && (!element.isVar || element.setter != null)) return null - if (addGetter && element.getter != null) return null + if (addGetter && ((element.typeReference == null && element.initializer == null) || element.getter != null)) return null return element.nameIdentifier?.textRange } diff --git a/idea/testData/intentions/addPropertyAccessors/both/noType.kt b/idea/testData/intentions/addPropertyAccessors/both/noType.kt new file mode 100644 index 00000000000..b37c403588a --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/both/noType.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER +var x \ No newline at end of file diff --git a/idea/testData/intentions/addPropertyAccessors/getter/noType.kt b/idea/testData/intentions/addPropertyAccessors/getter/noType.kt new file mode 100644 index 00000000000..b37c403588a --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/getter/noType.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER +var x \ No newline at end of file diff --git a/idea/testData/intentions/addPropertyAccessors/setter/noType.kt b/idea/testData/intentions/addPropertyAccessors/setter/noType.kt new file mode 100644 index 00000000000..6e1b77d20e1 --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/setter/noType.kt @@ -0,0 +1,3 @@ +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER +var x \ No newline at end of file diff --git a/idea/testData/intentions/addPropertyAccessors/setter/noType.kt.after b/idea/testData/intentions/addPropertyAccessors/setter/noType.kt.after new file mode 100644 index 00000000000..29fd5586793 --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/setter/noType.kt.after @@ -0,0 +1,6 @@ +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER +var x + set(value) { + field = value + } diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index e4675305e64..128cedeb74f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1475,6 +1475,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/addPropertyAccessors/both/local.kt"); } + @TestMetadata("noType.kt") + public void testNoType() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/both/noType.kt"); + } + @TestMetadata("top.kt") public void testTop() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/both/top.kt"); @@ -1543,6 +1548,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/addPropertyAccessors/getter/local.kt"); } + @TestMetadata("noType.kt") + public void testNoType() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/getter/noType.kt"); + } + @TestMetadata("top.kt") public void testTop() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/getter/top.kt"); @@ -1606,6 +1616,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/addPropertyAccessors/setter/local.kt"); } + @TestMetadata("noType.kt") + public void testNoType() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/setter/noType.kt"); + } + @TestMetadata("top.kt") public void testTop() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/setter/top.kt");