Reformat: if-then utilities

This commit is contained in:
Mikhail Glukhikh
2018-01-29 17:49:34 +03:00
parent 3b8f5bce41
commit f28bec0db1
@@ -74,7 +74,8 @@ fun KtExpression?.isTrivialStatementBody(): Boolean = when (this?.unwrapBlockOrP
fun KtExpression?.isNullExpression(): Boolean = this?.unwrapBlockOrParenthesis()?.node?.elementType == KtNodeTypes.NULL fun KtExpression?.isNullExpression(): Boolean = this?.unwrapBlockOrParenthesis()?.node?.elementType == KtNodeTypes.NULL
fun KtExpression?.isNullExpressionOrEmptyBlock(): Boolean = this.isNullExpression() || this is KtBlockExpression && this.statements.isEmpty() fun KtExpression?.isNullExpressionOrEmptyBlock(): Boolean =
this.isNullExpression() || this is KtBlockExpression && this.statements.isEmpty()
fun KtThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean { fun KtThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean {
val thrownExpression = this.thrownExpression as? KtCallExpression ?: return false val thrownExpression = this.thrownExpression as? KtCallExpression ?: return false
@@ -85,14 +86,23 @@ fun KtThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean {
val declDescriptor = descriptor?.containingDeclaration ?: return false val declDescriptor = descriptor?.containingDeclaration ?: return false
val exceptionName = DescriptorUtils.getFqName(declDescriptor).asString() val exceptionName = DescriptorUtils.getFqName(declDescriptor).asString()
return exceptionName in constant { setOf("kotlin.KotlinNullPointerException", "kotlin.NullPointerException", "java.lang.NullPointerException") } return exceptionName in constant {
&& thrownExpression.valueArguments.isEmpty() setOf(
"kotlin.KotlinNullPointerException",
"kotlin.NullPointerException",
"java.lang.NullPointerException"
)
} && thrownExpression.valueArguments.isEmpty()
} }
fun KtExpression.evaluatesTo(other: KtExpression): Boolean = fun KtExpression.evaluatesTo(other: KtExpression): Boolean =
this.unwrapBlockOrParenthesis().text == other.text this.unwrapBlockOrParenthesis().text == other.text
fun KtExpression.convertToIfNotNullExpression(conditionLhs: KtExpression, thenClause: KtExpression, elseClause: KtExpression?): KtIfExpression { fun KtExpression.convertToIfNotNullExpression(
conditionLhs: KtExpression,
thenClause: KtExpression,
elseClause: KtExpression?
): KtIfExpression {
val condition = KtPsiFactory(this).createExpressionByPattern("$0 != null", conditionLhs) val condition = KtPsiFactory(this).createExpressionByPattern("$0 != null", conditionLhs)
return this.convertToIfStatement(condition, thenClause, elseClause) return this.convertToIfStatement(condition, thenClause, elseClause)
} }
@@ -113,11 +123,13 @@ fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpressi
is KtIsExpression -> condition.leftHandSide is KtIsExpression -> condition.leftHandSide
else -> throw AssertionError("Only binary / is expressions are supported here: ${condition?.text}") else -> throw AssertionError("Only binary / is expressions are supported here: ${condition?.text}")
}!! }!!
KotlinIntroduceVariableHandler.doRefactoring(project, KotlinIntroduceVariableHandler.doRefactoring(
project,
editor, editor,
occurrenceInConditional, occurrenceInConditional,
false, false,
listOf(occurrenceInConditional, occurrenceInThenClause), null) listOf(occurrenceInConditional, occurrenceInThenClause), null
)
} }
fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) { fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) {
@@ -167,21 +179,25 @@ data class IfThenToSelectData(
internal fun replacedBaseClause(factory: KtPsiFactory): KtExpression { internal fun replacedBaseClause(factory: KtPsiFactory): KtExpression {
baseClause ?: error("Base clause must be not-null here") baseClause ?: error("Base clause must be not-null here")
val newReceiver = (condition as? KtIsExpression)?.let { val newReceiver = (condition as? KtIsExpression)?.let {
factory.createExpressionByPattern("$0 as? $1", factory.createExpressionByPattern(
"$0 as? $1",
it.leftHandSide, it.leftHandSide,
it.typeReference!!) it.typeReference!!
)
} }
return if (baseClauseEvaluatesToReceiver()) { return if (baseClauseEvaluatesToReceiver()) {
if (condition is KtIsExpression) newReceiver!! else baseClause if (condition is KtIsExpression) newReceiver!! else baseClause
} } else {
else {
when { when {
condition is KtIsExpression -> { condition is KtIsExpression -> {
when { when {
baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver( baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver(
factory, newReceiver!!, safeAccess = true) factory, newReceiver!!, safeAccess = true
hasImplicitReceiver() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(factory) )
hasImplicitReceiver() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(
factory
)
else -> error("Illegal state") else -> error("Illegal state")
} }
} }