max()/minI() supported
This commit is contained in:
@@ -45,6 +45,7 @@ object MatcherRegistrar {
|
|||||||
FindTransformationMatcher,
|
FindTransformationMatcher,
|
||||||
AddToCollectionTransformation.Matcher,
|
AddToCollectionTransformation.Matcher,
|
||||||
CountTransformation.Matcher,
|
CountTransformation.Matcher,
|
||||||
|
MaxOrMinTransformation.Matcher,
|
||||||
IntroduceIndexMatcher,
|
IntroduceIndexMatcher,
|
||||||
FilterTransformation.Matcher,
|
FilterTransformation.Matcher,
|
||||||
MapTransformation.Matcher,
|
MapTransformation.Matcher,
|
||||||
|
|||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.blockExpressionsOrSingle
|
||||||
|
|
||||||
|
class MaxOrMinTransformation(
|
||||||
|
loop: KtForExpression,
|
||||||
|
private val inputVariable: KtCallableDeclaration,
|
||||||
|
initialization: VariableInitialization,
|
||||||
|
private val isMax: Boolean
|
||||||
|
) : AssignToVariableResultTransformation(loop, initialization) {
|
||||||
|
|
||||||
|
override val presentation: String
|
||||||
|
get() = if (isMax) "max()" else "min()"
|
||||||
|
|
||||||
|
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
|
val call = chainedCallGenerator.generate(presentation)
|
||||||
|
return KtPsiFactory(call).createExpressionByPattern("$0\n ?: $1", call, initialization.initializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches:
|
||||||
|
* val variable = <initial>
|
||||||
|
* for (...) {
|
||||||
|
* ...
|
||||||
|
* if (variable > <input variable>) { // or '<' or operands swapped
|
||||||
|
* variable = <input variable>
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
object Matcher : TransformationMatcher {
|
||||||
|
override val indexVariableAllowed: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
override fun match(state: MatchingState): TransformationMatch.Result? {
|
||||||
|
val ifExpression = state.statements.singleOrNull() as? KtIfExpression ?: return null
|
||||||
|
if (ifExpression.`else` != null) return null
|
||||||
|
|
||||||
|
val condition = ifExpression.condition as? KtBinaryExpression ?: return null
|
||||||
|
val comparison = condition.operationToken
|
||||||
|
if (comparison !in setOf(KtTokens.GT, KtTokens.LT)) return null
|
||||||
|
val left = condition.left as? KtNameReferenceExpression ?: return null
|
||||||
|
val right = condition.right as? KtNameReferenceExpression ?: return null
|
||||||
|
val otherHand = if (left.isVariableReference(state.inputVariable)) {
|
||||||
|
right
|
||||||
|
}
|
||||||
|
else if (right.isVariableReference(state.inputVariable)) {
|
||||||
|
left
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
val variableInitialization = (otherHand.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||||
|
?: return null)
|
||||||
|
|
||||||
|
val then = ifExpression.then ?: return null
|
||||||
|
val statement = then.blockExpressionsOrSingle().singleOrNull() as? KtBinaryExpression ?: return null
|
||||||
|
if (statement.operationToken != KtTokens.EQ) return null
|
||||||
|
if (!statement.left.isVariableReference(variableInitialization.variable)) return null
|
||||||
|
if (!statement.right.isVariableReference(state.inputVariable)) return null
|
||||||
|
|
||||||
|
val isMax = (comparison == KtTokens.GT) xor (otherHand == left)
|
||||||
|
val transformation = MaxOrMinTransformation(state.outerLoop, state.inputVariable, variableInitialization, isMax)
|
||||||
|
return TransformationMatch.Result(transformation)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
var max_width = 0.0f
|
||||||
|
<caret>for (i in 0..lineCount - 1) {
|
||||||
|
val width = getLineWidth(i)
|
||||||
|
if (width > max_width) {
|
||||||
|
max_width = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
val <caret>max_width = (0..lineCount - 1)
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.max()
|
||||||
|
?: 0.0f
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
val <caret>max_width = (0..lineCount - 1)
|
||||||
|
.asSequence()
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.max()
|
||||||
|
?: 0.0f
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
var max_width = 0.0f
|
||||||
|
<caret>for (i in 0..lineCount - 1) {
|
||||||
|
val width = getLineWidth(i)
|
||||||
|
if (max_width < width) {
|
||||||
|
max_width = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
val <caret>max_width = (0..lineCount - 1)
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.max()
|
||||||
|
?: 0.0f
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.max()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.max()'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
val <caret>max_width = (0..lineCount - 1)
|
||||||
|
.asSequence()
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.max()
|
||||||
|
?: 0.0f
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
var min_width = Double.MAX_VALUE
|
||||||
|
<caret>for (i in 0..lineCount - 1) {
|
||||||
|
val width = getLineWidth(i)
|
||||||
|
if (width < min_width) {
|
||||||
|
min_width = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
val <caret>min_width = (0..lineCount - 1)
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.min()
|
||||||
|
?: Double.MAX_VALUE
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
val <caret>min_width = (0..lineCount - 1)
|
||||||
|
.asSequence()
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.min()
|
||||||
|
?: Double.MAX_VALUE
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
var min_width = Double.MAX_VALUE
|
||||||
|
<caret>for (i in 0..lineCount - 1) {
|
||||||
|
val width = getLineWidth(i)
|
||||||
|
if (min_width > width) {
|
||||||
|
min_width = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
val <caret>min_width = (0..lineCount - 1)
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.min()
|
||||||
|
?: Double.MAX_VALUE
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.min()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.min()'"
|
||||||
|
fun getMinLineWidth(lineCount: Int): Double {
|
||||||
|
val <caret>min_width = (0..lineCount - 1)
|
||||||
|
.asSequence()
|
||||||
|
.map { getLineWidth(it) }
|
||||||
|
.min()
|
||||||
|
?: Double.MAX_VALUE
|
||||||
|
return min_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Double = TODO()
|
||||||
@@ -947,6 +947,30 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("max1.kt")
|
||||||
|
public void testMax1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/max1.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("max2.kt")
|
||||||
|
public void testMax2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/max2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("min1.kt")
|
||||||
|
public void testMin1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/min1.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("min2.kt")
|
||||||
|
public void testMin2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/min2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("moveInitializationsCloserToLoop.kt")
|
@TestMetadata("moveInitializationsCloserToLoop.kt")
|
||||||
public void testMoveInitializationsCloserToLoop() throws Exception {
|
public void testMoveInitializationsCloserToLoop() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/moveInitializationsCloserToLoop.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/moveInitializationsCloserToLoop.kt");
|
||||||
|
|||||||
@@ -8107,6 +8107,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("max1.kt")
|
||||||
|
public void testMax1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/max1.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("max2.kt")
|
||||||
|
public void testMax2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/max2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("min1.kt")
|
||||||
|
public void testMin1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/min1.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("min2.kt")
|
||||||
|
public void testMin2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/min2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("moveInitializationsCloserToLoop.kt")
|
@TestMetadata("moveInitializationsCloserToLoop.kt")
|
||||||
public void testMoveInitializationsCloserToLoop() throws Exception {
|
public void testMoveInitializationsCloserToLoop() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/moveInitializationsCloserToLoop.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/moveInitializationsCloserToLoop.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user