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?.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 {
|
||||
val thrownExpression = this.thrownExpression as? KtCallExpression ?: return false
|
||||
@@ -85,14 +86,23 @@ fun KtThrowExpression.throwsNullPointerExceptionWithNoArguments(): Boolean {
|
||||
val declDescriptor = descriptor?.containingDeclaration ?: return false
|
||||
|
||||
val exceptionName = DescriptorUtils.getFqName(declDescriptor).asString()
|
||||
return exceptionName in constant { setOf("kotlin.KotlinNullPointerException", "kotlin.NullPointerException", "java.lang.NullPointerException") }
|
||||
&& thrownExpression.valueArguments.isEmpty()
|
||||
return exceptionName in constant {
|
||||
setOf(
|
||||
"kotlin.KotlinNullPointerException",
|
||||
"kotlin.NullPointerException",
|
||||
"java.lang.NullPointerException"
|
||||
)
|
||||
} && thrownExpression.valueArguments.isEmpty()
|
||||
}
|
||||
|
||||
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)
|
||||
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 =
|
||||
replaced(KtPsiFactory(this).createIf(condition, thenClause, elseClause))
|
||||
replaced(KtPsiFactory(this).createIf(condition, thenClause, elseClause))
|
||||
|
||||
fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpression, editor: Editor?) {
|
||||
val project = this.project
|
||||
@@ -113,11 +123,13 @@ fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpressi
|
||||
is KtIsExpression -> condition.leftHandSide
|
||||
else -> throw AssertionError("Only binary / is expressions are supported here: ${condition?.text}")
|
||||
}!!
|
||||
KotlinIntroduceVariableHandler.doRefactoring(project,
|
||||
editor,
|
||||
occurrenceInConditional,
|
||||
false,
|
||||
listOf(occurrenceInConditional, occurrenceInThenClause), null)
|
||||
KotlinIntroduceVariableHandler.doRefactoring(
|
||||
project,
|
||||
editor,
|
||||
occurrenceInConditional,
|
||||
false,
|
||||
listOf(occurrenceInConditional, occurrenceInThenClause), null
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
val descriptor = BindingContextUtils.extractVariableDescriptorFromReference(context, this)
|
||||
return descriptor is VariableDescriptor &&
|
||||
DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor))
|
||||
DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor))
|
||||
}
|
||||
|
||||
data class IfThenToSelectData(
|
||||
val context: BindingContext,
|
||||
val condition: KtOperationExpression,
|
||||
val receiverExpression: KtExpression,
|
||||
val baseClause: KtExpression?,
|
||||
val negatedClause: KtExpression?
|
||||
val context: BindingContext,
|
||||
val condition: KtOperationExpression,
|
||||
val receiverExpression: KtExpression,
|
||||
val baseClause: KtExpression?,
|
||||
val negatedClause: KtExpression?
|
||||
) {
|
||||
internal fun baseClauseEvaluatesToReceiver() =
|
||||
baseClause?.evaluatesTo(receiverExpression) == true
|
||||
baseClause?.evaluatesTo(receiverExpression) == true
|
||||
|
||||
internal fun replacedBaseClause(factory: KtPsiFactory): KtExpression {
|
||||
baseClause ?: error("Base clause must be not-null here")
|
||||
val newReceiver = (condition as? KtIsExpression)?.let {
|
||||
factory.createExpressionByPattern("$0 as? $1",
|
||||
it.leftHandSide,
|
||||
it.typeReference!!)
|
||||
factory.createExpressionByPattern(
|
||||
"$0 as? $1",
|
||||
it.leftHandSide,
|
||||
it.typeReference!!
|
||||
)
|
||||
}
|
||||
|
||||
return if (baseClauseEvaluatesToReceiver()) {
|
||||
if (condition is KtIsExpression) newReceiver!! else baseClause
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
when {
|
||||
condition is KtIsExpression -> {
|
||||
when {
|
||||
baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver(
|
||||
factory, newReceiver!!, safeAccess = true)
|
||||
hasImplicitReceiver() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(factory)
|
||||
factory, newReceiver!!, safeAccess = true
|
||||
)
|
||||
hasImplicitReceiver() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(
|
||||
factory
|
||||
)
|
||||
else -> error("Illegal state")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user