Implement "Surround with try/catch(/finally)" and "Surround with if/else" for expressions
Fixes #KT-14175 Fixes #KT-19782
This commit is contained in:
committed by
Nikolay Krasko
parent
e16d16fdb7
commit
08103d1cb4
@@ -672,12 +672,16 @@ fun main(args: Array<String>) {
|
|||||||
testClass<AbstractSurroundWithTest> {
|
testClass<AbstractSurroundWithTest> {
|
||||||
model("codeInsight/surroundWith/if", testMethod = "doTestWithIfSurrounder")
|
model("codeInsight/surroundWith/if", testMethod = "doTestWithIfSurrounder")
|
||||||
model("codeInsight/surroundWith/ifElse", testMethod = "doTestWithIfElseSurrounder")
|
model("codeInsight/surroundWith/ifElse", testMethod = "doTestWithIfElseSurrounder")
|
||||||
|
model("codeInsight/surroundWith/ifElseExpression", testMethod = "doTestWithIfElseExpressionSurrounder")
|
||||||
|
model("codeInsight/surroundWith/ifElseExpressionBraces", testMethod = "doTestWithIfElseExpressionBracesSurrounder")
|
||||||
model("codeInsight/surroundWith/not", testMethod = "doTestWithNotSurrounder")
|
model("codeInsight/surroundWith/not", testMethod = "doTestWithNotSurrounder")
|
||||||
model("codeInsight/surroundWith/parentheses", testMethod = "doTestWithParenthesesSurrounder")
|
model("codeInsight/surroundWith/parentheses", testMethod = "doTestWithParenthesesSurrounder")
|
||||||
model("codeInsight/surroundWith/stringTemplate", testMethod = "doTestWithStringTemplateSurrounder")
|
model("codeInsight/surroundWith/stringTemplate", testMethod = "doTestWithStringTemplateSurrounder")
|
||||||
model("codeInsight/surroundWith/when", testMethod = "doTestWithWhenSurrounder")
|
model("codeInsight/surroundWith/when", testMethod = "doTestWithWhenSurrounder")
|
||||||
model("codeInsight/surroundWith/tryCatch", testMethod = "doTestWithTryCatchSurrounder")
|
model("codeInsight/surroundWith/tryCatch", testMethod = "doTestWithTryCatchSurrounder")
|
||||||
|
model("codeInsight/surroundWith/tryCatchExpression", testMethod = "doTestWithTryCatchExpressionSurrounder")
|
||||||
model("codeInsight/surroundWith/tryCatchFinally", testMethod = "doTestWithTryCatchFinallySurrounder")
|
model("codeInsight/surroundWith/tryCatchFinally", testMethod = "doTestWithTryCatchFinallySurrounder")
|
||||||
|
model("codeInsight/surroundWith/tryCatchFinallyExpression", testMethod = "doTestWithTryCatchFinallyExpressionSurrounder")
|
||||||
model("codeInsight/surroundWith/tryFinally", testMethod = "doTestWithTryFinallySurrounder")
|
model("codeInsight/surroundWith/tryFinally", testMethod = "doTestWithTryFinallySurrounder")
|
||||||
model("codeInsight/surroundWith/functionLiteral", testMethod = "doTestWithFunctionLiteralSurrounder")
|
model("codeInsight/surroundWith/functionLiteral", testMethod = "doTestWithFunctionLiteralSurrounder")
|
||||||
model("codeInsight/surroundWith/withIfExpression", testMethod = "doTestWithSurroundWithIfExpression")
|
model("codeInsight/surroundWith/withIfExpression", testMethod = "doTestWithSurroundWithIfExpression")
|
||||||
|
|||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.CodeInsightUtilBase
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
|
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||||
|
|
||||||
|
abstract class KotlinControlFlowExpressionSurrounderBase : KotlinExpressionSurrounder() {
|
||||||
|
|
||||||
|
override fun isApplicable(expression: KtExpression) = true
|
||||||
|
override fun isApplicableToStatements() = false
|
||||||
|
|
||||||
|
override fun surroundExpression(project: Project, editor: Editor, expression: KtExpression): TextRange? {
|
||||||
|
val factory = KtPsiFactory(expression)
|
||||||
|
|
||||||
|
val newElement = factory.createExpressionByPattern(getPattern(), expression.text)
|
||||||
|
val replaced = expression.replaced(newElement)
|
||||||
|
|
||||||
|
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(replaced)
|
||||||
|
|
||||||
|
return getRange(editor, replaced)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun getPattern(): String
|
||||||
|
protected abstract fun getRange(editor: Editor, replaced: KtExpression): TextRange?
|
||||||
|
}
|
||||||
+6
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -33,7 +33,11 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
|
|||||||
new KotlinWhenSurrounder() ,
|
new KotlinWhenSurrounder() ,
|
||||||
new KotlinRuntimeTypeCastSurrounder(),
|
new KotlinRuntimeTypeCastSurrounder(),
|
||||||
new KotlinWithIfExpressionSurrounder(/* withElse = */false),
|
new KotlinWithIfExpressionSurrounder(/* withElse = */false),
|
||||||
new KotlinWithIfExpressionSurrounder(/* withElse = */true)
|
new KotlinWithIfExpressionSurrounder(/* withElse = */true),
|
||||||
|
new KotlinTryCatchExpressionSurrounder(),
|
||||||
|
new KotlinTryCatchFinallyExpressionSurrounder(),
|
||||||
|
new KotlinIfElseExpressionSurrounder(/* withBraces = */false),
|
||||||
|
new KotlinIfElseExpressionSurrounder(/* withBraces = */true)
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+15
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils;
|
|||||||
import org.jetbrains.kotlin.psi.KtCallExpression;
|
import org.jetbrains.kotlin.psi.KtCallExpression;
|
||||||
import org.jetbrains.kotlin.psi.KtExpression;
|
import org.jetbrains.kotlin.psi.KtExpression;
|
||||||
import org.jetbrains.kotlin.psi.KtQualifiedExpression;
|
import org.jetbrains.kotlin.psi.KtQualifiedExpression;
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
|
||||||
import org.jetbrains.kotlin.types.KotlinType;
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
|
|
||||||
@@ -44,13 +45,24 @@ public abstract class KotlinExpressionSurrounder implements Surrounder {
|
|||||||
if (expression instanceof KtCallExpression && expression.getParent() instanceof KtQualifiedExpression) {
|
if (expression instanceof KtCallExpression && expression.getParent() instanceof KtQualifiedExpression) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
KotlinType type = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL).getType(expression);
|
BindingContext context = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL);
|
||||||
if (type == null || isUnit(type)) {
|
KotlinType type = context.getType(expression);
|
||||||
|
if (type == null || (isUnit(type) && isApplicableToStatements())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean usedAsExpression = Boolean.TRUE.equals(context.get(BindingContext.USED_AS_EXPRESSION, expression));
|
||||||
|
if (!isApplicableToStatements() && !usedAsExpression) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return isApplicable(expression);
|
return isApplicable(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean isApplicableToStatements() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract boolean isApplicable(@NotNull KtExpression expression);
|
protected abstract boolean isApplicable(@NotNull KtExpression expression);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement.KotlinIfSurrounderBase
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtIfExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
|
||||||
|
|
||||||
|
class KotlinIfElseExpressionSurrounder(private val withBraces: Boolean) : KotlinControlFlowExpressionSurrounderBase() {
|
||||||
|
override fun getPattern(): String {
|
||||||
|
return if (withBraces) "if (a) { \n$0 } else {\n}" else "if (a) $0 else"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getTemplateDescription(): String {
|
||||||
|
return if (withBraces) "if () { expr } else {}" else "if () expr else"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getRange(editor: Editor, replaced: KtExpression): TextRange? {
|
||||||
|
val expression = when (replaced) {
|
||||||
|
is KtParenthesizedExpression -> replaced.expression
|
||||||
|
else -> replaced
|
||||||
|
} as? KtIfExpression
|
||||||
|
|
||||||
|
return expression?.let { KotlinIfSurrounderBase.getRange(editor, it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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 {}"
|
||||||
|
}
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement.KotlinTrySurrounderBase
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtTryExpression
|
||||||
|
|
||||||
|
abstract class KotlinTryExpressionSurrounderBase : KotlinControlFlowExpressionSurrounderBase() {
|
||||||
|
|
||||||
|
override fun getRange(editor: Editor, replaced: KtExpression): TextRange? {
|
||||||
|
return KotlinTrySurrounderBase.getCatchTypeParameterTextRange(replaced as KtTryExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+11
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -33,6 +33,11 @@ import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
|
|||||||
|
|
||||||
public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder {
|
public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isApplicableWhenUsedAsExpression() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
||||||
@@ -58,6 +63,11 @@ public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder
|
|||||||
|
|
||||||
ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
|
ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
|
||||||
|
|
||||||
|
return getRange(editor, ifExpression);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static TextRange getRange(Editor editor, @NotNull KtIfExpression ifExpression) {
|
||||||
KtExpression condition = ifExpression.getCondition();
|
KtExpression condition = ifExpression.getCondition();
|
||||||
assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
|
assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
|
||||||
// Delete condition from created if
|
// Delete condition from created if
|
||||||
|
|||||||
+26
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -24,12 +24,36 @@ import com.intellij.psi.PsiElement;
|
|||||||
import com.intellij.util.IncorrectOperationException;
|
import com.intellij.util.IncorrectOperationException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils;
|
||||||
|
import org.jetbrains.kotlin.psi.KtElement;
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression;
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||||
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
|
||||||
|
|
||||||
public abstract class KotlinStatementsSurrounder implements Surrounder {
|
public abstract class KotlinStatementsSurrounder implements Surrounder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
||||||
return elements.length > 0;
|
if (elements.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elements.length == 1 || !(elements[0] instanceof KtExpression) && !isApplicableWhenUsedAsExpression()) {
|
||||||
|
KtElement expression = (KtElement) elements[0];
|
||||||
|
|
||||||
|
Boolean usedAsExpression = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL)
|
||||||
|
.get(BindingContext.USED_AS_EXPRESSION, expression);
|
||||||
|
|
||||||
|
if (usedAsExpression != null && usedAsExpression) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isApplicableWhenUsedAsExpression() {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+7
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -29,6 +29,11 @@ import org.jetbrains.kotlin.psi.*;
|
|||||||
|
|
||||||
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
|
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isApplicableWhenUsedAsExpression() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
|
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
|
||||||
@@ -61,7 +66,7 @@ public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder
|
|||||||
@NotNull
|
@NotNull
|
||||||
protected abstract TextRange getTextRangeForCaret(@NotNull KtTryExpression expression);
|
protected abstract TextRange getTextRangeForCaret(@NotNull KtTryExpression expression);
|
||||||
|
|
||||||
protected static TextRange getCatchTypeParameterTextRange(@NotNull KtTryExpression expression) {
|
public static TextRange getCatchTypeParameterTextRange(@NotNull KtTryExpression expression) {
|
||||||
KtParameter parameter = expression.getCatchClauses().get(0).getCatchParameter();
|
KtParameter parameter = expression.getCatchClauses().get(0).getCatchParameter();
|
||||||
assert parameter != null : "Catch parameter should exists for " + expression.getText();
|
assert parameter != null : "Catch parameter should exists for " + expression.getText();
|
||||||
KtElement typeReference = parameter.getTypeReference();
|
KtElement typeReference = parameter.getTypeReference();
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a = if(true) {
|
||||||
|
<selection>"aaa"</selection>
|
||||||
|
} else {
|
||||||
|
"bbb"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a = if(true) {
|
||||||
|
<selection>"aaa"</selection>
|
||||||
|
} else {
|
||||||
|
"bbb"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
fun call() {}
|
||||||
|
|
||||||
|
<selection>call()</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
<selection>"aaa"</selection> + 1
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
(if (<selection></selection>) "aaa" else) + 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = <selection>"aaa"</selection>
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = if (<selection></selection>) "aaa" else
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
fun call() {}
|
||||||
|
|
||||||
|
<selection>call()</selection>
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = <selection>"aaa"</selection>
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = if (<selection></selection>) {
|
||||||
|
"aaa"
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a = if(true) {
|
||||||
|
<selection>"aaa"</selection>
|
||||||
|
} else {
|
||||||
|
"bbb"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
fun call() {}
|
||||||
|
|
||||||
|
<selection>call()</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = <selection>"aaa"</selection>
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = try {
|
||||||
|
"aaa"
|
||||||
|
} catch (e: <selection>Exception</selection>) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a = if(true) {
|
||||||
|
<selection>"aaa"</selection>
|
||||||
|
} else {
|
||||||
|
"bbb"
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
fun call() {}
|
||||||
|
|
||||||
|
<selection>call()</selection>
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = <selection>"aaa"</selection>
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = try {
|
||||||
|
"aaa"
|
||||||
|
} catch (e: <selection>Exception</selection>) {
|
||||||
|
} finally {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val a = if(true) {
|
||||||
|
<selection>"aaa"</selection>
|
||||||
|
} else {
|
||||||
|
"bbb"
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -43,6 +43,14 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
|
|||||||
doTest(path, new KotlinIfElseSurrounder());
|
doTest(path, new KotlinIfElseSurrounder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void doTestWithIfElseExpressionSurrounder(String path) throws Exception {
|
||||||
|
doTest(path, new KotlinIfElseExpressionSurrounder(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doTestWithIfElseExpressionBracesSurrounder(String path) throws Exception {
|
||||||
|
doTest(path, new KotlinIfElseExpressionSurrounder(true));
|
||||||
|
}
|
||||||
|
|
||||||
public void doTestWithNotSurrounder(String path) throws Exception {
|
public void doTestWithNotSurrounder(String path) throws Exception {
|
||||||
doTest(path, new KotlinNotSurrounder());
|
doTest(path, new KotlinNotSurrounder());
|
||||||
}
|
}
|
||||||
@@ -63,10 +71,19 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
|
|||||||
doTest(path, new KotlinTryCatchSurrounder());
|
doTest(path, new KotlinTryCatchSurrounder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void doTestWithTryCatchExpressionSurrounder(String path) throws Exception {
|
||||||
|
doTest(path, new KotlinTryCatchExpressionSurrounder());
|
||||||
|
}
|
||||||
|
|
||||||
public void doTestWithTryCatchFinallySurrounder(String path) throws Exception {
|
public void doTestWithTryCatchFinallySurrounder(String path) throws Exception {
|
||||||
doTest(path, new KotlinTryCatchFinallySurrounder());
|
doTest(path, new KotlinTryCatchFinallySurrounder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void doTestWithTryCatchFinallyExpressionSurrounder(String path) throws Exception {
|
||||||
|
doTest(path, new KotlinTryCatchFinallyExpressionSurrounder());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void doTestWithTryFinallySurrounder(String path) throws Exception {
|
public void doTestWithTryFinallySurrounder(String path) throws Exception {
|
||||||
doTest(path, new KotlinTryFinallySurrounder());
|
doTest(path, new KotlinTryFinallySurrounder());
|
||||||
}
|
}
|
||||||
|
|||||||
+120
@@ -68,6 +68,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
doTestWithIfSurrounder(fileName);
|
doTestWithIfSurrounder(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedAsExpression.kt")
|
||||||
|
public void testUsedAsExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/if/usedAsExpression.kt");
|
||||||
|
doTestWithIfSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("variable.kt")
|
@TestMetadata("variable.kt")
|
||||||
public void testVariable() throws Exception {
|
public void testVariable() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/if/variable.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/if/variable.kt");
|
||||||
@@ -311,6 +317,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
doTestWithIfElseSurrounder(fileName);
|
doTestWithIfElseSurrounder(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedAsExpression.kt")
|
||||||
|
public void testUsedAsExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElse/usedAsExpression.kt");
|
||||||
|
doTestWithIfElseSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("variable.kt")
|
@TestMetadata("variable.kt")
|
||||||
public void testVariable() throws Exception {
|
public void testVariable() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElse/variable.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElse/variable.kt");
|
||||||
@@ -318,6 +330,54 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/codeInsight/surroundWith/ifElseExpression")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class IfElseExpression extends AbstractSurroundWithTest {
|
||||||
|
public void testAllFilesPresentInIfElseExpression() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/surroundWith/ifElseExpression"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asStatement.kt")
|
||||||
|
public void testAsStatement() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElseExpression/asStatement.kt");
|
||||||
|
doTestWithIfElseExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("paranthesized.kt")
|
||||||
|
public void testParanthesized() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt");
|
||||||
|
doTestWithIfElseExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleExpression.kt")
|
||||||
|
public void testSingleExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt");
|
||||||
|
doTestWithIfElseExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/codeInsight/surroundWith/ifElseExpressionBraces")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class IfElseExpressionBraces extends AbstractSurroundWithTest {
|
||||||
|
public void testAllFilesPresentInIfElseExpressionBraces() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/surroundWith/ifElseExpressionBraces"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asStatement.kt")
|
||||||
|
public void testAsStatement() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/asStatement.kt");
|
||||||
|
doTestWithIfElseExpressionBracesSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleExpression.kt")
|
||||||
|
public void testSingleExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt");
|
||||||
|
doTestWithIfElseExpressionBracesSurrounder(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/codeInsight/surroundWith/not")
|
@TestMetadata("idea/testData/codeInsight/surroundWith/not")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
@@ -544,6 +604,33 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatch/singleExpression.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatch/singleExpression.kt");
|
||||||
doTestWithTryCatchSurrounder(fileName);
|
doTestWithTryCatchSurrounder(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedAsExpression.kt")
|
||||||
|
public void testUsedAsExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatch/usedAsExpression.kt");
|
||||||
|
doTestWithTryCatchSurrounder(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/codeInsight/surroundWith/tryCatchExpression")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class TryCatchExpression extends AbstractSurroundWithTest {
|
||||||
|
public void testAllFilesPresentInTryCatchExpression() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/surroundWith/tryCatchExpression"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asStatement.kt")
|
||||||
|
public void testAsStatement() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchExpression/asStatement.kt");
|
||||||
|
doTestWithTryCatchExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleExpression.kt")
|
||||||
|
public void testSingleExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt");
|
||||||
|
doTestWithTryCatchExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally")
|
@TestMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally")
|
||||||
@@ -565,6 +652,33 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally/singleExpression.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally/singleExpression.kt");
|
||||||
doTestWithTryCatchFinallySurrounder(fileName);
|
doTestWithTryCatchFinallySurrounder(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedAsExpression.kt")
|
||||||
|
public void testUsedAsExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally/usedAsExpression.kt");
|
||||||
|
doTestWithTryCatchFinallySurrounder(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class TryCatchFinallyExpression extends AbstractSurroundWithTest {
|
||||||
|
public void testAllFilesPresentInTryCatchFinallyExpression() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asStatement.kt")
|
||||||
|
public void testAsStatement() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/asStatement.kt");
|
||||||
|
doTestWithTryCatchFinallyExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleExpression.kt")
|
||||||
|
public void testSingleExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt");
|
||||||
|
doTestWithTryCatchFinallyExpressionSurrounder(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/codeInsight/surroundWith/tryFinally")
|
@TestMetadata("idea/testData/codeInsight/surroundWith/tryFinally")
|
||||||
@@ -586,6 +700,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryFinally/singleExpression.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryFinally/singleExpression.kt");
|
||||||
doTestWithTryFinallySurrounder(fileName);
|
doTestWithTryFinallySurrounder(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedAsExpression.kt")
|
||||||
|
public void testUsedAsExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryFinally/usedAsExpression.kt");
|
||||||
|
doTestWithTryFinallySurrounder(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/codeInsight/surroundWith/functionLiteral")
|
@TestMetadata("idea/testData/codeInsight/surroundWith/functionLiteral")
|
||||||
|
|||||||
Reference in New Issue
Block a user