[kotlin] support surround with finally as expression

^KTIJ-29325 fixed

GitOrigin-RevId: 4463e0c5e61614e61c42af14f8314c6017ef71c6
This commit is contained in:
Anna Kozlova
2024-04-11 12:03:40 +00:00
committed by intellij-monorepo-bot
parent 20dea6b25e
commit b35bac2d92
13 changed files with 90 additions and 0 deletions
@@ -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)
};
@@ -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)
@@ -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 {
@@ -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());
}
@@ -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 {
@@ -0,0 +1,5 @@
// IS_APPLICABLE: true
fun call() {}
fun foo() {
<selection>call()</selection>
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: true
fun call() {}
fun foo() {
<selection>call()</selection>
}
@@ -0,0 +1 @@
fun foo() = <selection>"aaa"</selection>
@@ -0,0 +1,4 @@
fun foo() = try {
"aaa"
} finally {
<selection><caret></selection>}
@@ -0,0 +1 @@
fun foo() = <selection>println("")</selection>
@@ -0,0 +1,4 @@
fun foo() = try {
println("")
} finally {
<selection><caret></selection>}
@@ -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")
@@ -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")