Expression can be parenthesized on replace with if (KT-22874, EA-91186)
#KT-22874 Fixed
This commit is contained in:
+11
-13
@@ -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)
|
||||
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test(some: Boolean) {
|
||||
<selection>some</selection>.equals(true)
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
fun test(some: Boolean) {
|
||||
(if (some) {
|
||||
<caret>
|
||||
} else {
|
||||
}).equals(true)
|
||||
}
|
||||
Generated
+6
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user