If to when: do not add label to outer loop if not necessary

So #KT-24767 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-06-04 18:37:59 +03:00
parent b4962a1880
commit 24008cdffb
4 changed files with 21 additions and 1 deletions
@@ -89,11 +89,14 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
}
}
var labelRequired = false
fun KtExpressionWithLabel.addLabelIfNecessary(): KtExpressionWithLabel {
if (this.getLabelName() != null) return this
if (this.getStrictParentOfType<KtLoopExpression>() != nearestLoopIfAny) return this
if (labelName != null) {
val jumpWithLabel = KtPsiFactory(project).createExpression("$text@$labelName") as KtExpressionWithLabel
labelRequired = true
return replaced(jumpWithLabel)
}
return this
@@ -187,7 +190,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
result.accept(loopJumpVisitor)
val labelName = loopJumpVisitor.labelName
if (loop != null && labelName != null && loop.parent !is KtLabeledExpression) {
if (loop != null && loopJumpVisitor.labelRequired && labelName != null && loop.parent !is KtLabeledExpression) {
val labeledLoopExpression = KtPsiFactory(result).createLabeledExpression(labelName)
labeledLoopExpression.baseExpression!!.replace(loop)
val replacedLabeledLoopExpression = loop.replace(labeledLoopExpression)