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
This commit is contained in:
Denis Zharkov
2016-09-22 14:08:25 +03:00
parent ab2b6743df
commit 23c0bdbd05
8 changed files with 63 additions and 2 deletions
@@ -106,7 +106,7 @@ internal fun createExpressionSelector(
private fun PsiElement.isStatement() = parent is KtBlockExpression private fun PsiElement.isStatement() = parent is KtBlockExpression
private class KtExpressionPostfixTemplateSelector( private class KtExpressionPostfixTemplateSelector(
val filter: PsiElement.() -> Boolean filter: PsiElement.() -> Boolean
) : PostfixTemplateExpressionSelectorBase(Condition(filter)) { ) : PostfixTemplateExpressionSelectorBase(Condition(filter)) {
override fun getNonFilteredExpressions( override fun getNonFilteredExpressions(
context: PsiElement, context: PsiElement,
@@ -114,7 +114,11 @@ private class KtExpressionPostfixTemplateSelector(
offset: Int offset: Int
) = context.parentsWithSelf ) = context.parentsWithSelf
.filterIsInstance<KtExpression>() .filterIsInstance<KtExpression>()
.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() .toList()
} }
@@ -124,3 +128,6 @@ private fun KtElement.isEffectivelyDeclaration() =
this !is KtFunctionLiteral && this !is KtFunctionLiteral &&
// !(fun (a) = 1) // !(fun (a) = 1)
(this !is KtNamedFunction || this.name == null) (this !is KtNamedFunction || this.name == null)
private val KtExpression.isSelector: Boolean
get() = parent is KtQualifiedExpression && (parent as KtQualifiedExpression).selectorExpression == this
+5
View File
@@ -0,0 +1,5 @@
class A
fun foo(a: Any) {
// Do not suggest 'A' as an expression to parenthesize
a as A.par<caret>
}
@@ -0,0 +1,5 @@
class A
fun foo(a: Any) {
// Do not suggest 'A' as an expression to parenthesize
(a as A)
}
+6
View File
@@ -0,0 +1,6 @@
class A {
fun bar() = 1
}
fun foo(a: A) {
val x = a.val<caret>.bar()
}
@@ -0,0 +1,7 @@
class A {
fun bar() = 1
}
fun foo(a: A) {
val a1 = a
val x = a1.bar()
}
+6
View File
@@ -0,0 +1,6 @@
class A {
fun bar() = 1
}
fun foo(a: A) {
val x = a.bar().val<caret>
}
@@ -0,0 +1,7 @@
class A {
fun bar() = 1
}
fun foo(a: A) {
val bar = a.bar()
val x = bar
}
@@ -95,6 +95,12 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
doTest(fileName); 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") @TestMetadata("return.kt")
public void testReturn() throws Exception { public void testReturn() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/return.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/return.kt");
@@ -119,6 +125,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
doTest(fileName); 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") @TestMetadata("var.kt")
public void testVar() throws Exception { public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt");