Refactoring: use sealed classes for try-catch expression surrounders
This commit is contained in:
+6
-11
@@ -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
|
||||
|
||||
-25
@@ -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) {}"
|
||||
}
|
||||
|
||||
-24
@@ -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 {}"
|
||||
}
|
||||
+12
-2
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user