diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index fa30190ca31..cb2701119c0 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -672,12 +672,16 @@ fun main(args: Array) { testClass { model("codeInsight/surroundWith/if", testMethod = "doTestWithIfSurrounder") 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/parentheses", testMethod = "doTestWithParenthesesSurrounder") model("codeInsight/surroundWith/stringTemplate", testMethod = "doTestWithStringTemplateSurrounder") model("codeInsight/surroundWith/when", testMethod = "doTestWithWhenSurrounder") model("codeInsight/surroundWith/tryCatch", testMethod = "doTestWithTryCatchSurrounder") + model("codeInsight/surroundWith/tryCatchExpression", testMethod = "doTestWithTryCatchExpressionSurrounder") model("codeInsight/surroundWith/tryCatchFinally", testMethod = "doTestWithTryCatchFinallySurrounder") + model("codeInsight/surroundWith/tryCatchFinallyExpression", testMethod = "doTestWithTryCatchFinallyExpressionSurrounder") model("codeInsight/surroundWith/tryFinally", testMethod = "doTestWithTryFinallySurrounder") model("codeInsight/surroundWith/functionLiteral", testMethod = "doTestWithFunctionLiteralSurrounder") model("codeInsight/surroundWith/withIfExpression", testMethod = "doTestWithSurroundWithIfExpression") diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinControlFlowExpressionSurrounderBase.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinControlFlowExpressionSurrounderBase.kt new file mode 100644 index 00000000000..f4776ef3b65 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinControlFlowExpressionSurrounderBase.kt @@ -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? +} \ No newline at end of file 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 ffcd3c82186..4d75c498ce6 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 @@ -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"); * you may not use this file except in compliance with the License. @@ -33,7 +33,11 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor { new KotlinWhenSurrounder() , new KotlinRuntimeTypeCastSurrounder(), 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 diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurrounder.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurrounder.java index dd0eb5dd9ba..0c43e3fc4bb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurrounder.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinExpressionSurrounder.java @@ -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"); * 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.KtExpression; import org.jetbrains.kotlin.psi.KtQualifiedExpression; +import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; import org.jetbrains.kotlin.types.KotlinType; @@ -44,13 +45,24 @@ public abstract class KotlinExpressionSurrounder implements Surrounder { if (expression instanceof KtCallExpression && expression.getParent() instanceof KtQualifiedExpression) { return false; } - KotlinType type = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL).getType(expression); - if (type == null || isUnit(type)) { + BindingContext context = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL); + KotlinType type = context.getType(expression); + if (type == null || (isUnit(type) && isApplicableToStatements())) { return false; } + + boolean usedAsExpression = Boolean.TRUE.equals(context.get(BindingContext.USED_AS_EXPRESSION, expression)); + if (!isApplicableToStatements() && !usedAsExpression) { + return false; + } + return isApplicable(expression); } + protected boolean isApplicableToStatements() { + return true; + } + protected abstract boolean isApplicable(@NotNull KtExpression expression); @Nullable diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinIfElseExpressionSurrounder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinIfElseExpressionSurrounder.kt new file mode 100644 index 00000000000..e189393640f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinIfElseExpressionSurrounder.kt @@ -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) } + } + +} \ No newline at end of file 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 new file mode 100644 index 00000000000..cbf5e434508 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchExpressionSurrounder.kt @@ -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) {}" +} + 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 new file mode 100644 index 00000000000..9a07e2b889a --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryCatchFinallyExpressionSurrounder.kt @@ -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 {}" +} \ 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/KotlinTryExpressionSurrounderBase.kt new file mode 100644 index 00000000000..7cb5570f5a0 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinTryExpressionSurrounderBase.kt @@ -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) + } +} + diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java index a7ec193a693..0b9fabf9f77 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java @@ -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"); * 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 { + @Override + protected boolean isApplicableWhenUsedAsExpression() { + return false; + } + @Nullable @Override 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); + return getRange(editor, ifExpression); + } + + @NotNull + public static TextRange getRange(Editor editor, @NotNull KtIfExpression ifExpression) { KtExpression condition = ifExpression.getCondition(); assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText(); // Delete condition from created if diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinStatementsSurrounder.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinStatementsSurrounder.java index 5d2646c8ce9..a06119a6a89 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinStatementsSurrounder.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinStatementsSurrounder.java @@ -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"); * 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 org.jetbrains.annotations.NotNull; 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 { @Override 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 diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java index 754be5f5f23..d472aaf4cf3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTrySurrounderBase.java @@ -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"); * 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 { + @Override + protected boolean isApplicableWhenUsedAsExpression() { + return false; + } + @Nullable @Override 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 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(); assert parameter != null : "Catch parameter should exists for " + expression.getText(); KtElement typeReference = parameter.getTypeReference(); diff --git a/idea/testData/codeInsight/surroundWith/if/usedAsExpression.kt b/idea/testData/codeInsight/surroundWith/if/usedAsExpression.kt new file mode 100644 index 00000000000..a472b5c1cb5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/if/usedAsExpression.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + val a = if(true) { + "aaa" + } else { + "bbb" + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElse/usedAsExpression.kt b/idea/testData/codeInsight/surroundWith/ifElse/usedAsExpression.kt new file mode 100644 index 00000000000..a472b5c1cb5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElse/usedAsExpression.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + val a = if(true) { + "aaa" + } else { + "bbb" + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpression/asStatement.kt b/idea/testData/codeInsight/surroundWith/ifElseExpression/asStatement.kt new file mode 100644 index 00000000000..9bcf342a7aa --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpression/asStatement.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +fun foo() { + fun call() {} + + call() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt b/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt new file mode 100644 index 00000000000..9fa8d595c61 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt @@ -0,0 +1,3 @@ +fun foo() { + "aaa" + 1 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt.after b/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt.after new file mode 100644 index 00000000000..b6e2cdd82f5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpression/paranthesized.kt.after @@ -0,0 +1,3 @@ +fun foo() { + (if () "aaa" else) + 1 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt b/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt new file mode 100644 index 00000000000..9bb0d07acdc --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt.after new file mode 100644 index 00000000000..6b491270d48 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpression/singleExpression.kt.after @@ -0,0 +1,3 @@ +fun foo() { + val a = if () "aaa" else +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/asStatement.kt b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/asStatement.kt new file mode 100644 index 00000000000..9bcf342a7aa --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/asStatement.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +fun foo() { + fun call() {} + + call() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt new file mode 100644 index 00000000000..9bb0d07acdc --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt.after new file mode 100644 index 00000000000..d5afbff06cd --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/ifElseExpressionBraces/singleExpression.kt.after @@ -0,0 +1,6 @@ +fun foo() { + val a = if () { + "aaa" + } else { + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatch/usedAsExpression.kt b/idea/testData/codeInsight/surroundWith/tryCatch/usedAsExpression.kt new file mode 100644 index 00000000000..a472b5c1cb5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatch/usedAsExpression.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + val a = if(true) { + "aaa" + } else { + "bbb" + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchExpression/asStatement.kt b/idea/testData/codeInsight/surroundWith/tryCatchExpression/asStatement.kt new file mode 100644 index 00000000000..9bcf342a7aa --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchExpression/asStatement.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +fun foo() { + fun call() {} + + call() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt b/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt new file mode 100644 index 00000000000..9bb0d07acdc --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt.after new file mode 100644 index 00000000000..e9be0391acc --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchExpression/singleExpression.kt.after @@ -0,0 +1,6 @@ +fun foo() { + val a = try { + "aaa" + } catch (e: Exception) { + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchFinally/usedAsExpression.kt b/idea/testData/codeInsight/surroundWith/tryCatchFinally/usedAsExpression.kt new file mode 100644 index 00000000000..a472b5c1cb5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchFinally/usedAsExpression.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + val a = if(true) { + "aaa" + } else { + "bbb" + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/asStatement.kt b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/asStatement.kt new file mode 100644 index 00000000000..9bcf342a7aa --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/asStatement.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +fun foo() { + fun call() {} + + call() +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt new file mode 100644 index 00000000000..9bb0d07acdc --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt @@ -0,0 +1,3 @@ +fun foo() { + val a = "aaa" +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt.after b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt.after new file mode 100644 index 00000000000..1d9c2846ffe --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryCatchFinallyExpression/singleExpression.kt.after @@ -0,0 +1,7 @@ +fun foo() { + val a = try { + "aaa" + } catch (e: Exception) { + } finally { + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/tryFinally/usedAsExpression.kt b/idea/testData/codeInsight/surroundWith/tryFinally/usedAsExpression.kt new file mode 100644 index 00000000000..a472b5c1cb5 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/tryFinally/usedAsExpression.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + val a = if(true) { + "aaa" + } else { + "bbb" + } +} \ No newline at end of file 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 ac8f0e59c98..e07a53474c4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/AbstractSurroundWithTest.java @@ -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"); * 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()); } + 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 { doTest(path, new KotlinNotSurrounder()); } @@ -63,10 +71,19 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase doTest(path, new KotlinTryCatchSurrounder()); } + public void doTestWithTryCatchExpressionSurrounder(String path) throws Exception { + doTest(path, new KotlinTryCatchExpressionSurrounder()); + } + public void doTestWithTryCatchFinallySurrounder(String path) throws Exception { doTest(path, new KotlinTryCatchFinallySurrounder()); } + public void doTestWithTryCatchFinallyExpressionSurrounder(String path) throws Exception { + doTest(path, new KotlinTryCatchFinallyExpressionSurrounder()); + } + + public void doTestWithTryFinallySurrounder(String path) throws Exception { doTest(path, new KotlinTryFinallySurrounder()); } diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java index cbd7b1066d9..a1e7bc4927d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java @@ -68,6 +68,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { 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") public void testVariable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/if/variable.kt"); @@ -311,6 +317,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { 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") public void testVariable() throws Exception { 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") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -544,6 +604,33 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatch/singleExpression.kt"); 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") @@ -565,6 +652,33 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryCatchFinally/singleExpression.kt"); 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") @@ -586,6 +700,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/tryFinally/singleExpression.kt"); 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")