Lift assignment out: if last statement is lambda, enclose it in parentheses if necessary
#KT-38155 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
e6476c39ca
commit
ee406f1622
@@ -450,7 +450,7 @@ public class KtPsiUtil {
|
|||||||
|
|
||||||
if (innerExpression instanceof KtLambdaExpression) {
|
if (innerExpression instanceof KtLambdaExpression) {
|
||||||
PsiElement prevSibling = PsiTreeUtil.skipWhitespacesAndCommentsBackward(currentInner);
|
PsiElement prevSibling = PsiTreeUtil.skipWhitespacesAndCommentsBackward(currentInner);
|
||||||
if (prevSibling != null && prevSibling.getText().endsWith(KtTokens.RPAR.getValue())) return true;
|
if (endWithParenthesisOrCallExpression(prevSibling)) return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentElement instanceof KtCallExpression && currentInner == ((KtCallExpression) parentElement).getCalleeExpression()) {
|
if (parentElement instanceof KtCallExpression && currentInner == ((KtCallExpression) parentElement).getCalleeExpression()) {
|
||||||
@@ -557,6 +557,15 @@ public class KtPsiUtil {
|
|||||||
return innerPriority < parentPriority;
|
return innerPriority < parentPriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean endWithParenthesisOrCallExpression(PsiElement element) {
|
||||||
|
if (element == null) return false;
|
||||||
|
if (element.getText().endsWith(KtTokens.RPAR.getValue()) || element instanceof KtCallExpression) return true;
|
||||||
|
PsiElement[] children = element.getChildren();
|
||||||
|
int length = children.length;
|
||||||
|
if (length == 0) return false;
|
||||||
|
return endWithParenthesisOrCallExpression(children[length - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isKeepBinaryExpressionParenthesized(KtBinaryExpression expression) {
|
private static boolean isKeepBinaryExpressionParenthesized(KtBinaryExpression expression) {
|
||||||
PsiElement expr = expression.getFirstChild();
|
PsiElement expr = expression.getFirstChild();
|
||||||
while (expr != null) {
|
while (expr != null) {
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
<caret>if (i == 1) {
|
||||||
|
f = { true }
|
||||||
|
} else {
|
||||||
|
foo { i }
|
||||||
|
f = { false }
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(f: () -> Int) {}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
f = if (i == 1) {
|
||||||
|
{ true }
|
||||||
|
} else {
|
||||||
|
foo { i }
|
||||||
|
({ false })
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo(f: () -> Int) {}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
<caret>if (i == 1) {
|
||||||
|
f = { true }
|
||||||
|
} else {
|
||||||
|
val foo = Foo().foo { i } // comment
|
||||||
|
f = { false }
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
fun foo(f: () -> Int) {}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
f = if (i == 1) {
|
||||||
|
{ true }
|
||||||
|
} else {
|
||||||
|
val foo = Foo().foo { i } // comment
|
||||||
|
({ false })
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
fun foo(f: () -> Int) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
<caret>if (i == 1) {
|
||||||
|
f = { true }
|
||||||
|
} else {
|
||||||
|
val g: () -> Boolean = { false }
|
||||||
|
f = { g() }
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
f = if (i == 1) {
|
||||||
|
{ true }
|
||||||
|
} else {
|
||||||
|
val g: () -> Boolean = { false }
|
||||||
|
{ g() }
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
<caret>if (i == 1) {
|
||||||
|
f = { true }
|
||||||
|
} else {
|
||||||
|
val foo = foo()
|
||||||
|
f = { false }
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
fun test(i: Int) {
|
||||||
|
val f: () -> Boolean
|
||||||
|
f = if (i == 1) {
|
||||||
|
{ true }
|
||||||
|
} else {
|
||||||
|
val foo = foo()
|
||||||
|
({ false })
|
||||||
|
}
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
+20
@@ -5603,6 +5603,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("afterRightBrace.kt")
|
||||||
|
public void testAfterRightBrace() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("afterRightBrace2.kt")
|
||||||
|
public void testAfterRightBrace2() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("afterRightBrace3.kt")
|
||||||
|
public void testAfterRightBrace3() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("afterRightParenthesis.kt")
|
||||||
|
public void testAfterRightParenthesis() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt");
|
||||||
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInIfToAssignment() throws Exception {
|
public void testAllFilesPresentInIfToAssignment() throws Exception {
|
||||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/liftOut/ifToAssignment"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/liftOut/ifToAssignment"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user