From ce128c5d6e8d385cc0f1240df54e2c9df2da2124 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 28 Sep 2016 18:06:34 +0300 Subject: [PATCH] Do not suggest operation references in postfix templates #KT-14087 Fixed --- .../idea/codeInsight/postfix/KtPostfixTemplateProvider.kt | 6 +++++- .../codeInsight/postfix/doNotProposeWrappingIncrement.kt | 3 +++ .../postfix/doNotProposeWrappingIncrement.kt.after | 3 +++ .../postfix/PostfixTemplateProviderTestGenerated.java | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt create mode 100644 idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt index 39b4697eeac..ba4312f8431 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt @@ -24,6 +24,7 @@ import com.intellij.openapi.util.Condition import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import org.jetbrains.annotations.TestOnly +import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.negate import org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable.KotlinIntroduceVariableHandler @@ -139,10 +140,13 @@ private class KtExpressionPostfixTemplateSelector( it !is KtBlockExpression && it !is KtDeclarationWithBody && !it.isEffectivelyDeclaration() - }.filter { !it.isSelector && it.parent !is KtUserType } + }.filter { !it.isSelector && it.parent !is KtUserType && !it.isOperationReference } .toList() } +private val KtExpression.isOperationReference: Boolean + get() = this.node.elementType == KtNodeTypes.OPERATION_REFERENCE + private fun KtElement.isEffectivelyDeclaration() = this is KtNamedDeclaration && // function literal is an expression while it's also a subtype of KtNamedDeclaration diff --git a/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt b/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt new file mode 100644 index 00000000000..868b3eef59d --- /dev/null +++ b/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt @@ -0,0 +1,3 @@ +fun foo(x: Int) { + x++.par +} diff --git a/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt.after b/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt.after new file mode 100644 index 00000000000..c7ddc00c92f --- /dev/null +++ b/idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt.after @@ -0,0 +1,3 @@ +fun foo(x: Int) { + (x++) +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java index 296f5ab8bb6..22aa077bc49 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java @@ -41,6 +41,12 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat doTest(fileName); } + @TestMetadata("doNotProposeWrappingIncrement.kt") + public void testDoNotProposeWrappingIncrement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/doNotProposeWrappingIncrement.kt"); + doTest(fileName); + } + @TestMetadata("else.kt") public void testElse() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/else.kt");