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:
@@ -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<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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
+18
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user