From 91e62832eec041168cd54571c27d03542d927ec1 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Wed, 9 Jun 2021 10:02:53 +0000 Subject: [PATCH] FIR IDE: Add applicability range to HLAddAccessorIntention (copied from FE 1.0's logic) and actually check the range in AbstractHLIntention. --- .../kotlin/idea/fir/api/AbstractHLIntention.kt | 5 +++++ .../fir/intentions/HLAddAccessorIntentions.kt | 7 ++++--- .../fir/intentions/HLIntentionTestGenerated.java | 15 +++++++++++++++ .../addPropertyAccessors/both/caretNotOnName.kt | 4 ++++ .../addPropertyAccessors/getter/caretNotOnName.kt | 4 ++++ .../addPropertyAccessors/setter/caretNotOnName.kt | 4 ++++ .../idea/intentions/IntentionTestGenerated.java | 15 +++++++++++++++ 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt create mode 100644 idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt create mode 100644 idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/AbstractHLIntention.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/AbstractHLIntention.kt index 901d22bffaa..1c75d41ef05 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/AbstractHLIntention.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/api/AbstractHLIntention.kt @@ -26,6 +26,11 @@ abstract class AbstractHLIntention( if (!applicator.isApplicableByPsi(element, project)) return false val ranges = applicabilityRange.getApplicabilityRanges(element) if (ranges.isEmpty()) return false + + // An HLApplicabilityRange should be relative to the element, while `caretOffset` is absolute + val relativeCaretOffset = caretOffset - element.textRange.startOffset + if (ranges.none { it.containsOffset(relativeCaretOffset) }) return false + val input = getInput(element) if (input != null && input.isValidFor(element)) { setFamilyNameGetter(applicator::getFamilyName) diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddAccessorIntentions.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddAccessorIntentions.kt index 3527e7950af..56c4ad03304 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddAccessorIntentions.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddAccessorIntentions.kt @@ -12,8 +12,8 @@ import org.jetbrains.kotlin.idea.api.applicator.applicator import org.jetbrains.kotlin.idea.fir.api.AbstractHLIntention import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicabilityRange import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicatorInputProvider +import org.jetbrains.kotlin.idea.fir.api.applicator.applicabilityTarget import org.jetbrains.kotlin.idea.fir.api.applicator.inputProvider -import org.jetbrains.kotlin.idea.fir.applicators.ApplicabilityRanges import org.jetbrains.kotlin.idea.frontend.api.symbols.KtPropertySymbol import org.jetbrains.kotlin.idea.intentions.AbstractAddAccessorsIntention import org.jetbrains.kotlin.lexer.KtTokens @@ -26,8 +26,9 @@ import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier abstract class HLAddAccessorIntention(private val addGetter: Boolean, private val addSetter: Boolean) : AbstractHLIntention(KtProperty::class) { - // TODO: Copy and test range from AddAccessorIntentions.applicabilityRange - override val applicabilityRange: HLApplicabilityRange = ApplicabilityRanges.SELF + override val applicabilityRange: HLApplicabilityRange = applicabilityTarget { ktProperty -> + if (ktProperty.hasInitializer()) ktProperty.nameIdentifier else ktProperty + } override val applicator: HLApplicator = applicator { applyTo { ktProperty, _, _, editor -> diff --git a/idea/idea-fir/tests/org/jetbrains/kotlin/idea/fir/intentions/HLIntentionTestGenerated.java b/idea/idea-fir/tests/org/jetbrains/kotlin/idea/fir/intentions/HLIntentionTestGenerated.java index d936abb89ff..5bb6ad55ee2 100644 --- a/idea/idea-fir/tests/org/jetbrains/kotlin/idea/fir/intentions/HLIntentionTestGenerated.java +++ b/idea/idea-fir/tests/org/jetbrains/kotlin/idea/fir/intentions/HLIntentionTestGenerated.java @@ -48,6 +48,11 @@ public class HLIntentionTestGenerated extends AbstractHLIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/both"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt"); + } + @TestMetadata("delegate.kt") public void testDelegate() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/both/delegate.kt"); @@ -126,6 +131,11 @@ public class HLIntentionTestGenerated extends AbstractHLIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/getter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt"); + } + @TestMetadata("const.kt") public void testConst() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/getter/const.kt"); @@ -209,6 +219,11 @@ public class HLIntentionTestGenerated extends AbstractHLIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/setter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt"); + } + @TestMetadata("delegate.kt") public void testDelegate() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/setter/delegate.kt"); diff --git a/idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt b/idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt new file mode 100644 index 00000000000..125e3e13bee --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +class Test { + var x = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt b/idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt new file mode 100644 index 00000000000..f92e5bf264b --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +class Test { + val x = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt b/idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt new file mode 100644 index 00000000000..125e3e13bee --- /dev/null +++ b/idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +class Test { + var x = 1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ebb92e60720..a592e0d94ac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1621,6 +1621,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/both"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/both/caretNotOnName.kt"); + } + @TestMetadata("delegate.kt") public void testDelegate() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/both/delegate.kt"); @@ -1699,6 +1704,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/getter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/getter/caretNotOnName.kt"); + } + @TestMetadata("const.kt") public void testConst() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/getter/const.kt"); @@ -1782,6 +1792,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/setter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); } + @TestMetadata("caretNotOnName.kt") + public void testCaretNotOnName() throws Exception { + runTest("idea/testData/intentions/addPropertyAccessors/setter/caretNotOnName.kt"); + } + @TestMetadata("delegate.kt") public void testDelegate() throws Exception { runTest("idea/testData/intentions/addPropertyAccessors/setter/delegate.kt");