Skip to content

Commit

Permalink
Added indent handling for the case when condition or loop inside scop…
Browse files Browse the repository at this point in the history
…e function

### What's done:
* added findIndentBeforeNode() function
* reworked test for loops
* added test for conditions
  • Loading branch information
DrAlexD committed Sep 22, 2023
1 parent 194d47f commit 7756747
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.BLOCK
import org.jetbrains.kotlin.KtNodeTypes.CALL_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.IF
import org.jetbrains.kotlin.KtNodeTypes.LAMBDA_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.SAFE_ACCESS_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.WHEN
Expand Down Expand Up @@ -66,12 +67,7 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
val thenNode = ifPsi.then?.node
val elseKeyword = ifPsi.elseKeyword
val elseNode = ifPsi.`else`?.node
val indent = node
.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
val indent = node.findIndentBeforeNode()

if (node.isSingleLineIfElse()) {
return
Expand Down Expand Up @@ -132,11 +128,8 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
if (loopBodyNode == null || loopBodyNode.elementType != BLOCK) {
NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, node.elementType.toString(), node.startOffset, node) {
// fixme proper way to calculate indent? or get step size (instead of hardcoded 4)
val indent = node.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
val indent = node.findIndentBeforeNode()

loopBody?.run {
replaceWithBlock(indent)
}
Expand All @@ -152,6 +145,20 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}
}

private fun ASTNode.findIndentBeforeNode(): Int {
val indentNode = if (treeParent?.treeParent?.treeParent?.elementType == LAMBDA_EXPRESSION) {
treeParent.prevSibling { it.elementType == WHITE_SPACE }
} else {
prevSibling { it.elementType == WHITE_SPACE }
}

return indentNode!!

Check warning

Code scanning / detekt

Unsafe calls on nullable types detected. These calls will throw a NullPointerException in case the nullable value is null. Warning

Calling !! on a nullable type will throw a NullPointerException at runtime in case the value is null. It should be avoided.
.text
.lines()
.last()
.count { it == ' ' }
}

@Suppress("UnsafeCallOnNullableType")
private fun checkWhenBranches(node: ASTNode) {
(node.psi as KtWhenExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class BracesRuleFixTest : FixTestBase("test/paragraph3/braces", ::BracesInCondit
fixAndCompare("LoopsBracesExpected.kt", "LoopsBracesTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to if-else statements inside scope functions`() {
fixAndCompare("IfElseBracesInsideScopeFunctionsExpected.kt", "IfElseBracesInsideScopeFunctionsTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to loops inside scope functions`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) {
foo()
} else {
bar()
}
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else {
bar()
}
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) {
foo()
} else {
bar()
}
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) {
foo()
} else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) {
foo()
} else {
bar()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) foo()
else bar()
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else bar()
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) foo()
else bar()
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) foo()
else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) foo()
else bar()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@ package test.paragraph3.braces
fun foo1() {
str.apply {
for (i in 1..100) {
println(i)
}
println(i)
}
}
}

fun foo2() {
str.let {
for (i in 1..100) {
println(i)
}
str.let { while (x > 0) {
println(i)
}
}
}

fun foo3() {
str.run {
for (i in 1..100) {
println(i)
}
do {
println(i)
}
while (x > 0)
}
}

fun foo4() {
str.with {
for (i in 1..100) {
println(i)
}
str.with { do {
println(i)
}
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) {
println(i)
}
while (x > 0)
{
println(i)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ fun foo1() {
}

fun foo2() {
str.let {
for (i in 1..100) println(i)
str.let { while (x > 0) println(i)
}
}

fun foo3() {
str.run {
for (i in 1..100) println(i)
do println(i)
while (x > 0)
}
}

fun foo4() {
str.with {
for (i in 1..100) println(i)
str.with { do println(i)
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) println(i)
for (i in 1..100) {
while (x > 0)
println(i)
}
}
}

0 comments on commit 7756747

Please sign in to comment.