From 19824201e2783f0c8269ac8d63d8dd0acfa925ae Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sat, 3 Aug 2019 17:29:09 +0200 Subject: [PATCH] Move statement: do not move into lambda beyond 'if/when/try/for/while' #KT-9065 Fixed --- .../upDownMover/KotlinExpressionMover.kt | 9 +++ .../moveUpDown/expressions/lambdaInDoWhile.kt | 8 +++ .../expressions/lambdaInDoWhile.kt.after | 8 +++ .../expressions/lambdaInDoWhile2.kt | 8 +++ .../expressions/lambdaInDoWhile2.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInFor.kt | 8 +++ .../expressions/lambdaInFor.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInFor2.kt | 8 +++ .../expressions/lambdaInFor2.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInIf.kt | 8 +++ .../expressions/lambdaInIf.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInIf2.kt | 8 +++ .../expressions/lambdaInIf2.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInTry.kt | 9 +++ .../expressions/lambdaInTry.kt.after | 9 +++ .../moveUpDown/expressions/lambdaInTry2.kt | 9 +++ .../expressions/lambdaInTry2.kt.after | 9 +++ .../moveUpDown/expressions/lambdaInTry3.kt | 9 +++ .../expressions/lambdaInTry3.kt.after | 9 +++ .../moveUpDown/expressions/lambdaInWhen.kt | 10 +++ .../expressions/lambdaInWhen.kt.after | 10 +++ .../moveUpDown/expressions/lambdaInWhen2.kt | 10 +++ .../expressions/lambdaInWhen2.kt.after | 10 +++ .../moveUpDown/expressions/lambdaInWhile.kt | 8 +++ .../expressions/lambdaInWhile.kt.after | 8 +++ .../moveUpDown/expressions/lambdaInWhile2.kt | 8 +++ .../expressions/lambdaInWhile2.kt.after | 8 +++ .../MoveStatementTestGenerated.java | 65 +++++++++++++++++++ 28 files changed, 296 insertions(+) create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt.after create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt create mode 100644 idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt index eb2b713ede9..2b74d9fc269 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.kt @@ -282,6 +282,15 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() { element: PsiElement, down: Boolean ): KtBlockExpression? { + if (element is KtIfExpression || + element is KtWhenExpression || + element is KtWhenEntry || + element is KtTryExpression || + element is KtFinallySection || + element is KtCatchClause || + element is KtLoopExpression + ) return null + val callExpression = KtPsiUtil.getOutermostDescendantElement(element, down, IS_CALL_EXPRESSION) as KtCallExpression? ?: return null val functionLiterals = callExpression.lambdaArguments diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt new file mode 100644 index 00000000000..d3d9a03848a --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + println() + do { + run { + } + } while (i == 1) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt.after new file mode 100644 index 00000000000..a893dd730d2 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt.after @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + do { + println() + run { + } + } while (i == 1) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt new file mode 100644 index 00000000000..af78bb462d1 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + do { + run { + } + } while (i == 1) + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt.after new file mode 100644 index 00000000000..2cf4c06f396 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt.after @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + do { + run { + } + println() + } while (i == 1) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt new file mode 100644 index 00000000000..3e6c892a7f1 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt @@ -0,0 +1,8 @@ +// MOVE: down +fun test() { + println() + for (i in 1..10) { + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt.after new file mode 100644 index 00000000000..3303ae31d19 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt.after @@ -0,0 +1,8 @@ +// MOVE: down +fun test() { + for (i in 1..10) { + println() + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt new file mode 100644 index 00000000000..f3e451ad4b9 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt @@ -0,0 +1,8 @@ +// MOVE: up +fun test() { + for (i in 1..10) { + run { + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt.after new file mode 100644 index 00000000000..be47f621763 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt.after @@ -0,0 +1,8 @@ +// MOVE: up +fun test() { + for (i in 1..10) { + run { + } + println() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt new file mode 100644 index 00000000000..3b6f5a41f1f --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + println() + if (i == 1) { + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt.after new file mode 100644 index 00000000000..cf7e8901e18 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt.after @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + if (i == 1) { + println() + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt new file mode 100644 index 00000000000..878738f3815 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + if (i == 1) { + run { + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt.after new file mode 100644 index 00000000000..0f02e1ef796 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt.after @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + if (i == 1) { + run { + } + println() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt new file mode 100644 index 00000000000..ca2a2f4199b --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt @@ -0,0 +1,9 @@ +// MOVE: down +fun test() { + println() + try { + run { + } + } finally { + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt.after new file mode 100644 index 00000000000..b84b8f3455c --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt.after @@ -0,0 +1,9 @@ +// MOVE: down +fun test() { + try { + println() + run { + } + } finally { + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt new file mode 100644 index 00000000000..ae484be3460 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt @@ -0,0 +1,9 @@ +// MOVE: up +fun test() { + try { + } finally { + run { + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt.after new file mode 100644 index 00000000000..43cde304c42 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt.after @@ -0,0 +1,9 @@ +// MOVE: up +fun test() { + try { + } finally { + run { + } + println() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt new file mode 100644 index 00000000000..af036df6c40 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt @@ -0,0 +1,9 @@ +// MOVE: up +fun test() { + try { + } catch (e: Exception) { + run { + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt.after new file mode 100644 index 00000000000..a1f54b24307 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt.after @@ -0,0 +1,9 @@ +// MOVE: up +fun test() { + try { + } catch (e: Exception) { + run { + } + println() + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt new file mode 100644 index 00000000000..dbe2b07dc5b --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt @@ -0,0 +1,10 @@ +// MOVE: down +fun test(i: Int) { + println() + when (i) { + 1 -> { + run { + } + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt.after new file mode 100644 index 00000000000..d709eb99497 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt.after @@ -0,0 +1,10 @@ +// MOVE: down +fun test(i: Int) { + when (i) { + 1 -> { + println() + run { + } + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt new file mode 100644 index 00000000000..3474dedd4f8 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt @@ -0,0 +1,10 @@ +// MOVE: up +fun test(i: Int) { + when (i) { + 1 -> { + run { + } + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt.after new file mode 100644 index 00000000000..8ee61afe711 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt.after @@ -0,0 +1,10 @@ +// MOVE: up +fun test(i: Int) { + when (i) { + 1 -> { + run { + } + println() + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt new file mode 100644 index 00000000000..81a8ae73acc --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + println() + while (i == 1) { + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt.after new file mode 100644 index 00000000000..478f94a770b --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt.after @@ -0,0 +1,8 @@ +// MOVE: down +fun test(i: Int) { + while (i == 1) { + println() + run { + } + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt new file mode 100644 index 00000000000..ce35cd8c69f --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + while (i == 1) { + run { + } + } + println() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt.after b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt.after new file mode 100644 index 00000000000..82f8f3cbf3c --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt.after @@ -0,0 +1,8 @@ +// MOVE: up +fun test(i: Int) { + while (i == 1) { + run { + } + println() + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java index 3539bf9acad..b7ca02aecef 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java @@ -1063,6 +1063,71 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest { runTest("idea/testData/codeInsight/moveUpDown/expressions/lambda3.kt"); } + @TestMetadata("lambdaInDoWhile.kt") + public void testLambdaInDoWhile() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile.kt"); + } + + @TestMetadata("lambdaInDoWhile2.kt") + public void testLambdaInDoWhile2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInDoWhile2.kt"); + } + + @TestMetadata("lambdaInFor.kt") + public void testLambdaInFor() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor.kt"); + } + + @TestMetadata("lambdaInFor2.kt") + public void testLambdaInFor2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInFor2.kt"); + } + + @TestMetadata("lambdaInIf.kt") + public void testLambdaInIf() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf.kt"); + } + + @TestMetadata("lambdaInIf2.kt") + public void testLambdaInIf2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInIf2.kt"); + } + + @TestMetadata("lambdaInTry.kt") + public void testLambdaInTry() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry.kt"); + } + + @TestMetadata("lambdaInTry2.kt") + public void testLambdaInTry2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry2.kt"); + } + + @TestMetadata("lambdaInTry3.kt") + public void testLambdaInTry3() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInTry3.kt"); + } + + @TestMetadata("lambdaInWhen.kt") + public void testLambdaInWhen() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen.kt"); + } + + @TestMetadata("lambdaInWhen2.kt") + public void testLambdaInWhen2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhen2.kt"); + } + + @TestMetadata("lambdaInWhile.kt") + public void testLambdaInWhile() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile.kt"); + } + + @TestMetadata("lambdaInWhile2.kt") + public void testLambdaInWhile2() throws Exception { + runTest("idea/testData/codeInsight/moveUpDown/expressions/lambdaInWhile2.kt"); + } + @TestMetadata("multilineComment1.kt") public void testMultilineComment1() throws Exception { runTest("idea/testData/codeInsight/moveUpDown/expressions/multilineComment1.kt");