From b35bac2d9215a6b0e846f8040118658107f80ccc Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 11 Apr 2024 11:55:53 +0200 Subject: [PATCH] [kotlin] support `surround with finally` as expression ^KTIJ-29325 fixed GitOrigin-RevId: 4463e0c5e61614e61c42af14f8314c6017ef71c6 --- .../KotlinExpressionSurroundDescriptor.java | 1 + .../KotlinTryExpressionSurrounder.kt | 17 ++++++++++++++ .../KotlinFirSurroundWithTestGenerated.java | 23 +++++++++++++++++++ .../AbstractSurroundWithTest.java | 4 ++++ .../SurroundWithTestGenerated.java | 23 +++++++++++++++++++ .../tryFinallyExpression/asStatement.kt | 5 ++++ .../tryFinallyExpression/asStatement.kt.after | 5 ++++ .../tryFinallyExpression/singleExpression.kt | 1 + .../singleExpression.kt.after | 4 ++++ .../singleExpressionUnit.kt | 1 + .../singleExpressionUnit.kt.after | 4 ++++ .../fe10/testGenerator/Fe10GenerateTests.kt | 1 + .../codeinsight/GenerateK2CodeInsightTests.kt | 1 + 13 files changed, 90 insertions(+) create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt create mode 100644 plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt.after diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java index 28904c5bc57f..190950eb1463 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java @@ -16,6 +16,7 @@ public class KotlinExpressionSurroundDescriptor extends KotlinExpressionSurround new KotlinWithIfExpressionSurrounder(/* withElse = */true), new KotlinTryExpressionSurrounder.TryCatch(), new KotlinTryExpressionSurrounder.TryCatchFinally(), + new KotlinTryExpressionSurrounder.TryFinally(), new KotlinIfElseExpressionSurrounder(/* withBraces = */false), new KotlinIfElseExpressionSurrounder(/* withBraces = */true) }; diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt index 99d0395d2918..4b0548a5475b 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt @@ -23,6 +23,23 @@ sealed class KotlinTryExpressionSurrounder : KotlinControlFlowExpressionSurround } + class TryFinally : KotlinTryExpressionSurrounder() { + @NlsSafe + override fun getTemplateDescription() = "try { expr } finally {}" + override fun getPattern() = "try { $0 } finally {\nb\n}" + override fun getRange( + editor: Editor, + replaced: KtExpression + ): TextRange? { + val blockExpression = (replaced as KtTryExpression).finallyBlock?.finalExpression ?: return null + val stmt = blockExpression.statements[0] + val range = stmt.textRange + stmt.delete() + val offset = range?.startOffset ?: return null + return TextRange(offset, offset) + } + } + override fun getRange(editor: Editor, replaced: KtExpression): TextRange? { val tryExpression = replaced as KtTryExpression return KotlinTrySurrounderBase.getCatchTypeParameterTextRange(tryExpression) diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/surroundWith/KotlinFirSurroundWithTestGenerated.java b/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/surroundWith/KotlinFirSurroundWithTestGenerated.java index 5a82ea566d2a..43b0cf7254f2 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/surroundWith/KotlinFirSurroundWithTestGenerated.java +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/test/org/jetbrains/kotlin/idea/k2/surroundWith/KotlinFirSurroundWithTestGenerated.java @@ -612,6 +612,29 @@ public abstract class KotlinFirSurroundWithTestGenerated extends AbstractKotlinF } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression") + public static class TryFinallyExpression extends AbstractKotlinFirSurroundWithTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithTryFinallyExpressionSurrounder, this, testDataFilePath); + } + + @TestMetadata("asStatement.kt") + public void testAsStatement() throws Exception { + runTest("../../idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt"); + } + + @TestMetadata("singleExpression.kt") + public void testSingleExpression() throws Exception { + runTest("../../idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt"); + } + + @TestMetadata("singleExpressionUnit.kt") + public void testSingleExpressionUnit() throws Exception { + runTest("../../idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt"); + } + } + @RunWith(JUnit3RunnerWithInners.class) @TestMetadata("../../idea/tests/testData/codeInsight/surroundWith/functionLiteral") public static class FunctionLiteral extends AbstractKotlinFirSurroundWithTest { diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java index 6b61df3a9379..b0335d880fe5 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -63,6 +63,10 @@ public abstract class AbstractSurroundWithTest extends KotlinLightCodeInsightFix doTest(path, new KotlinTryExpressionSurrounder.TryCatch()); } + public void doTestWithTryFinallyExpressionSurrounder(String path) throws Exception { + doTest(path, new KotlinTryExpressionSurrounder.TryFinally()); + } + public void doTestWithTryCatchFinallySurrounder(String path) throws Exception { doTest(path, new KotlinTryCatchFinallySurrounder()); } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java index 1e7fd12378b2..44747c7897d1 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java @@ -612,6 +612,29 @@ public abstract class SurroundWithTestGenerated extends AbstractSurroundWithTest } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/codeInsight/surroundWith/tryFinallyExpression") + public static class TryFinallyExpression extends AbstractSurroundWithTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithTryFinallyExpressionSurrounder, this, testDataFilePath); + } + + @TestMetadata("asStatement.kt") + public void testAsStatement() throws Exception { + runTest("testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt"); + } + + @TestMetadata("singleExpression.kt") + public void testSingleExpression() throws Exception { + runTest("testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt"); + } + + @TestMetadata("singleExpressionUnit.kt") + public void testSingleExpressionUnit() throws Exception { + runTest("testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt"); + } + } + @RunWith(JUnit3RunnerWithInners.class) @TestMetadata("testData/codeInsight/surroundWith/functionLiteral") public static class FunctionLiteral extends AbstractSurroundWithTest { diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt new file mode 100644 index 000000000000..c45a6954f5ab --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: true +fun call() {} +fun foo() { + call() +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt.after new file mode 100644 index 000000000000..c45a6954f5ab --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/asStatement.kt.after @@ -0,0 +1,5 @@ +// IS_APPLICABLE: true +fun call() {} +fun foo() { + call() +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt new file mode 100644 index 000000000000..05eca02b7324 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt @@ -0,0 +1 @@ +fun foo() = "aaa" \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt.after new file mode 100644 index 000000000000..4b5b5573145c --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpression.kt.after @@ -0,0 +1,4 @@ +fun foo() = try { + "aaa" +} finally { +} diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt new file mode 100644 index 000000000000..79e3d0913c27 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt @@ -0,0 +1 @@ +fun foo() = println("") \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt.after b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt.after new file mode 100644 index 000000000000..0723371260c9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression/singleExpressionUnit.kt.after @@ -0,0 +1,4 @@ +fun foo() = try { + println("") +} finally { +} diff --git a/plugins/kotlin/util/test-generator-fe10/test/org/jetbrains/kotlin/fe10/testGenerator/Fe10GenerateTests.kt b/plugins/kotlin/util/test-generator-fe10/test/org/jetbrains/kotlin/fe10/testGenerator/Fe10GenerateTests.kt index 7b0705c22d13..d46fdf9ddaf6 100644 --- a/plugins/kotlin/util/test-generator-fe10/test/org/jetbrains/kotlin/fe10/testGenerator/Fe10GenerateTests.kt +++ b/plugins/kotlin/util/test-generator-fe10/test/org/jetbrains/kotlin/fe10/testGenerator/Fe10GenerateTests.kt @@ -540,6 +540,7 @@ private fun assembleWorkspace(): TWorkspace = workspace { model("codeInsight/surroundWith/tryCatchFinally", testMethodName = "doTestWithTryCatchFinallySurrounder") model("codeInsight/surroundWith/tryCatchFinallyExpression", testMethodName = "doTestWithTryCatchFinallyExpressionSurrounder") model("codeInsight/surroundWith/tryFinally", testMethodName = "doTestWithTryFinallySurrounder") + model("codeInsight/surroundWith/tryFinallyExpression", testMethodName = "doTestWithTryFinallyExpressionSurrounder") model("codeInsight/surroundWith/functionLiteral", testMethodName = "doTestWithFunctionLiteralSurrounder") model("codeInsight/surroundWith/withIfExpression", testMethodName = "doTestWithSurroundWithIfExpression") model("codeInsight/surroundWith/withIfElseExpression", testMethodName = "doTestWithSurroundWithIfElseExpression") diff --git a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2CodeInsightTests.kt b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2CodeInsightTests.kt index 750108b925b4..b1095884cd64 100644 --- a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2CodeInsightTests.kt +++ b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2CodeInsightTests.kt @@ -44,6 +44,7 @@ internal fun MutableTWorkspace.generateK2CodeInsightTests() { model("../../../idea/tests/testData/codeInsight/surroundWith/tryCatchFinally", testMethodName = "doTestWithTryCatchFinallySurrounder") model("../../../idea/tests/testData/codeInsight/surroundWith/tryCatchFinallyExpression", testMethodName = "doTestWithTryCatchFinallyExpressionSurrounder") model("../../../idea/tests/testData/codeInsight/surroundWith/tryFinally", testMethodName = "doTestWithTryFinallySurrounder") + model("../../../idea/tests/testData/codeInsight/surroundWith/tryFinallyExpression", testMethodName = "doTestWithTryFinallyExpressionSurrounder") model("../../../idea/tests/testData/codeInsight/surroundWith/functionLiteral", testMethodName = "doTestWithFunctionLiteralSurrounder") model("../../../idea/tests/testData/codeInsight/surroundWith/withIfExpression", testMethodName = "doTestWithSurroundWithIfExpression") model("../../../idea/tests/testData/codeInsight/surroundWith/withIfElseExpression", testMethodName = "doTestWithSurroundWithIfElseExpression")