Reformat: if-then utilities
This commit is contained in:
+41
-25
@@ -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)
|
||||||
}
|
}
|
||||||
@@ -103,7 +113,7 @@ fun KtExpression.convertToIfNullExpression(conditionLhs: KtExpression, thenClaus
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun KtExpression.convertToIfStatement(condition: KtExpression, thenClause: KtExpression, elseClause: KtExpression? = null): KtIfExpression =
|
fun KtExpression.convertToIfStatement(condition: KtExpression, thenClause: KtExpression, elseClause: KtExpression? = null): KtIfExpression =
|
||||||
replaced(KtPsiFactory(this).createIf(condition, thenClause, elseClause))
|
replaced(KtPsiFactory(this).createIf(condition, thenClause, elseClause))
|
||||||
|
|
||||||
fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpression, editor: Editor?) {
|
fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpression, editor: Editor?) {
|
||||||
val project = this.project
|
val project = this.project
|
||||||
@@ -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(
|
||||||
editor,
|
project,
|
||||||
occurrenceInConditional,
|
editor,
|
||||||
false,
|
occurrenceInConditional,
|
||||||
listOf(occurrenceInConditional, occurrenceInThenClause), null)
|
false,
|
||||||
|
listOf(occurrenceInConditional, occurrenceInThenClause), null
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) {
|
fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) {
|
||||||
@@ -151,37 +163,41 @@ fun KtExpression.isStable(context: BindingContext = this.analyze()): Boolean {
|
|||||||
if (this is KtConstantExpression || this is KtThisExpression) return true
|
if (this is KtConstantExpression || this is KtThisExpression) return true
|
||||||
val descriptor = BindingContextUtils.extractVariableDescriptorFromReference(context, this)
|
val descriptor = BindingContextUtils.extractVariableDescriptorFromReference(context, this)
|
||||||
return descriptor is VariableDescriptor &&
|
return descriptor is VariableDescriptor &&
|
||||||
DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor))
|
DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
data class IfThenToSelectData(
|
data class IfThenToSelectData(
|
||||||
val context: BindingContext,
|
val context: BindingContext,
|
||||||
val condition: KtOperationExpression,
|
val condition: KtOperationExpression,
|
||||||
val receiverExpression: KtExpression,
|
val receiverExpression: KtExpression,
|
||||||
val baseClause: KtExpression?,
|
val baseClause: KtExpression?,
|
||||||
val negatedClause: KtExpression?
|
val negatedClause: KtExpression?
|
||||||
) {
|
) {
|
||||||
internal fun baseClauseEvaluatesToReceiver() =
|
internal fun baseClauseEvaluatesToReceiver() =
|
||||||
baseClause?.evaluatesTo(receiverExpression) == true
|
baseClause?.evaluatesTo(receiverExpression) == true
|
||||||
|
|
||||||
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(
|
||||||
it.leftHandSide,
|
"$0 as? $1",
|
||||||
it.typeReference!!)
|
it.leftHandSide,
|
||||||
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user