diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index f2c7dcf2e40..6fa83b9d7c1 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -450,7 +450,7 @@ public class KtPsiUtil { if (innerExpression instanceof KtLambdaExpression) { 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()) { @@ -557,6 +557,15 @@ public class KtPsiUtil { 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) { PsiElement expr = expression.getFirstChild(); while (expr != null) { diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt new file mode 100644 index 00000000000..2ddc78e1753 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt @@ -0,0 +1,12 @@ +fun test(i: Int) { + val f: () -> Boolean + if (i == 1) { + f = { true } + } else { + foo { i } + f = { false } + } + f() +} + +fun foo(f: () -> Int) {} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt.after b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt.after new file mode 100644 index 00000000000..aa89b3442a7 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace.kt.after @@ -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) {} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt new file mode 100644 index 00000000000..1d84ac9536b --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt @@ -0,0 +1,14 @@ +fun test(i: Int) { + val f: () -> Boolean + if (i == 1) { + f = { true } + } else { + val foo = Foo().foo { i } // comment + f = { false } + } + f() +} + +class Foo { + fun foo(f: () -> Int) {} +} diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt.after b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt.after new file mode 100644 index 00000000000..53293568031 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace2.kt.after @@ -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) {} +} diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt new file mode 100644 index 00000000000..17660264a77 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt @@ -0,0 +1,10 @@ +fun test(i: Int) { + val f: () -> Boolean + if (i == 1) { + f = { true } + } else { + val g: () -> Boolean = { false } + f = { g() } + } + f() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt.after b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt.after new file mode 100644 index 00000000000..b88bc87f1ed --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightBrace3.kt.after @@ -0,0 +1,10 @@ +fun test(i: Int) { + val f: () -> Boolean + f = if (i == 1) { + { true } + } else { + val g: () -> Boolean = { false } + { g() } + } + f() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt new file mode 100644 index 00000000000..9091e0a6beb --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt @@ -0,0 +1,12 @@ +fun test(i: Int) { + val f: () -> Boolean + if (i == 1) { + f = { true } + } else { + val foo = foo() + f = { false } + } + f() +} + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt.after b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt.after new file mode 100644 index 00000000000..2242f623aca --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/afterRightParenthesis.kt.after @@ -0,0 +1,12 @@ +fun test(i: Int) { + val f: () -> Boolean + f = if (i == 1) { + { true } + } else { + val foo = foo() + ({ false }) + } + f() +} + +fun foo() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 5815f98b6be..d3be06f5ced 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5603,6 +5603,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { 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 { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/liftOut/ifToAssignment"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); }