From dc7f81a9041a2419bdf58b85d10de1f0f4962708 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 13 May 2015 16:33:28 +0300 Subject: [PATCH] Code corrections after code review --- .../kotlin/psi/psiUtil/jetPsiUtil.kt | 8 ++--- .../expressions/OperatorConventions.java | 35 ++++++++++--------- .../jetbrains/kotlin/idea/intentions/Utils.kt | 2 +- .../ReplaceCallWithBinaryOperatorIntention.kt | 19 ++++------ .../ReplaceCallWithUnaryOperatorIntention.kt | 16 ++++----- .../ReplaceContainsIntention.kt | 4 +-- .../ReplaceGetIntention.kt | 4 +-- .../ReplaceInvokeIntention.kt | 4 +-- .../intrinsic/operation/CompareToBOIF.kt | 2 +- .../intrinsic/operation/EqualsBOIF.kt | 2 +- .../intrinsic/operation/LongCompareToBOIF.kt | 2 +- .../operation/binaryOperationIntrinsics.kt | 2 +- 12 files changed, 45 insertions(+), 55 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt index 66069e20a19..b4a6b8d4e40 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/jetPsiUtil.kt @@ -136,12 +136,8 @@ public fun T.copied(): T = copy() as T public fun JetElement.blockExpressionsOrSingle(): Sequence = if (this is JetBlockExpression) getStatements().asSequence() else sequenceOf(this) -public fun JetExpression.lastBlockStatementOrThis(): JetExpression { - return if (this is JetBlockExpression) - this.getStatements().lastIsInstanceOrNull() ?: this - else - this -} +public fun JetExpression.lastBlockStatementOrThis(): JetExpression + = (this as? JetBlockExpression)?.getStatements()?.lastIsInstanceOrNull() ?: this public fun JetBlockExpression.appendElement(element: JetElement): JetElement { val rBrace = getRBrace() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/OperatorConventions.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/OperatorConventions.java index 4d50ad64151..1883652cff7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/OperatorConventions.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/OperatorConventions.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.lexer.JetSingleValueToken; import org.jetbrains.kotlin.lexer.JetToken; import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.name.Name; @@ -48,7 +49,7 @@ public class OperatorConventions { DOUBLE, FLOAT, LONG, INT, SHORT, BYTE, CHAR ); - public static final ImmutableBiMap UNARY_OPERATION_NAMES = ImmutableBiMap.builder() + public static final ImmutableBiMap UNARY_OPERATION_NAMES = ImmutableBiMap.builder() .put(JetTokens.PLUSPLUS, Name.identifier("inc")) .put(JetTokens.MINUSMINUS, Name.identifier("dec")) .put(JetTokens.PLUS, Name.identifier("plus")) @@ -56,7 +57,7 @@ public class OperatorConventions { .put(JetTokens.EXCL, Name.identifier("not")) .build(); - public static final ImmutableBiMap BINARY_OPERATION_NAMES = ImmutableBiMap.builder() + public static final ImmutableBiMap BINARY_OPERATION_NAMES = ImmutableBiMap.builder() .put(JetTokens.MUL, Name.identifier("times")) .put(JetTokens.PLUS, Name.identifier("plus")) .put(JetTokens.MINUS, Name.identifier("minus")) @@ -65,25 +66,25 @@ public class OperatorConventions { .put(JetTokens.RANGE, Name.identifier("rangeTo")) .build(); - public static final ImmutableSet NOT_OVERLOADABLE = - ImmutableSet.of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ); + public static final ImmutableSet NOT_OVERLOADABLE = + ImmutableSet.of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ); - public static final ImmutableSet INCREMENT_OPERATIONS = - ImmutableSet.of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS); + public static final ImmutableSet INCREMENT_OPERATIONS = + ImmutableSet.of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS); - public static final ImmutableSet COMPARISON_OPERATIONS = - ImmutableSet.of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ); + public static final ImmutableSet COMPARISON_OPERATIONS = + ImmutableSet.of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ); - public static final ImmutableSet EQUALS_OPERATIONS = - ImmutableSet.of(JetTokens.EQEQ, JetTokens.EXCLEQ); + public static final ImmutableSet EQUALS_OPERATIONS = + ImmutableSet.of(JetTokens.EQEQ, JetTokens.EXCLEQ); - public static final ImmutableSet IDENTITY_EQUALS_OPERATIONS = - ImmutableSet.of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ); + public static final ImmutableSet IDENTITY_EQUALS_OPERATIONS = + ImmutableSet.of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ); - public static final ImmutableSet IN_OPERATIONS = - ImmutableSet.of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN); + public static final ImmutableSet IN_OPERATIONS = + ImmutableSet.of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN); - public static final ImmutableBiMap ASSIGNMENT_OPERATIONS = ImmutableBiMap.builder() + public static final ImmutableBiMap ASSIGNMENT_OPERATIONS = ImmutableBiMap.builder() .put(JetTokens.MULTEQ, Name.identifier("timesAssign")) .put(JetTokens.DIVEQ, Name.identifier("divAssign")) .put(JetTokens.PERCEQ, Name.identifier("modAssign")) @@ -91,7 +92,7 @@ public class OperatorConventions { .put(JetTokens.MINUSEQ, Name.identifier("minusAssign")) .build(); - public static final ImmutableBiMap ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.builder() + public static final ImmutableBiMap ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.builder() .put(JetTokens.MULTEQ, JetTokens.MUL) .put(JetTokens.DIVEQ, JetTokens.DIV) .put(JetTokens.PERCEQ, JetTokens.PERC) @@ -99,7 +100,7 @@ public class OperatorConventions { .put(JetTokens.MINUSEQ, JetTokens.MINUS) .build(); - public static final ImmutableBiMap BOOLEAN_OPERATIONS = ImmutableBiMap.builder() + public static final ImmutableBiMap BOOLEAN_OPERATIONS = ImmutableBiMap.builder() .put(JetTokens.ANDAND, Name.identifier("and")) .put(JetTokens.OROR, Name.identifier("or")) .build(); diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index 98a1da0d82d..8a6fd258fd1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -106,7 +106,7 @@ fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression { val JetQualifiedExpression.callExpression: JetCallExpression? get() = getSelectorExpression() as? JetCallExpression -val JetQualifiedExpression.functionName: String? +val JetQualifiedExpression.calleeName: String? get() = (callExpression?.getCalleeExpression() as? JetSimpleNameExpression)?.getText() fun JetQualifiedExpression.toResolvedCall(): ResolvedCall? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt index ad63f4f8539..cf39e87fedb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithBinaryOperatorIntention.kt @@ -20,16 +20,18 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.functionName +import org.jetbrains.kotlin.idea.intentions.calleeName import org.jetbrains.kotlin.idea.intentions.toResolvedCall +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch +import org.jetbrains.kotlin.types.expressions.OperatorConventions public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace call with binary operator") { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { - val operation = operation(element.functionName) ?: return null + val operation = operation(element.calleeName) ?: return null val resolvedCall = element.toResolvedCall() ?: return null if (!resolvedCall.getStatus().isSuccess()) return null if (resolvedCall.getCall().getTypeArgumentList() != null) return null @@ -40,7 +42,7 @@ public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeInten } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { - val operation = operation(element.functionName)!! + val operation = operation(element.calleeName)!! val argument = element.callExpression!!.getValueArguments().single().getArgumentExpression()!! val receiver = element.getReceiverExpression() @@ -48,14 +50,7 @@ public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeInten } private fun operation(functionName: String?): String? { - return when (functionName) { - "plus" -> "+" - "minus" -> "-" - "div" -> "/" - "times" -> "*" - "mod" -> "%" - "rangeTo" -> ".." - else -> null - } + if (functionName == null) return null + return OperatorConventions.BINARY_OPERATION_NAMES.inverse()[Name.identifier(functionName)]?.getValue() } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt index 3a5d8921587..36d7a2a8767 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceCallWithUnaryOperatorIntention.kt @@ -20,14 +20,16 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.functionName +import org.jetbrains.kotlin.idea.intentions.calleeName +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern +import org.jetbrains.kotlin.types.expressions.OperatorConventions public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace call with unary operator") { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { - val operation = operation(element.functionName) ?: return null + val operation = operation(element.calleeName) ?: return null val call = element.callExpression ?: return null if (call.getTypeArgumentList() != null) return null if (!call.getValueArguments().isEmpty()) return null @@ -36,17 +38,13 @@ public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingRangeIntent } override fun applyTo(element: JetDotQualifiedExpression, editor: Editor) { - val operation = operation(element.functionName)!! + val operation = operation(element.calleeName)!! val receiver = element.getReceiverExpression() element.replace(JetPsiFactory(element).createExpressionByPattern("$0$1", operation, receiver)) } private fun operation(functionName: String?) : String? { - return when (functionName) { - "plus" -> "+" - "minus" -> "-" - "not" -> "!" - else -> null - } + if (functionName == null) return null + return OperatorConventions.UNARY_OPERATION_NAMES.inverse()[Name.identifier(functionName)]?.getValue() } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt index 46f84b4bf8a..26071723314 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt @@ -20,7 +20,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.functionName +import org.jetbrains.kotlin.idea.intentions.calleeName import org.jetbrains.kotlin.idea.intentions.toResolvedCall import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* @@ -30,7 +30,7 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions public class ReplaceContainsIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'contains' call with 'in' operator") { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { - if (element.functionName != OperatorConventions.CONTAINS.asString()) return null + if (element.calleeName != OperatorConventions.CONTAINS.asString()) return null val resolvedCall = element.toResolvedCall() ?: return null if (!resolvedCall.getStatus().isSuccess()) return null val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt index 7b13f8e5ff2..fe79622eff9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceGetIntention.kt @@ -21,7 +21,7 @@ import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.functionName +import org.jetbrains.kotlin.idea.intentions.calleeName import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.buildExpression @@ -30,7 +30,7 @@ public class ExplicitGetInspection : IntentionBasedInspection(javaClass(), "Replace 'get' call with index operator") { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { - if (element.functionName != "get") return null + if (element.calleeName != "get") return null val call = element.callExpression ?: return null if (call.getTypeArgumentList() != null) return null val arguments = call.getValueArguments() diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt index 3e087787c73..c1818dfa567 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceInvokeIntention.kt @@ -20,13 +20,13 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.callExpression -import org.jetbrains.kotlin.idea.intentions.functionName +import org.jetbrains.kotlin.idea.intentions.calleeName import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.types.expressions.OperatorConventions public class ReplaceInvokeIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'invoke' with direct call") { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { - if (element.functionName != OperatorConventions.INVOKE.asString()) return null + if (element.calleeName != OperatorConventions.INVOKE.asString()) return null return element.callExpression!!.getCalleeExpression()!!.getTextRange() } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/CompareToBOIF.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/CompareToBOIF.kt index 989e26eb977..561a890c01f 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/CompareToBOIF.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/CompareToBOIF.kt @@ -64,7 +64,7 @@ object CompareToBOIF : BinaryOperationIntrinsicFactory { } } - override public fun getSupportTokens(): ImmutableSet = OperatorConventions.COMPARISON_OPERATIONS + override public fun getSupportTokens() = OperatorConventions.COMPARISON_OPERATIONS override public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? { if (descriptor.isDynamic()) return CompareToIntrinsic diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/EqualsBOIF.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/EqualsBOIF.kt index 0f0acee0cdd..f7022702e0d 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/EqualsBOIF.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/EqualsBOIF.kt @@ -81,7 +81,7 @@ object EqualsBOIF : BinaryOperationIntrinsicFactory { } } - override public fun getSupportTokens(): ImmutableSet = OperatorConventions.EQUALS_OPERATIONS + override public fun getSupportTokens() = OperatorConventions.EQUALS_OPERATIONS override public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? { if (JsDescriptorUtils.isBuiltin(descriptor) || TopLevelFIF.EQUALS_IN_ANY.apply(descriptor)) { diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/LongCompareToBOIF.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/LongCompareToBOIF.kt index d9b33ff9002..fc486ab5e51 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/LongCompareToBOIF.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/LongCompareToBOIF.kt @@ -69,7 +69,7 @@ public object LongCompareToBOIF : BinaryOperationIntrinsicFactory { private val LONG_COMPARE_TO_CHAR = CompareToBinaryIntrinsic( ID, { longFromInt(charToInt(it)) }) private val LONG_COMPARE_TO_LONG = CompareToBinaryIntrinsic( ID, ID ) - override public fun getSupportTokens(): ImmutableSet = OperatorConventions.COMPARISON_OPERATIONS + override public fun getSupportTokens() = OperatorConventions.COMPARISON_OPERATIONS override public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? { if (JsDescriptorUtils.isBuiltin(descriptor)) { diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/binaryOperationIntrinsics.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/binaryOperationIntrinsics.kt index 3818012e6cb..c2fa42d5cba 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/binaryOperationIntrinsics.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/operation/binaryOperationIntrinsics.kt @@ -73,7 +73,7 @@ public class BinaryOperationIntrinsics { trait BinaryOperationIntrinsicFactory { - public fun getSupportTokens(): ImmutableSet + public fun getSupportTokens(): ImmutableSet public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? }