Expression can be parenthesized on replace with if (KT-22874, EA-91186)

#KT-22874 Fixed
This commit is contained in:
Nikolay Krasko
2018-02-15 17:33:39 +03:00
parent 67183ad3b0
commit 866129984e
4 changed files with 26 additions and 13 deletions
@@ -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)
@@ -0,0 +1,3 @@
fun test(some: Boolean) {
<selection>some</selection>.equals(true)
}
@@ -0,0 +1,6 @@
fun test(some: Boolean) {
(if (some) {
<caret>
} else {
}).equals(true)
}
@@ -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");