From 239f88e5d3064344abf23f56db6056d943e9835c Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 12 Sep 2017 13:09:39 +0300 Subject: [PATCH] Refactoring: use sealed classes for try-catch expression surrounders --- .../KotlinExpressionSurroundDescriptor.java | 17 +++++-------- .../KotlinTryCatchExpressionSurrounder.kt | 25 ------------------- ...tlinTryCatchFinallyExpressionSurrounder.kt | 24 ------------------ ...se.kt => KotlinTryExpressionSurrounder.kt} | 14 +++++++++-- .../AbstractSurroundWithTest.java | 5 ++-- 5 files changed, 20 insertions(+), 65 deletions(-) delete mode 100644 idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchExpressionSurrounder.kt delete mode 100644 idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchFinallyExpressionSurrounder.kt rename idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/{KotlinTryExpressionSurrounderBase.kt => KotlinTryExpressionSurrounder.kt} (64%) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java index 4d75c498ce6..eb3998e1633 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurroundDescriptor.java @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils; import org.jetbrains.kotlin.psi.KtExpression; public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { - private static final Surrounder[] SURROUNDERS = { new KotlinNotSurrounder(), new KotlinStringTemplateSurrounder(), @@ -34,8 +33,8 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { new KotlinRuntimeTypeCastSurrounder(), new KotlinWithIfExpressionSurrounder(/* withElse = */false), new KotlinWithIfExpressionSurrounder(/* withElse = */true), - new KotlinTryCatchExpressionSurrounder(), - new KotlinTryCatchFinallyExpressionSurrounder(), + new KotlinTryExpressionSurrounder.TryCatch(), + new KotlinTryExpressionSurrounder.TryCatchFinally(), new KotlinIfElseExpressionSurrounder(/* withBraces = */false), new KotlinIfElseExpressionSurrounder(/* withBraces = */true) }; @@ -43,14 +42,10 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { @Override @NotNull public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { - KtExpression expression = (KtExpression) CodeInsightUtils.findElement(file, - startOffset, - endOffset, - CodeInsightUtils.ElementKind.EXPRESSION); - if (expression == null) { - return PsiElement.EMPTY_ARRAY; - } - return new PsiElement[] {expression}; + KtExpression expression = (KtExpression) CodeInsightUtils.findElement( + file, startOffset, endOffset, CodeInsightUtils.ElementKind.EXPRESSION); + + return expression == null ? PsiElement.EMPTY_ARRAY : new PsiElement[] {expression}; } @Override diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchExpressionSurrounder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchExpressionSurrounder.kt deleted file mode 100644 index cbf5e434508..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchExpressionSurrounder.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2010-2017 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.kotlin.idea.codeInsight.surroundWith.expression - -class KotlinTryCatchExpressionSurrounder : KotlinTryExpressionSurrounderBase() { - - override fun getTemplateDescription() = "try { expr } catch {}" - - override fun getPattern() = "try { $0 } catch (e: Exception) {}" -} - diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchFinallyExpressionSurrounder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchFinallyExpressionSurrounder.kt deleted file mode 100644 index 9a07e2b889a..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchFinallyExpressionSurrounder.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2010-2017 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.kotlin.idea.codeInsight.surroundWith.expression - -class KotlinTryCatchFinallyExpressionSurrounder : KotlinTryExpressionSurrounderBase() { - - override fun getTemplateDescription() = "try { expr } catch {} finally {}" - - override fun getPattern() = "try { $0 } catch (e: Exception) {} finally {}" -} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounderBase.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt similarity index 64% rename from idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounderBase.kt rename to idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt index 7cb5570f5a0..3ba31571748 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounderBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounder.kt @@ -22,10 +22,20 @@ import org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement.KotlinTrySur import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtTryExpression -abstract class KotlinTryExpressionSurrounderBase : KotlinControlFlowExpressionSurrounderBase() { +sealed class KotlinTryExpressionSurrounder : KotlinControlFlowExpressionSurrounderBase() { + class TryCatch : KotlinTryExpressionSurrounder() { + override fun getTemplateDescription() = "try { expr } catch {}" + override fun getPattern() = "try { $0 } catch (e: Exception) {}" + } + + class TryCatchFinally : KotlinTryExpressionSurrounder() { + override fun getTemplateDescription() = "try { expr } catch {} finally {}" + override fun getPattern() = "try { $0 } catch (e: Exception) {} finally {}" + } override fun getRange(editor: Editor, replaced: KtExpression): TextRange? { - return KotlinTrySurrounderBase.getCatchTypeParameterTextRange(replaced as KtTryExpression) + val tryExpression = replaced as KtTryExpression + return KotlinTrySurrounderBase.getCatchTypeParameterTextRange(tryExpression) } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java index e07a53474c4..10f129752fc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -72,7 +72,7 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase } public void doTestWithTryCatchExpressionSurrounder(String path) throws Exception { - doTest(path, new KotlinTryCatchExpressionSurrounder()); + doTest(path, new KotlinTryExpressionSurrounder.TryCatch()); } public void doTestWithTryCatchFinallySurrounder(String path) throws Exception { @@ -80,10 +80,9 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase } public void doTestWithTryCatchFinallyExpressionSurrounder(String path) throws Exception { - doTest(path, new KotlinTryCatchFinallyExpressionSurrounder()); + doTest(path, new KotlinTryExpressionSurrounder.TryCatchFinally()); } - public void doTestWithTryFinallySurrounder(String path) throws Exception { doTest(path, new KotlinTryFinallySurrounder()); }