Cleanup branchedTransformations & update copyright

This commit is contained in:
Dmitry Gridin
2019-06-03 20:04:27 +07:00
parent 74c177fd26
commit b4949a1683
2 changed files with 38 additions and 62 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
@@ -131,8 +120,7 @@ fun KtExpression.convertToIfStatement(condition: KtExpression, thenClause: KtExp
fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpression, editor: Editor?) {
val project = this.project
val condition = condition
val occurrenceInConditional = when (condition) {
val occurrenceInConditional = when (val condition = condition) {
is KtBinaryExpression -> condition.left
is KtIsExpression -> condition.leftHandSide
else -> throw AssertionError("Only binary / is expressions are supported here: ${condition?.text}")
@@ -221,14 +209,22 @@ data class IfThenToSelectData(
baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver(
factory, newReceiver!!, safeAccess = true
)
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern(
"$0?.$1",
newReceiver!!,
baseClause
).insertSafeCalls(
factory
)
baseClause is KtCallExpression -> baseClause.replaceCallWithLet(newReceiver!!, factory)
else -> error("Illegal state")
}
}
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern("$0?.$1", receiverExpression, baseClause).insertSafeCalls(factory)
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern(
"$0?.$1",
receiverExpression,
baseClause
).insertSafeCalls(factory)
baseClause is KtCallExpression -> baseClause.replaceCallWithLet(receiverExpression, factory)
else -> baseClause.insertSafeCalls(factory)
}
@@ -308,15 +304,12 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData?
internal fun KtExpression?.isClauseTransformableToLetOnly(receiver: KtExpression?) =
this is KtCallExpression && (resolveToCall()?.getImplicitReceiverValue() == null || receiver !is KtThisExpression)
internal fun KtIfExpression.shouldBeTransformed(): Boolean {
val condition = condition
return when (condition) {
is KtBinaryExpression -> {
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
!baseClause.isClauseTransformableToLetOnly(condition.checkedExpression())
}
else -> false
internal fun KtIfExpression.shouldBeTransformed(): Boolean = when (val condition = condition) {
is KtBinaryExpression -> {
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
!baseClause.isClauseTransformableToLetOnly(condition.checkedExpression())
}
else -> false
}
private fun KtExpression.checkedExpression() = when (this) {
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
@@ -38,8 +27,7 @@ fun KtWhenCondition.toExpression(subject: KtExpression?): KtExpression {
is KtWhenConditionWithExpression -> {
if (subject != null) {
factory.createExpressionByPattern("$0 == $1", subject, expression ?: "")
}
else {
} else {
expression!!
}
}
@@ -48,7 +36,7 @@ fun KtWhenCondition.toExpression(subject: KtExpression?): KtExpression {
}
}
fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
if (subjectExpression != null) return null
var lastCandidate: KtExpression? = null
@@ -73,28 +61,25 @@ fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
return lastCandidate
}
private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? {
return when(this) {
is KtIsExpression -> leftHandSide
private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? = when (this) {
is KtIsExpression -> leftHandSide
is KtBinaryExpression -> {
val lhs = left
val op = operationToken
when (op) {
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: right
KtTokens.OROR -> {
val leftCandidate = lhs.getWhenConditionSubjectCandidate()
val rightCandidate = right.getWhenConditionSubjectCandidate()
if (leftCandidate.matches(rightCandidate)) leftCandidate else null
}
else -> null
is KtBinaryExpression -> {
val lhs = left
when (operationToken) {
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: right
KtTokens.OROR -> {
val leftCandidate = lhs.getWhenConditionSubjectCandidate()
val rightCandidate = right.getWhenConditionSubjectCandidate()
if (leftCandidate.matches(rightCandidate)) leftCandidate else null
}
else -> null
}
else -> null
}
else -> null
}
fun KtWhenExpression.introduceSubject(): KtWhenExpression? {
@@ -110,8 +95,7 @@ fun KtWhenExpression.introduceSubject(): KtWhenExpression? {
if (entry.isElse) {
appendFixedText("else")
}
else {
} else {
for ((i, condition) in entry.conditions.withIndex()) {
if (i > 0) appendFixedText(",")
@@ -146,8 +130,7 @@ private fun BuilderByPattern<KtExpression>.appendConditionWithSubjectRemoved(con
is KtBinaryExpression -> {
val lhs = conditionExpression.left
val rhs = conditionExpression.right
val op = conditionExpression.operationToken
when (op) {
when (conditionExpression.operationToken) {
KtTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
KtTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
KtTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)