Minor changes after code review
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
fun foo(list: List<String>): String? {
|
||||
<spot>return list.firstOrNull { s -> s.length > 0 }</spot>
|
||||
<spot>return list.firstOrNull { it.length > 0 }</spot>
|
||||
}
|
||||
@@ -66,7 +66,7 @@ interface SequenceTransformationMatcher {
|
||||
}
|
||||
|
||||
class SequenceTransformationMatch(
|
||||
val transformations: Collection<SequenceTransformation>,
|
||||
val transformations: List<SequenceTransformation>,
|
||||
val newState: MatchingState
|
||||
) {
|
||||
init {
|
||||
@@ -82,5 +82,5 @@ interface ResultTransformationMatcher {
|
||||
|
||||
class ResultTransformationMatch(
|
||||
val resultTransformation: ResultTransformation,
|
||||
val sequenceTransformations: Collection<SequenceTransformation> = listOf()
|
||||
val sequenceTransformations: List<SequenceTransformation> = listOf()
|
||||
)
|
||||
|
||||
@@ -94,7 +94,7 @@ fun match(loop: KtForExpression): ResultTransformationMatch? {
|
||||
fun convertLoop(loop: KtForExpression, matchResult: ResultTransformationMatch): KtExpression {
|
||||
val commentSaver = CommentSaver(matchResult.resultTransformation.commentSavingRange)
|
||||
|
||||
var callChain = matchResult.generateCallChain(loop)
|
||||
val callChain = matchResult.generateCallChain(loop)
|
||||
|
||||
val result = matchResult.resultTransformation.convertLoop(callChain)
|
||||
|
||||
@@ -158,11 +158,11 @@ private fun ResultTransformationMatch.generateCallChain(loop: KtForExpression):
|
||||
var sequenceTransformations = sequenceTransformations
|
||||
val last = sequenceTransformations.lastOrNull()
|
||||
var lastFilter: FilterOrMap? = null
|
||||
var lastMap: FilterOrMap? = null //TODO
|
||||
val lastMap: FilterOrMap? = null //TODO
|
||||
if (last is FilterTransformation && resultTransformation.canIncludeFilter) {
|
||||
val condition = if (last.isInverse) last.condition.negate() else last.condition
|
||||
lastFilter = FilterOrMap(condition, last.inputVariable)
|
||||
sequenceTransformations = sequenceTransformations.take(sequenceTransformations.size - 1)
|
||||
sequenceTransformations = sequenceTransformations.dropLast(1)
|
||||
}
|
||||
|
||||
val lineBreak = if (sequenceTransformations.isNotEmpty()) "\n" else ""
|
||||
@@ -170,20 +170,18 @@ private fun ResultTransformationMatch.generateCallChain(loop: KtForExpression):
|
||||
var callChain = loop.loopRange!!
|
||||
|
||||
val psiFactory = KtPsiFactory(loop)
|
||||
fun chainedCallGenerator(): ChainedCallGenerator {
|
||||
return object : ChainedCallGenerator {
|
||||
override fun generate(pattern: String, vararg args: Any): KtExpression {
|
||||
val newPattern = "\$${args.size}$lineBreak.$pattern"
|
||||
return psiFactory.createExpressionByPattern(newPattern, *args, callChain)
|
||||
}
|
||||
val chainedCallGenerator = object : ChainedCallGenerator {
|
||||
override fun generate(pattern: String, vararg args: Any): KtExpression {
|
||||
val newPattern = "\$${args.size}$lineBreak.$pattern"
|
||||
return psiFactory.createExpressionByPattern(newPattern, *args, callChain)
|
||||
}
|
||||
}
|
||||
|
||||
for (transformation in sequenceTransformations) {
|
||||
callChain = transformation.generateCode(chainedCallGenerator())
|
||||
callChain = transformation.generateCode(chainedCallGenerator)
|
||||
}
|
||||
|
||||
callChain = resultTransformation.generateCode(chainedCallGenerator(), lastFilter, lastMap)
|
||||
callChain = resultTransformation.generateCode(chainedCallGenerator, lastFilter, lastMap)
|
||||
return callChain
|
||||
}
|
||||
|
||||
|
||||
+9
@@ -60,6 +60,15 @@ class FindAndAssignTransformation(
|
||||
return initialDeclaration
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
* val variable = ...
|
||||
* for (...) {
|
||||
* ...
|
||||
* variable = ...
|
||||
* break
|
||||
* }
|
||||
*/
|
||||
object Matcher : ResultTransformationMatcher {
|
||||
override fun match(state: MatchingState): ResultTransformationMatch? {
|
||||
//TODO: pass indexVariable as null if not used
|
||||
|
||||
+8
@@ -56,6 +56,14 @@ class FindAndReturnTransformation(
|
||||
return endReturn
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
* for (...) {
|
||||
* ...
|
||||
* return ...
|
||||
* }
|
||||
* return ...
|
||||
*/
|
||||
object Matcher : ResultTransformationMatcher {
|
||||
override fun match(state: MatchingState): ResultTransformationMatch? {
|
||||
//TODO: pass indexVariable as null if not used
|
||||
|
||||
+15
@@ -43,6 +43,21 @@ class FilterTransformation(
|
||||
}
|
||||
|
||||
//TODO: merge subsequent filters
|
||||
/**
|
||||
* Matches:
|
||||
* for (...) {
|
||||
* if (<condition>) {
|
||||
* ...
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* or
|
||||
*
|
||||
* for (...) {
|
||||
* if (<condition>) continue
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
object Matcher : SequenceTransformationMatcher {
|
||||
override fun match(state: MatchingState): SequenceTransformationMatch? {
|
||||
if (state.indexVariable != null) return null //TODO?
|
||||
|
||||
+9
@@ -42,6 +42,15 @@ class FlatMapTransformation(
|
||||
return chainedCallGenerator.generate("flatMap$0:'{}'", lambda)
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
* for (...) {
|
||||
* ...
|
||||
* for (...) {
|
||||
* ...
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
object Matcher : SequenceTransformationMatcher {
|
||||
override fun match(state: MatchingState): SequenceTransformationMatch? {
|
||||
if (state.indexVariable != null) return null
|
||||
|
||||
+7
@@ -38,6 +38,13 @@ class MapTransformation(
|
||||
return chainedCallGenerator.generate("map$0:'{}'", lambda)
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches:
|
||||
* for (...) {
|
||||
* val v = ...
|
||||
* ...
|
||||
* }
|
||||
*/
|
||||
object Matcher : SequenceTransformationMatcher {
|
||||
override fun match(state: MatchingState): SequenceTransformationMatch? {
|
||||
if (state.indexVariable != null) return null //TODO?
|
||||
|
||||
Reference in New Issue
Block a user