Skip to content

Commit

Permalink
removed usage of KtElement.replaceWithBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Sep 22, 2023
1 parent 58afc2c commit af46f8e
Showing 1 changed file with 2 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, "ELSE",
(elseNode?.treeParent?.prevSibling { it.elementType == ELSE_KEYWORD } ?: node).startOffset, node) {
elseNode?.run {
(psi as KtElement).replaceWithBlock(indent)
replaceWithBlock(indent)
}
?: run {
// `else` can have empty body e.g. when there is a semicolon after: `else ;`
Expand All @@ -141,7 +141,7 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
.lines()
.last()
.count { it == ' ' }
loopBody?.run {
loopBodyNode?.run {
replaceWithBlock(indent)
}
?: run {
Expand Down Expand Up @@ -174,13 +174,6 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}
}

private fun KtElement.replaceWithBlock(indent: Int) {
val ktBlockNode = KtBlockExpression(
"{\n${" ".repeat(indent + INDENT_STEP)}$text\n${" ".repeat(indent)}}"
)
this.astReplace(ktBlockNode)
}

private fun ASTNode.replaceWithBlock(indent: Int) {
val blockNode = ASTFactory.composite(BLOCK)
this.treeParent.addChild(blockNode, this)
Expand Down

0 comments on commit af46f8e

Please sign in to comment.