Cleanup branchedTransformations & update copyright
This commit is contained in:
+18
-25
@@ -1,17 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
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?) {
|
fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpression, editor: Editor?) {
|
||||||
val project = this.project
|
val project = this.project
|
||||||
val condition = condition
|
val occurrenceInConditional = when (val condition = condition) {
|
||||||
val occurrenceInConditional = when (condition) {
|
|
||||||
is KtBinaryExpression -> condition.left
|
is KtBinaryExpression -> condition.left
|
||||||
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}")
|
||||||
@@ -221,14 +209,22 @@ data class IfThenToSelectData(
|
|||||||
baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver(
|
baseClause is KtDotQualifiedExpression -> baseClause.replaceFirstReceiver(
|
||||||
factory, newReceiver!!, safeAccess = true
|
factory, newReceiver!!, safeAccess = true
|
||||||
)
|
)
|
||||||
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern("$0?.$1", newReceiver!!, baseClause).insertSafeCalls(
|
hasImplicitReceiverReplaceableBySafeCall() -> factory.createExpressionByPattern(
|
||||||
|
"$0?.$1",
|
||||||
|
newReceiver!!,
|
||||||
|
baseClause
|
||||||
|
).insertSafeCalls(
|
||||||
factory
|
factory
|
||||||
)
|
)
|
||||||
baseClause is KtCallExpression -> baseClause.replaceCallWithLet(newReceiver!!, factory)
|
baseClause is KtCallExpression -> baseClause.replaceCallWithLet(newReceiver!!, factory)
|
||||||
else -> error("Illegal state")
|
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)
|
baseClause is KtCallExpression -> baseClause.replaceCallWithLet(receiverExpression, factory)
|
||||||
else -> baseClause.insertSafeCalls(factory)
|
else -> baseClause.insertSafeCalls(factory)
|
||||||
}
|
}
|
||||||
@@ -308,15 +304,12 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData?
|
|||||||
internal fun KtExpression?.isClauseTransformableToLetOnly(receiver: KtExpression?) =
|
internal fun KtExpression?.isClauseTransformableToLetOnly(receiver: KtExpression?) =
|
||||||
this is KtCallExpression && (resolveToCall()?.getImplicitReceiverValue() == null || receiver !is KtThisExpression)
|
this is KtCallExpression && (resolveToCall()?.getImplicitReceiverValue() == null || receiver !is KtThisExpression)
|
||||||
|
|
||||||
internal fun KtIfExpression.shouldBeTransformed(): Boolean {
|
internal fun KtIfExpression.shouldBeTransformed(): Boolean = when (val condition = condition) {
|
||||||
val condition = condition
|
is KtBinaryExpression -> {
|
||||||
return when (condition) {
|
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
|
||||||
is KtBinaryExpression -> {
|
!baseClause.isClauseTransformableToLetOnly(condition.checkedExpression())
|
||||||
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
|
|
||||||
!baseClause.isClauseTransformableToLetOnly(condition.checkedExpression())
|
|
||||||
}
|
|
||||||
else -> false
|
|
||||||
}
|
}
|
||||||
|
else -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtExpression.checkedExpression() = when (this) {
|
private fun KtExpression.checkedExpression() = when (this) {
|
||||||
|
|||||||
+20
-37
@@ -1,17 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
||||||
@@ -38,8 +27,7 @@ fun KtWhenCondition.toExpression(subject: KtExpression?): KtExpression {
|
|||||||
is KtWhenConditionWithExpression -> {
|
is KtWhenConditionWithExpression -> {
|
||||||
if (subject != null) {
|
if (subject != null) {
|
||||||
factory.createExpressionByPattern("$0 == $1", subject, expression ?: "")
|
factory.createExpressionByPattern("$0 == $1", subject, expression ?: "")
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
expression!!
|
expression!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,7 +36,7 @@ fun KtWhenCondition.toExpression(subject: KtExpression?): KtExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
|
fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
|
||||||
if (subjectExpression != null) return null
|
if (subjectExpression != null) return null
|
||||||
|
|
||||||
var lastCandidate: KtExpression? = null
|
var lastCandidate: KtExpression? = null
|
||||||
@@ -73,28 +61,25 @@ fun KtWhenExpression.getSubjectToIntroduce(): KtExpression? {
|
|||||||
return lastCandidate
|
return lastCandidate
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? {
|
private fun KtExpression?.getWhenConditionSubjectCandidate(): KtExpression? = when (this) {
|
||||||
return when(this) {
|
is KtIsExpression -> leftHandSide
|
||||||
is KtIsExpression -> leftHandSide
|
|
||||||
|
|
||||||
is KtBinaryExpression -> {
|
is KtBinaryExpression -> {
|
||||||
val lhs = left
|
val lhs = left
|
||||||
val op = operationToken
|
when (operationToken) {
|
||||||
when (op) {
|
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
|
||||||
KtTokens.IN_KEYWORD, KtTokens.NOT_IN -> lhs
|
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: right
|
||||||
KtTokens.EQEQ -> lhs as? KtNameReferenceExpression ?: right
|
KtTokens.OROR -> {
|
||||||
KtTokens.OROR -> {
|
val leftCandidate = lhs.getWhenConditionSubjectCandidate()
|
||||||
val leftCandidate = lhs.getWhenConditionSubjectCandidate()
|
val rightCandidate = right.getWhenConditionSubjectCandidate()
|
||||||
val rightCandidate = right.getWhenConditionSubjectCandidate()
|
if (leftCandidate.matches(rightCandidate)) leftCandidate else null
|
||||||
if (leftCandidate.matches(rightCandidate)) leftCandidate else null
|
|
||||||
}
|
|
||||||
else -> null
|
|
||||||
}
|
}
|
||||||
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KtWhenExpression.introduceSubject(): KtWhenExpression? {
|
fun KtWhenExpression.introduceSubject(): KtWhenExpression? {
|
||||||
@@ -110,8 +95,7 @@ fun KtWhenExpression.introduceSubject(): KtWhenExpression? {
|
|||||||
|
|
||||||
if (entry.isElse) {
|
if (entry.isElse) {
|
||||||
appendFixedText("else")
|
appendFixedText("else")
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
for ((i, condition) in entry.conditions.withIndex()) {
|
for ((i, condition) in entry.conditions.withIndex()) {
|
||||||
if (i > 0) appendFixedText(",")
|
if (i > 0) appendFixedText(",")
|
||||||
|
|
||||||
@@ -146,8 +130,7 @@ private fun BuilderByPattern<KtExpression>.appendConditionWithSubjectRemoved(con
|
|||||||
is KtBinaryExpression -> {
|
is KtBinaryExpression -> {
|
||||||
val lhs = conditionExpression.left
|
val lhs = conditionExpression.left
|
||||||
val rhs = conditionExpression.right
|
val rhs = conditionExpression.right
|
||||||
val op = conditionExpression.operationToken
|
when (conditionExpression.operationToken) {
|
||||||
when (op) {
|
|
||||||
KtTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
|
KtTokens.IN_KEYWORD -> appendFixedText("in ").appendExpression(rhs)
|
||||||
KtTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
|
KtTokens.NOT_IN -> appendFixedText("!in ").appendExpression(rhs)
|
||||||
KtTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)
|
KtTokens.EQEQ -> appendExpression(if (subject.matches(lhs)) rhs else lhs)
|
||||||
|
|||||||
Reference in New Issue
Block a user