diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWithIfExpressionSurrounder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWithIfExpressionSurrounder.kt index dbb5634b74e..9d9541859d0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWithIfExpressionSurrounder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWithIfExpressionSurrounder.kt @@ -22,28 +22,26 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.conversion.copy.range -import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtIfExpression -import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.typeUtil.isBoolean import org.jetbrains.kotlin.utils.sure class KotlinWithIfExpressionSurrounder(val withElse: Boolean) : KotlinExpressionSurrounder() { override fun isApplicable(expression: KtExpression) = - super.isApplicable(expression) && (expression.analyze(BodyResolveMode.PARTIAL).getType(expression)?.isBoolean() ?: false) + super.isApplicable(expression) && (expression.analyze(BodyResolveMode.PARTIAL).getType(expression)?.isBoolean() ?: false) override fun surroundExpression(project: Project, editor: Editor, expression: KtExpression): TextRange? { val factory = KtPsiFactory(project) - val ifExpression = - expression.replace( - factory.createIf( - expression, - factory.createBlock("blockStubContentToBeRemovedLater"), - if (withElse) factory.createEmptyBody() else null - ) - ) as KtIfExpression + val replaceResult = expression.replace( + factory.createIf( + expression, + factory.createBlock("blockStubContentToBeRemovedLater"), + if (withElse) factory.createEmptyBody() else null + ) + ) as KtExpression + + val ifExpression = KtPsiUtil.deparenthesizeOnce(replaceResult) as KtIfExpression CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression) diff --git a/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt b/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt new file mode 100644 index 00000000000..11cd6cc4989 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt @@ -0,0 +1,3 @@ +fun test(some: Boolean) { + some.equals(true) +} \ No newline at end of file diff --git a/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt.after b/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt.after new file mode 100644 index 00000000000..cf62e06d780 --- /dev/null +++ b/idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt.after @@ -0,0 +1,6 @@ +fun test(some: Boolean) { + (if (some) { + + } else { + }).equals(true) +} \ No newline at end of file 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 4ac0053efb3..e893b5d19ae 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/surroundWith/SurroundWithTestGenerated.java @@ -759,6 +759,12 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest { doTestWithSurroundWithIfElseExpression(fileName); } + @TestMetadata("replaceWithParenthesized.kt") + public void testReplaceWithParenthesized() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/withIfElseExpression/replaceWithParenthesized.kt"); + doTestWithSurroundWithIfElseExpression(fileName); + } + @TestMetadata("variable.kt") public void testVariable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/surroundWith/withIfElseExpression/variable.kt");