From 23c0bdbd05a0fc5777850d72ad92b9f02447bd38 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 22 Sep 2016 14:08:25 +0300 Subject: [PATCH] Add filtering of suggested expressions for postfix templates Do not suggest qualified expression's selector or a part of user type because they aren't really independent expressions --- .../postfix/KtPostfixTemplateProvider.kt | 11 +++++++++-- .../codeInsight/postfix/parAfterUserType.kt | 5 +++++ .../postfix/parAfterUserType.kt.after | 5 +++++ .../codeInsight/postfix/valAfterReceiver.kt | 6 ++++++ .../postfix/valAfterReceiver.kt.after | 7 +++++++ .../codeInsight/postfix/valAfterSelector.kt | 6 ++++++ .../postfix/valAfterSelector.kt.after | 7 +++++++ .../PostfixTemplateProviderTestGenerated.java | 18 ++++++++++++++++++ 8 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 idea/testData/codeInsight/postfix/parAfterUserType.kt create mode 100644 idea/testData/codeInsight/postfix/parAfterUserType.kt.after create mode 100644 idea/testData/codeInsight/postfix/valAfterReceiver.kt create mode 100644 idea/testData/codeInsight/postfix/valAfterReceiver.kt.after create mode 100644 idea/testData/codeInsight/postfix/valAfterSelector.kt create mode 100644 idea/testData/codeInsight/postfix/valAfterSelector.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 3f6b81a9f01..bb92a3f6c05 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt @@ -106,7 +106,7 @@ internal fun createExpressionSelector( private fun PsiElement.isStatement() = parent is KtBlockExpression private class KtExpressionPostfixTemplateSelector( - val filter: PsiElement.() -> Boolean + filter: PsiElement.() -> Boolean ) : PostfixTemplateExpressionSelectorBase(Condition(filter)) { override fun getNonFilteredExpressions( context: PsiElement, @@ -114,7 +114,11 @@ private class KtExpressionPostfixTemplateSelector( offset: Int ) = context.parentsWithSelf .filterIsInstance() - .takeWhile { it !is KtBlockExpression && it !is KtDeclarationWithBody && !it.isEffectivelyDeclaration() } + .takeWhile { + it !is KtBlockExpression && + it !is KtDeclarationWithBody && + !it.isEffectivelyDeclaration() + }.filter { !it.isSelector && it.parent !is KtUserType } .toList() } @@ -124,3 +128,6 @@ private fun KtElement.isEffectivelyDeclaration() = this !is KtFunctionLiteral && // !(fun (a) = 1) (this !is KtNamedFunction || this.name == null) + +private val KtExpression.isSelector: Boolean + get() = parent is KtQualifiedExpression && (parent as KtQualifiedExpression).selectorExpression == this diff --git a/idea/testData/codeInsight/postfix/parAfterUserType.kt b/idea/testData/codeInsight/postfix/parAfterUserType.kt new file mode 100644 index 00000000000..b8c83f8bfa1 --- /dev/null +++ b/idea/testData/codeInsight/postfix/parAfterUserType.kt @@ -0,0 +1,5 @@ +class A +fun foo(a: Any) { + // Do not suggest 'A' as an expression to parenthesize + a as A.par +} diff --git a/idea/testData/codeInsight/postfix/parAfterUserType.kt.after b/idea/testData/codeInsight/postfix/parAfterUserType.kt.after new file mode 100644 index 00000000000..c225e7d1621 --- /dev/null +++ b/idea/testData/codeInsight/postfix/parAfterUserType.kt.after @@ -0,0 +1,5 @@ +class A +fun foo(a: Any) { + // Do not suggest 'A' as an expression to parenthesize + (a as A) +} diff --git a/idea/testData/codeInsight/postfix/valAfterReceiver.kt b/idea/testData/codeInsight/postfix/valAfterReceiver.kt new file mode 100644 index 00000000000..8c129997261 --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAfterReceiver.kt @@ -0,0 +1,6 @@ +class A { + fun bar() = 1 +} +fun foo(a: A) { + val x = a.val.bar() +} diff --git a/idea/testData/codeInsight/postfix/valAfterReceiver.kt.after b/idea/testData/codeInsight/postfix/valAfterReceiver.kt.after new file mode 100644 index 00000000000..24629c1345d --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAfterReceiver.kt.after @@ -0,0 +1,7 @@ +class A { + fun bar() = 1 +} +fun foo(a: A) { + val a1 = a + val x = a1.bar() +} diff --git a/idea/testData/codeInsight/postfix/valAfterSelector.kt b/idea/testData/codeInsight/postfix/valAfterSelector.kt new file mode 100644 index 00000000000..8142cb0a4b4 --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAfterSelector.kt @@ -0,0 +1,6 @@ +class A { + fun bar() = 1 +} +fun foo(a: A) { + val x = a.bar().val +} diff --git a/idea/testData/codeInsight/postfix/valAfterSelector.kt.after b/idea/testData/codeInsight/postfix/valAfterSelector.kt.after new file mode 100644 index 00000000000..51029cc741b --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAfterSelector.kt.after @@ -0,0 +1,7 @@ +class A { + fun bar() = 1 +} +fun foo(a: A) { + val bar = a.bar() + val x = bar +} 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 d689f6f4545..296f5ab8bb6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java @@ -95,6 +95,12 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat doTest(fileName); } + @TestMetadata("parAfterUserType.kt") + public void testParAfterUserType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/parAfterUserType.kt"); + doTest(fileName); + } + @TestMetadata("return.kt") public void testReturn() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/return.kt"); @@ -119,6 +125,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat doTest(fileName); } + @TestMetadata("valAfterReceiver.kt") + public void testValAfterReceiver() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/valAfterReceiver.kt"); + doTest(fileName); + } + + @TestMetadata("valAfterSelector.kt") + public void testValAfterSelector() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/valAfterSelector.kt"); + doTest(fileName); + } + @TestMetadata("var.kt") public void testVar() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt");