Reformat ConvertTwoComparisonsToRangeCheckIntention
This commit is contained in:
committed by
Dmitry Gridin
parent
6c2eb3dd16
commit
54df0aad16
+15
-7
@@ -49,9 +49,13 @@ class ConvertTwoComparisonsToRangeCheckIntention : SelfTargetingOffsetIndependen
|
|||||||
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||||
val rangeData = generateRangeExpressionData(element) ?: return
|
val rangeData = generateRangeExpressionData(element) ?: return
|
||||||
val factory = KtPsiFactory(element)
|
val factory = KtPsiFactory(element)
|
||||||
element.replace(factory.createExpressionByPattern("$0 in $1..$2", rangeData.value,
|
element.replace(
|
||||||
|
factory.createExpressionByPattern(
|
||||||
|
"$0 in $1..$2", rangeData.value,
|
||||||
factory.createExpression(rangeData.min),
|
factory.createExpression(rangeData.min),
|
||||||
factory.createExpression(rangeData.max)))
|
factory.createExpression(rangeData.max)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateRangeExpressionData(condition: KtBinaryExpression): RangeExpressionData? {
|
private fun generateRangeExpressionData(condition: KtBinaryExpression): RangeExpressionData? {
|
||||||
@@ -89,17 +93,21 @@ class ConvertTwoComparisonsToRangeCheckIntention : SelfTargetingOffsetIndependen
|
|||||||
secondLess: KtExpression, secondGreater: KtExpression, secondStrict: Boolean
|
secondLess: KtExpression, secondGreater: KtExpression, secondStrict: Boolean
|
||||||
) = when {
|
) = when {
|
||||||
firstGreater !is KtConstantExpression && firstGreater.evaluatesTo(secondLess) ->
|
firstGreater !is KtConstantExpression && firstGreater.evaluatesTo(secondLess) ->
|
||||||
generateRangeExpressionData(firstGreater,
|
generateRangeExpressionData(
|
||||||
|
firstGreater,
|
||||||
min = firstLess,
|
min = firstLess,
|
||||||
max = secondGreater,
|
max = secondGreater,
|
||||||
incrementMinByOne = firstStrict,
|
incrementMinByOne = firstStrict,
|
||||||
decrementMaxByOne = secondStrict)
|
decrementMaxByOne = secondStrict
|
||||||
|
)
|
||||||
firstLess !is KtConstantExpression && firstLess.evaluatesTo(secondGreater) ->
|
firstLess !is KtConstantExpression && firstLess.evaluatesTo(secondGreater) ->
|
||||||
generateRangeExpressionData(firstLess,
|
generateRangeExpressionData(
|
||||||
|
firstLess,
|
||||||
min = secondLess,
|
min = secondLess,
|
||||||
max = firstGreater,
|
max = firstGreater,
|
||||||
incrementMinByOne = secondStrict,
|
incrementMinByOne = secondStrict,
|
||||||
decrementMaxByOne = firstStrict)
|
decrementMaxByOne = firstStrict
|
||||||
|
)
|
||||||
else ->
|
else ->
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
@@ -171,7 +179,7 @@ class ConvertTwoComparisonsToRangeCheckIntention : SelfTargetingOffsetIndependen
|
|||||||
return (intConst as? Number)?.toDouble()?.toString()
|
return (intConst as? Number)?.toDouble()?.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KotlinType.isComparable() = DescriptorUtils.isSubtypeOfClass(this, this.builtIns.getComparable())
|
private fun KotlinType.isComparable() = DescriptorUtils.isSubtypeOfClass(this, this.builtIns.comparable)
|
||||||
|
|
||||||
private fun KotlinType.isFloatingPoint(): Boolean {
|
private fun KotlinType.isFloatingPoint(): Boolean {
|
||||||
return KotlinBuiltIns.isFloat(this) || KotlinBuiltIns.isDouble(this)
|
return KotlinBuiltIns.isFloat(this) || KotlinBuiltIns.isDouble(this)
|
||||||
|
|||||||
Reference in New Issue
Block a user