From 50bd766992e8220b2d55fbda88759040bd17207e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 5 Apr 2016 20:41:04 +0300 Subject: [PATCH] Minor changes after code review --- .../after.kt.template | 2 +- .../intentions/loopToCallChain/interfaces.kt | 4 ++-- .../loopToCallChain/matchAndConvert.kt | 20 +++++++++---------- .../result/FindAndAssignTransformation.kt | 9 +++++++++ .../result/FindAndReturnTransformation.kt | 8 ++++++++ .../sequence/FilterTransformation.kt | 15 ++++++++++++++ .../sequence/FlatMapTransformation.kt | 9 +++++++++ .../sequence/MapTransformation.kt | 7 +++++++ 8 files changed, 60 insertions(+), 14 deletions(-) diff --git a/idea/resources/intentionDescriptions/LoopToCallChainIntention/after.kt.template b/idea/resources/intentionDescriptions/LoopToCallChainIntention/after.kt.template index 01134af2f4e..f2941d61a08 100644 --- a/idea/resources/intentionDescriptions/LoopToCallChainIntention/after.kt.template +++ b/idea/resources/intentionDescriptions/LoopToCallChainIntention/after.kt.template @@ -1,3 +1,3 @@ fun foo(list: List): String? { - return list.firstOrNull { s -> s.length > 0 } + return list.firstOrNull { it.length > 0 } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/interfaces.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/interfaces.kt index 3e3778eddde..b580ca82806 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/interfaces.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/interfaces.kt @@ -66,7 +66,7 @@ interface SequenceTransformationMatcher { } class SequenceTransformationMatch( - val transformations: Collection, + val transformations: List, val newState: MatchingState ) { init { @@ -82,5 +82,5 @@ interface ResultTransformationMatcher { class ResultTransformationMatch( val resultTransformation: ResultTransformation, - val sequenceTransformations: Collection = listOf() + val sequenceTransformations: List = listOf() ) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt index 5eb9736c94f..3f9d7aba417 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt @@ -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 } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndAssignTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndAssignTransformation.kt index 549163aa555..8aee61ffb25 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndAssignTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndAssignTransformation.kt @@ -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 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndReturnTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndReturnTransformation.kt index ba266693293..c108a6a85eb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndReturnTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/FindAndReturnTransformation.kt @@ -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 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt index 721d7e8f238..11eca31fc6f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt @@ -43,6 +43,21 @@ class FilterTransformation( } //TODO: merge subsequent filters + /** + * Matches: + * for (...) { + * if () { + * ... + * } + * } + * + * or + * + * for (...) { + * if () continue + * ... + * } + */ object Matcher : SequenceTransformationMatcher { override fun match(state: MatchingState): SequenceTransformationMatch? { if (state.indexVariable != null) return null //TODO? diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FlatMapTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FlatMapTransformation.kt index 7607be7a064..80064894c31 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FlatMapTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FlatMapTransformation.kt @@ -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 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt index 6e73f5cc1ca..5d03353b58f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt @@ -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?