From f28bec0db13588209eec65db8f1c17715bd148fe Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 29 Jan 2018 17:49:34 +0300 Subject: [PATCH] Reformat: if-then utilities --- .../branchedTransformations/IfThenUtils.kt | 66 ++++++++++++------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 28c02623769..a17c647f9b3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -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") } }