Reformat InvertIfConditionIntention
This commit is contained in:
committed by
Mikhail Glukhikh
parent
20b2277951
commit
dce5cb3ce9
@@ -47,8 +47,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
|
|
||||||
val newCondition = element.condition!!.negate()
|
val newCondition = element.condition!!.negate()
|
||||||
|
|
||||||
val newIf = handleSpecialCases(element, newCondition)
|
val newIf = handleSpecialCases(element, newCondition) ?: handleStandardCase(element, newCondition)
|
||||||
?: handleStandardCase(element, newCondition)
|
|
||||||
|
|
||||||
val commentRestoreRange = if (rBrace != null)
|
val commentRestoreRange = if (rBrace != null)
|
||||||
PsiChildRange(newIf, rBrace)
|
PsiChildRange(newIf, rBrace)
|
||||||
@@ -105,9 +104,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
val block = ifExpression.parent as? KtBlockExpression
|
val block = ifExpression.parent as? KtBlockExpression
|
||||||
if (block != null) {
|
if (block != null) {
|
||||||
val rBrace = block.rBrace
|
val rBrace = block.rBrace
|
||||||
val afterIfInBlock = ifExpression.siblings(withItself = false)
|
val afterIfInBlock = ifExpression.siblings(withItself = false).takeWhile { it != rBrace }.toList()
|
||||||
.takeWhile { it != rBrace }
|
|
||||||
.toList()
|
|
||||||
val lastStatementInBlock = afterIfInBlock.lastIsInstanceOrNull<KtExpression>()
|
val lastStatementInBlock = afterIfInBlock.lastIsInstanceOrNull<KtExpression>()
|
||||||
if (lastStatementInBlock != null) {
|
if (lastStatementInBlock != null) {
|
||||||
val exitStatementAfterIf = if (lastStatementInBlock.isExitStatement())
|
val exitStatementAfterIf = if (lastStatementInBlock.isExitStatement())
|
||||||
@@ -121,8 +118,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
//TODO: no block if single?
|
//TODO: no block if single?
|
||||||
val newThenRange = if (isEmptyReturn(lastThenStatement) && isEmptyReturn(lastStatementInBlock)) {
|
val newThenRange = if (isEmptyReturn(lastThenStatement) && isEmptyReturn(lastStatementInBlock)) {
|
||||||
PsiChildRange(first, lastStatementInBlock.prevSibling).trimWhiteSpaces()
|
PsiChildRange(first, lastStatementInBlock.prevSibling).trimWhiteSpaces()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
PsiChildRange(first, last).trimWhiteSpaces()
|
PsiChildRange(first, last).trimWhiteSpaces()
|
||||||
}
|
}
|
||||||
val newIf = factory.createExpressionByPattern("if ($0) { $1 }", newCondition, newThenRange) as KtIfExpression
|
val newIf = factory.createExpressionByPattern("if ($0) { $1 }", newCondition, newThenRange) as KtIfExpression
|
||||||
@@ -162,7 +158,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun isEmptyReturn(statement: KtExpression) =
|
private fun isEmptyReturn(statement: KtExpression) =
|
||||||
statement is KtReturnExpression && statement.returnedExpression == null && statement.labeledExpression == null
|
statement is KtReturnExpression && statement.returnedExpression == null && statement.labeledExpression == null
|
||||||
|
|
||||||
private fun copyThenBranchAfter(ifExpression: KtIfExpression): KtIfExpression {
|
private fun copyThenBranchAfter(ifExpression: KtIfExpression): KtIfExpression {
|
||||||
val factory = KtPsiFactory(ifExpression)
|
val factory = KtPsiFactory(ifExpression)
|
||||||
@@ -184,8 +180,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
parent.addRangeAfter(range.first, range.last, ifExpression)
|
parent.addRangeAfter(range.first, range.last, ifExpression)
|
||||||
parent.addAfter(factory.createNewLine(), ifExpression)
|
parent.addAfter(factory.createNewLine(), ifExpression)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
parent.addAfter(thenBranch, ifExpression)
|
parent.addAfter(thenBranch, ifExpression)
|
||||||
parent.addAfter(factory.createNewLine(), ifExpression)
|
parent.addAfter(factory.createNewLine(), ifExpression)
|
||||||
}
|
}
|
||||||
@@ -198,8 +193,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
val lastStatement = parent.statements.last()
|
val lastStatement = parent.statements.last()
|
||||||
if (expression == lastStatement) {
|
if (expression == lastStatement) {
|
||||||
return exitStatementExecutedAfter(parent)
|
return exitStatementExecutedAfter(parent)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (lastStatement.isExitStatement() && expression.siblings(withItself = false).firstIsInstance<KtExpression>() == lastStatement) {
|
if (lastStatement.isExitStatement() && expression.siblings(withItself = false).firstIsInstance<KtExpression>() == lastStatement) {
|
||||||
return lastStatement
|
return lastStatement
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user