From 0b634cc9189d920fe9b5ff846bfaf418c8082414 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 19 Jun 2013 16:08:53 +0400 Subject: [PATCH] Implement Unwrap/Remove for try expressions --- .../jet/generators/tests/GenerateTests.java | 5 + .../codeInsight/unwrap/KoitlinUnwrappers.java | 73 ++++++++++++ .../unwrap/KotlinComponentUnwrapper.java | 17 ++- .../codeInsight/unwrap/KotlinRemover.java | 31 +++++ .../unwrap/KotlinUnwrapDescriptor.java | 5 + .../unwrapAndRemove/removeCatch/catch.kt | 15 +++ .../removeCatch/catch.kt.after | 12 ++ .../removeFinally/finallyInBlock.kt | 18 +++ .../removeFinally/finallyInBlock.kt.after | 15 +++ .../removeFinally/finallyInReturn.kt | 15 +++ .../removeFinally/finallyInReturn.kt.after | 12 ++ .../unwrapCatch/catchCompoundInBlock.kt | 18 +++ .../unwrapCatch/catchCompoundInBlock.kt.after | 7 ++ .../unwrapCatch/catchCompoundInReturn.kt | 16 +++ .../unwrapCatch/catchSimpleInReturn.kt | 15 +++ .../unwrapCatch/catchSimpleInReturn.kt.after | 4 + .../unwrapFinally/finallyCompoundInBlock.kt | 18 +++ .../finallyCompoundInBlock.kt.after | 7 ++ .../unwrapFinally/finallyCompoundInReturn.kt | 16 +++ .../unwrapFinally/finallySimpleInReturn.kt | 15 +++ .../unwrapTry/tryCompoundInBlock.kt | 11 ++ .../unwrapTry/tryCompoundInBlock.kt.after | 7 ++ .../unwrapTry/tryCompoundInReturn.kt | 9 ++ .../unwrapTry/trySimpleInReturn.kt | 8 ++ .../unwrapTry/trySimpleInReturn.kt.after | 4 + .../unwrap/AbstractUnwrapRemoveTest.java | 20 ++++ .../unwrap/UnwrapRemoveTestGenerated.java | 107 +++++++++++++++++- 27 files changed, 494 insertions(+), 6 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinRemover.java create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallySimpleInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt.after create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt create mode 100644 idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt.after diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 89d576bba48..d0922dcbd7d 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -379,6 +379,11 @@ public class GenerateTests { testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapElse", "doTestElseUnwrapper"), testModel("idea/testData/codeInsight/unwrapAndRemove/removeElse", "doTestElseRemover"), testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapLoop", "doTestLoopUnwrapper"), + testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapTry", "doTestTryUnwrapper"), + testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch", "doTestCatchUnwrapper"), + testModel("idea/testData/codeInsight/unwrapAndRemove/removeCatch", "doTestCatchRemover"), + testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally", "doTestFinallyUnwrapper"), + testModel("idea/testData/codeInsight/unwrapAndRemove/removeFinally", "doTestFinallyRemover"), testModel("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda", "doTestLambdaUnwrapper") ); } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KoitlinUnwrappers.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KoitlinUnwrappers.java index fe5c517be1c..52d4a760487 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KoitlinUnwrappers.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KoitlinUnwrappers.java @@ -76,4 +76,77 @@ public class KoitlinUnwrappers { return target instanceof JetLoopExpression ? ((JetLoopExpression) target).getBody() : null; } } + + public static class KotlinTryUnwrapper extends KotlinComponentUnwrapper { + public KotlinTryUnwrapper(String key) { + super(key); + } + + @Override + @Nullable + protected JetExpression getExpressionToUnwrap(@NotNull JetElement target) { + return target instanceof JetTryExpression ? ((JetTryExpression) target).getTryBlock() : null; + } + } + + public static class KotlinCatchUnwrapper extends KotlinComponentUnwrapper { + public KotlinCatchUnwrapper(String key) { + super(key); + } + + @NotNull + @Override + protected JetElement getEnclosingElement(@NotNull JetElement element) { + return (JetElement)element.getParent(); + } + + @Override + protected JetExpression getExpressionToUnwrap(@NotNull JetElement target) { + return target instanceof JetCatchClause ? ((JetCatchClause) target).getCatchBody() : null; + } + } + + public static class KotlinCatchRemover extends KotlinRemover { + public KotlinCatchRemover(String key) { + super(key); + } + + @Override + public boolean isApplicableTo(PsiElement e) { + return e instanceof JetCatchClause; + } + } + + public static class KotlinFinallyUnwrapper extends KotlinComponentUnwrapper { + public KotlinFinallyUnwrapper(String key) { + super(key); + } + + @Override + public boolean isApplicableTo(PsiElement e) { + return super.isApplicableTo(e) && getEnclosingElement((JetElement)e).getParent() instanceof JetBlockExpression; + } + + @NotNull + @Override + protected JetElement getEnclosingElement(@NotNull JetElement element) { + return (JetElement)element.getParent(); + } + + @Override + protected JetExpression getExpressionToUnwrap(@NotNull JetElement target) { + return target instanceof JetFinallySection ? ((JetFinallySection) target).getFinalExpression() : null; + } + } + + public static class KotlinFinallyRemover extends KotlinRemover { + public KotlinFinallyRemover(String key) { + super(key); + } + + @Override + public boolean isApplicableTo(PsiElement e) { + return e instanceof JetFinallySection; + } + } } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinComponentUnwrapper.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinComponentUnwrapper.java index be72bcaf98a..54e8e5d5316 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinComponentUnwrapper.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinComponentUnwrapper.java @@ -31,21 +31,28 @@ public abstract class KotlinComponentUnwrapper extends KotlinUnwrapRemoveBase { @Nullable protected abstract JetExpression getExpressionToUnwrap(@NotNull JetElement target); + @NotNull + protected JetElement getEnclosingElement(@NotNull JetElement element) { + return element; + } + @Override public boolean isApplicableTo(PsiElement e) { if (!(e instanceof JetElement)) return false; JetExpression expressionToUnwrap = getExpressionToUnwrap((JetElement) e); - return expressionToUnwrap != null && canExtractExpression(expressionToUnwrap, (JetElement) e.getParent()); + return expressionToUnwrap != null && canExtractExpression(expressionToUnwrap, + (JetElement) getEnclosingElement((JetElement) e).getParent()); } @Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { - JetExpression targetExpression = (JetExpression) element; - JetExpression expressionToUnwrap = getExpressionToUnwrap(targetExpression); + JetElement targetElement = (JetElement) element; + JetExpression expressionToUnwrap = getExpressionToUnwrap(targetElement); assert expressionToUnwrap != null; - context.extractFromExpression(expressionToUnwrap, targetExpression); - context.delete(targetExpression); + JetElement enclosingElement = getEnclosingElement(targetElement); + context.extractFromExpression(expressionToUnwrap, enclosingElement); + context.delete(enclosingElement); } } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinRemover.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinRemover.java new file mode 100644 index 00000000000..4522986eb46 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinRemover.java @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.codeInsight.unwrap; + +import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; + +public abstract class KotlinRemover extends KotlinUnwrapRemoveBase { + public KotlinRemover(String key) { + super(key); + } + + @Override + protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { + context.delete(element); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinUnwrapDescriptor.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinUnwrapDescriptor.java index d3f661f6433..a105ac0d978 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinUnwrapDescriptor.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/unwrap/KotlinUnwrapDescriptor.java @@ -27,6 +27,11 @@ public class KotlinUnwrapDescriptor extends UnwrapDescriptorBase { new KoitlinUnwrappers.KotlinElseRemover("remove.else"), new KoitlinUnwrappers.KotlinElseUnwrapper("unwrap.else"), new KoitlinUnwrappers.KotlinLoopUnwrapper("unwrap.expression"), + new KoitlinUnwrappers.KotlinTryUnwrapper("unwrap.expression"), + new KoitlinUnwrappers.KotlinCatchUnwrapper("unwrap.expression"), + new KoitlinUnwrappers.KotlinCatchRemover("remove.expression"), + new KoitlinUnwrappers.KotlinFinallyUnwrapper("unwrap.expression"), + new KoitlinUnwrappers.KotlinFinallyRemover("remove.expression"), new KotlinLambdaUnwrapper("unwrap.expression"), }; } diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt b/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt new file mode 100644 index 00000000000..d34c09620f6 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt @@ -0,0 +1,15 @@ +// OPTION: 1 +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt.after b/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt.after new file mode 100644 index 00000000000..cd1400f4005 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt.after @@ -0,0 +1,12 @@ +// OPTION: 1 +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: Exception) { + -2 + } + finally { + + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt new file mode 100644 index 00000000000..5a5db2ee401 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt @@ -0,0 +1,18 @@ +// OPTION: 1 +fun foo(n: Int): Int { + try { + n / 0 + } + catch (e: ArithmeticException) { + val m = -1 + m + } + catch (e: Exception) { + -2 + } + finally { + println("finally") + } + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt.after b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt.after new file mode 100644 index 00000000000..3ff07da9dd7 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt.after @@ -0,0 +1,15 @@ +// OPTION: 1 +fun foo(n: Int): Int { + try { + n / 0 + } + catch (e: ArithmeticException) { + val m = -1 + m + } + catch (e: Exception) { + -2 + } + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt new file mode 100644 index 00000000000..f5058f8b146 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt @@ -0,0 +1,15 @@ +// OPTION: 0 +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + println("finally") + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt.after b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt.after new file mode 100644 index 00000000000..cf503cb3327 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt.after @@ -0,0 +1,12 @@ +// OPTION: 0 +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt new file mode 100644 index 00000000000..b0ced8a8791 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt @@ -0,0 +1,18 @@ +// OPTION: 0 +fun foo(n: Int): Int { + try { + n / 0 + } + catch (e: ArithmeticException) { + val m = -1 + m + } + catch (e: Exception) { + -2 + } + finally { + + } + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt.after new file mode 100644 index 00000000000..c5bfbd423e9 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt.after @@ -0,0 +1,7 @@ +// OPTION: 0 +fun foo(n: Int): Int { + val m = -1 + m + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInReturn.kt new file mode 100644 index 00000000000..b00f7d3e42c --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInReturn.kt @@ -0,0 +1,16 @@ +// IS_APPLICABLE: false +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + val m = -1 + m + } + catch (e: Exception) { + -2 + } + finally { + + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt new file mode 100644 index 00000000000..a0ee0916d9e --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt @@ -0,0 +1,15 @@ +// OPTION: 0 +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt.after new file mode 100644 index 00000000000..d9cdb7d3d57 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt.after @@ -0,0 +1,4 @@ +// OPTION: 0 +fun foo(n: Int): Int { + return -1 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt new file mode 100644 index 00000000000..eafab6dcd4b --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt @@ -0,0 +1,18 @@ +// OPTION: 0 +fun foo(n: Int): Int { + try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + val s = "finally" + println(s) + } + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt.after new file mode 100644 index 00000000000..3d853eed449 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt.after @@ -0,0 +1,7 @@ +// OPTION: 0 +fun foo(n: Int): Int { + val s = "finally" + println(s) + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInReturn.kt new file mode 100644 index 00000000000..a0ab13ebc85 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInReturn.kt @@ -0,0 +1,16 @@ +// IS_APPLICABLE: false +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + val s = "finally" + println(s) + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallySimpleInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallySimpleInReturn.kt new file mode 100644 index 00000000000..4b732e94b07 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallySimpleInReturn.kt @@ -0,0 +1,15 @@ +// IS_APPLICABLE: false +fun foo(n: Int): Int { + return try { + n / 0 + } + catch (e: ArithmeticException) { + -1 + } + catch (e: Exception) { + -2 + } + finally { + println("finally") + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt new file mode 100644 index 00000000000..7ab7aac30fd --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt @@ -0,0 +1,11 @@ +// OPTION: 1 +fun foo(n : Int): Int { + try { + val m = n + 1 + m/0 + } catch (e: Exception) { + -1 + } + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt.after new file mode 100644 index 00000000000..6240f7e1d2c --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt.after @@ -0,0 +1,7 @@ +// OPTION: 1 +fun foo(n : Int): Int { + val m = n + 1 + m/0 + + return 0 +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInReturn.kt new file mode 100644 index 00000000000..2390c58ba98 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInReturn.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +fun foo(n : Int): Int { + return try { + val m = n + 1 + m/0 + } catch (e: Exception) { + -1 + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt new file mode 100644 index 00000000000..3b53ecdb503 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt @@ -0,0 +1,8 @@ +// OPTION: 0 +fun foo(n : Int): Int { + return try { + n/0 + } catch (e: Exception) { + -1 + } +} diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt.after new file mode 100644 index 00000000000..3135d2b7da7 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt.after @@ -0,0 +1,4 @@ +// OPTION: 0 +fun foo(n : Int): Int { + return n/0 +} diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/AbstractUnwrapRemoveTest.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/AbstractUnwrapRemoveTest.java index fdb85ac02f2..964b1a8940a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/AbstractUnwrapRemoveTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/AbstractUnwrapRemoveTest.java @@ -46,6 +46,26 @@ public abstract class AbstractUnwrapRemoveTest extends LightCodeInsightTestCase doTest(path, KoitlinUnwrappers.KotlinLoopUnwrapper.class); } + public void doTestTryUnwrapper(@NotNull String path) throws Exception { + doTest(path, KoitlinUnwrappers.KotlinTryUnwrapper.class); + } + + public void doTestCatchUnwrapper(@NotNull String path) throws Exception { + doTest(path, KoitlinUnwrappers.KotlinCatchUnwrapper.class); + } + + public void doTestCatchRemover(@NotNull String path) throws Exception { + doTest(path, KoitlinUnwrappers.KotlinCatchRemover.class); + } + + public void doTestFinallyUnwrapper(@NotNull String path) throws Exception { + doTest(path, KoitlinUnwrappers.KotlinFinallyUnwrapper.class); + } + + public void doTestFinallyRemover(@NotNull String path) throws Exception { + doTest(path, KoitlinUnwrappers.KotlinFinallyRemover.class); + } + public void doTestLambdaUnwrapper(@NotNull String path) throws Exception { doTest(path, KotlinLambdaUnwrapper.class); } diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/UnwrapRemoveTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/UnwrapRemoveTestGenerated.java index 79a7e9fd9b4..92c013c37e9 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/UnwrapRemoveTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/unwrap/UnwrapRemoveTestGenerated.java @@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.codeInsight.unwrap.AbstractUnwrapRemoveTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") -@InnerTestClasses({UnwrapRemoveTestGenerated.UnwrapThen.class, UnwrapRemoveTestGenerated.UnwrapElse.class, UnwrapRemoveTestGenerated.RemoveElse.class, UnwrapRemoveTestGenerated.UnwrapLoop.class, UnwrapRemoveTestGenerated.UnwrapLambda.class}) +@InnerTestClasses({UnwrapRemoveTestGenerated.UnwrapThen.class, UnwrapRemoveTestGenerated.UnwrapElse.class, UnwrapRemoveTestGenerated.RemoveElse.class, UnwrapRemoveTestGenerated.UnwrapLoop.class, UnwrapRemoveTestGenerated.UnwrapTry.class, UnwrapRemoveTestGenerated.UnwrapCatch.class, UnwrapRemoveTestGenerated.RemoveCatch.class, UnwrapRemoveTestGenerated.UnwrapFinally.class, UnwrapRemoveTestGenerated.RemoveFinally.class, UnwrapRemoveTestGenerated.UnwrapLambda.class}) public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest { @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapThen") public static class UnwrapThen extends AbstractUnwrapRemoveTest { @@ -114,6 +114,106 @@ public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest { } + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapTry") + public static class UnwrapTry extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInUnwrapTry() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/unwrapTry"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("tryCompoundInBlock.kt") + public void testTryCompoundInBlock() throws Exception { + doTestTryUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInBlock.kt"); + } + + @TestMetadata("tryCompoundInReturn.kt") + public void testTryCompoundInReturn() throws Exception { + doTestTryUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapTry/tryCompoundInReturn.kt"); + } + + @TestMetadata("trySimpleInReturn.kt") + public void testTrySimpleInReturn() throws Exception { + doTestTryUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapTry/trySimpleInReturn.kt"); + } + + } + + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch") + public static class UnwrapCatch extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInUnwrapCatch() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("catchCompoundInBlock.kt") + public void testCatchCompoundInBlock() throws Exception { + doTestCatchUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInBlock.kt"); + } + + @TestMetadata("catchCompoundInReturn.kt") + public void testCatchCompoundInReturn() throws Exception { + doTestCatchUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchCompoundInReturn.kt"); + } + + @TestMetadata("catchSimpleInReturn.kt") + public void testCatchSimpleInReturn() throws Exception { + doTestCatchUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapCatch/catchSimpleInReturn.kt"); + } + + } + + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/removeCatch") + public static class RemoveCatch extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInRemoveCatch() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/removeCatch"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("catch.kt") + public void testCatch() throws Exception { + doTestCatchRemover("idea/testData/codeInsight/unwrapAndRemove/removeCatch/catch.kt"); + } + + } + + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally") + public static class UnwrapFinally extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInUnwrapFinally() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("finallyCompoundInBlock.kt") + public void testFinallyCompoundInBlock() throws Exception { + doTestFinallyUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInBlock.kt"); + } + + @TestMetadata("finallyCompoundInReturn.kt") + public void testFinallyCompoundInReturn() throws Exception { + doTestFinallyUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallyCompoundInReturn.kt"); + } + + @TestMetadata("finallySimpleInReturn.kt") + public void testFinallySimpleInReturn() throws Exception { + doTestFinallyUnwrapper("idea/testData/codeInsight/unwrapAndRemove/unwrapFinally/finallySimpleInReturn.kt"); + } + + } + + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/removeFinally") + public static class RemoveFinally extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInRemoveFinally() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/unwrapAndRemove/removeFinally"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("finallyInBlock.kt") + public void testFinallyInBlock() throws Exception { + doTestFinallyRemover("idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInBlock.kt"); + } + + @TestMetadata("finallyInReturn.kt") + public void testFinallyInReturn() throws Exception { + doTestFinallyRemover("idea/testData/codeInsight/unwrapAndRemove/removeFinally/finallyInReturn.kt"); + } + + } + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapLambda") public static class UnwrapLambda extends AbstractUnwrapRemoveTest { public void testAllFilesPresentInUnwrapLambda() throws Exception { @@ -178,6 +278,11 @@ public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest { suite.addTestSuite(UnwrapElse.class); suite.addTestSuite(RemoveElse.class); suite.addTestSuite(UnwrapLoop.class); + suite.addTestSuite(UnwrapTry.class); + suite.addTestSuite(UnwrapCatch.class); + suite.addTestSuite(RemoveCatch.class); + suite.addTestSuite(UnwrapFinally.class); + suite.addTestSuite(RemoveFinally.class); suite.addTestSuite(UnwrapLambda.class); return suite; }