Code corrections after code review

This commit is contained in:
Valentin Kipyatkov
2015-05-13 16:33:28 +03:00
parent 1ad5ea4f7e
commit dc7f81a904
12 changed files with 45 additions and 55 deletions
@@ -136,12 +136,8 @@ public fun <T: PsiElement> T.copied(): T = copy() as T
public fun JetElement.blockExpressionsOrSingle(): Sequence<JetElement> =
if (this is JetBlockExpression) getStatements().asSequence() else sequenceOf(this)
public fun JetExpression.lastBlockStatementOrThis(): JetExpression {
return if (this is JetBlockExpression)
this.getStatements().lastIsInstanceOrNull<JetExpression>() ?: this
else
this
}
public fun JetExpression.lastBlockStatementOrThis(): JetExpression
= (this as? JetBlockExpression)?.getStatements()?.lastIsInstanceOrNull<JetExpression>() ?: this
public fun JetBlockExpression.appendElement(element: JetElement): JetElement {
val rBrace = getRBrace()
@@ -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<JetToken, Name> UNARY_OPERATION_NAMES = ImmutableBiMap.<JetToken, Name>builder()
public static final ImmutableBiMap<JetSingleValueToken, Name> UNARY_OPERATION_NAMES = ImmutableBiMap.<JetSingleValueToken, Name>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<JetToken, Name> BINARY_OPERATION_NAMES = ImmutableBiMap.<JetToken, Name>builder()
public static final ImmutableBiMap<JetSingleValueToken, Name> BINARY_OPERATION_NAMES = ImmutableBiMap.<JetSingleValueToken, Name>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<JetToken> NOT_OVERLOADABLE =
ImmutableSet.<JetToken>of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
public static final ImmutableSet<JetSingleValueToken> NOT_OVERLOADABLE =
ImmutableSet.of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
public static final ImmutableSet<JetToken> INCREMENT_OPERATIONS =
ImmutableSet.<JetToken>of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);
public static final ImmutableSet<JetSingleValueToken> INCREMENT_OPERATIONS =
ImmutableSet.of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);
public static final ImmutableSet<JetToken> COMPARISON_OPERATIONS =
ImmutableSet.<JetToken>of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ);
public static final ImmutableSet<JetSingleValueToken> COMPARISON_OPERATIONS =
ImmutableSet.of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ);
public static final ImmutableSet<JetToken> EQUALS_OPERATIONS =
ImmutableSet.<JetToken>of(JetTokens.EQEQ, JetTokens.EXCLEQ);
public static final ImmutableSet<JetSingleValueToken> EQUALS_OPERATIONS =
ImmutableSet.of(JetTokens.EQEQ, JetTokens.EXCLEQ);
public static final ImmutableSet<JetToken> IDENTITY_EQUALS_OPERATIONS =
ImmutableSet.<JetToken>of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
public static final ImmutableSet<JetSingleValueToken> IDENTITY_EQUALS_OPERATIONS =
ImmutableSet.of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
public static final ImmutableSet<JetToken> IN_OPERATIONS =
ImmutableSet.<JetToken>of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN);
public static final ImmutableSet<JetSingleValueToken> IN_OPERATIONS =
ImmutableSet.<JetSingleValueToken>of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN);
public static final ImmutableBiMap<JetToken, Name> ASSIGNMENT_OPERATIONS = ImmutableBiMap.<JetToken, Name>builder()
public static final ImmutableBiMap<JetSingleValueToken, Name> ASSIGNMENT_OPERATIONS = ImmutableBiMap.<JetSingleValueToken, Name>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<JetToken, JetToken> ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.<JetToken, JetToken>builder()
public static final ImmutableBiMap<JetSingleValueToken, JetSingleValueToken> ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.<JetSingleValueToken, JetSingleValueToken>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<JetToken, Name> BOOLEAN_OPERATIONS = ImmutableBiMap.<JetToken, Name>builder()
public static final ImmutableBiMap<JetSingleValueToken, Name> BOOLEAN_OPERATIONS = ImmutableBiMap.<JetSingleValueToken, Name>builder()
.put(JetTokens.ANDAND, Name.identifier("and"))
.put(JetTokens.OROR, Name.identifier("or"))
.build();
@@ -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<out CallableDescriptor>? {
@@ -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<JetDotQualifiedExpression>(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()
}
}
@@ -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<JetDotQualifiedExpression>(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()
}
}
@@ -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<JetDotQualifiedExpression>(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
@@ -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<JetDotQualifiedExp
public class ReplaceGetIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(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()
@@ -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<JetDotQualifiedExpression>(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()
}
@@ -64,7 +64,7 @@ object CompareToBOIF : BinaryOperationIntrinsicFactory {
}
}
override public fun getSupportTokens(): ImmutableSet<JetToken> = OperatorConventions.COMPARISON_OPERATIONS
override public fun getSupportTokens() = OperatorConventions.COMPARISON_OPERATIONS
override public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? {
if (descriptor.isDynamic()) return CompareToIntrinsic
@@ -81,7 +81,7 @@ object EqualsBOIF : BinaryOperationIntrinsicFactory {
}
}
override public fun getSupportTokens(): ImmutableSet<JetToken> = 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)) {
@@ -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<JetToken> = OperatorConventions.COMPARISON_OPERATIONS
override public fun getSupportTokens() = OperatorConventions.COMPARISON_OPERATIONS
override public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic? {
if (JsDescriptorUtils.isBuiltin(descriptor)) {
@@ -73,7 +73,7 @@ public class BinaryOperationIntrinsics {
trait BinaryOperationIntrinsicFactory {
public fun getSupportTokens(): ImmutableSet<JetToken>
public fun getSupportTokens(): ImmutableSet<out JetToken>
public fun getIntrinsic(descriptor: FunctionDescriptor): BinaryOperationIntrinsic?
}