takeWhile supported
This commit is contained in:
+43
-5
@@ -67,6 +67,12 @@ class FilterTransformation(
|
||||
* if (<condition>) continue
|
||||
* ...
|
||||
* }
|
||||
* or
|
||||
*
|
||||
* for (...) {
|
||||
* if (<condition>) break
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
object Matcher : SequenceTransformationMatcher {
|
||||
override fun match(state: MatchingState): SequenceTransformationMatch? {
|
||||
@@ -81,11 +87,24 @@ class FilterTransformation(
|
||||
return SequenceTransformationMatch(transformation, newState)
|
||||
}
|
||||
else {
|
||||
val continueExpression = then.blockExpressionsOrSingle().singleOrNull() as? KtContinueExpression ?: return null
|
||||
if (continueExpression.targetLoop() != state.innerLoop) return null
|
||||
val transformation = createFilterTransformation(state.outerLoop, state.inputVariable, state.indexVariable, condition, isInverse = true)
|
||||
val newState = state.copy(statements = state.statements.drop(1))
|
||||
return SequenceTransformationMatch(transformation, newState)
|
||||
val statement = then.blockExpressionsOrSingle().singleOrNull() ?: return null
|
||||
when (statement) {
|
||||
is KtContinueExpression -> {
|
||||
if (statement.targetLoop() != state.innerLoop) return null
|
||||
val transformation = createFilterTransformation(state.outerLoop, state.inputVariable, state.indexVariable, condition, isInverse = true)
|
||||
val newState = state.copy(statements = state.statements.drop(1))
|
||||
return SequenceTransformationMatch(transformation, newState)
|
||||
}
|
||||
|
||||
is KtBreakExpression -> {
|
||||
if (statement.targetLoop() != state.outerLoop) return null
|
||||
val transformation = TakeWhileTransformation(state.outerLoop, state.inputVariable, condition.negate())
|
||||
val newState = state.copy(statements = state.statements.drop(1))
|
||||
return SequenceTransformationMatch(transformation, newState)
|
||||
}
|
||||
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,3 +196,22 @@ class FilterIndexedTransformation(
|
||||
}
|
||||
}
|
||||
|
||||
class TakeWhileTransformation(
|
||||
override val loop: KtForExpression,
|
||||
val inputVariable: KtCallableDeclaration,
|
||||
val condition: KtExpression
|
||||
) : SequenceTransformation {
|
||||
|
||||
//TODO: merge multiple
|
||||
|
||||
override val affectsIndex: Boolean
|
||||
get() = false
|
||||
|
||||
override val presentation: String
|
||||
get() = "takeWhile{}"
|
||||
|
||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||
val lambda = generateLambda(inputVariable, condition)
|
||||
return chainedCallGenerator.generate("takeWhile$0:'{}'", lambda)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with '+= takeWhile{}'"
|
||||
fun foo(list: List<String>, target: MutableCollection<String>) {
|
||||
<caret>for (s in list) {
|
||||
if (s.isEmpty()) break
|
||||
target.add(s)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with '+= takeWhile{}'"
|
||||
fun foo(list: List<String>, target: MutableCollection<String>) {
|
||||
<caret>target += list.takeWhile { !it.isEmpty() }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with '+= flatMap{}.takeWhile{}'"
|
||||
fun foo(list: List<String>, target: MutableCollection<Int>) {
|
||||
Outer@
|
||||
<caret>for (s in list) {
|
||||
for (i in s.indices) {
|
||||
if (i > 1000) break@Outer
|
||||
target.add(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with '+= flatMap{}.takeWhile{}'"
|
||||
fun foo(list: List<String>, target: MutableCollection<Int>) {
|
||||
<caret>target += list
|
||||
.flatMap { it.indices }
|
||||
.takeWhile { it <= 1000 }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(list: List<String>, target: MutableCollection<Int>) {
|
||||
<caret>for (s in list) {
|
||||
for (i in s.indices) {
|
||||
if (i > 1000) break
|
||||
target.add(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user