Skip to content

Commit 9a157f4

Browse files
committed
Fix indent calculation when a line contains only expressions and whitespaces.
1 parent 05b19db commit 9a157f4

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
kotlin.code.style=official
22

3-
VERSION_NAME=1.8.1
3+
VERSION_NAME=1.8.2
44

55
GROUP=com.bennyhuo.kotlin
66

@@ -25,4 +25,4 @@ KOTLIN_PLUGIN_ID=com.bennyhuo.kotlin.trimindent
2525

2626
SONATYPE_HOST=S01
2727
SONATYPE_AUTOMATIC_RELEASE=true
28-
RELEASE_SIGNING_ENABLED=true
28+
RELEASE_SIGNING_ENABLED=true

trimindent-compiler/src/main/java/com/bennyhuo/kotlin/trimindent/compiler/TrimIndentIrGenerator.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ class TrimIndentIrGenerator : IrGenerationExtension {
3737
acc += if (acc.isEmpty()) {
3838
element.values
3939
} else {
40+
// prefix${expression}postfix
41+
val prefix = acc.last()
42+
if (prefix.isBlank()) {
43+
// append whatever a non-blank character as a placeholder of the expression.
44+
acc[acc.lastIndex] = "$prefix!"
45+
}
46+
4047
// the first value is not belong to a new line.
48+
// It follows the previous expression in the same line.
4149
element.values.subList(1, element.values.size)
4250
}
4351
}
@@ -86,4 +94,4 @@ class TrimIndentIrGenerator : IrGenerationExtension {
8694
}
8795
})
8896
}
89-
}
97+
}

trimindent-compiler/src/test/java/com/bennyhuo/kotlin/trimindent/compiler/TrimIndentTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class TrimIndentTest {
2323
testBase("reIndent.kt")
2424
}
2525

26+
@Test
27+
fun blankLine() {
28+
testBase("blankLine.kt")
29+
}
30+
2631
private fun testBase(fileName: String) {
2732
val loader = FileBasedModuleInfoLoader("testData/$fileName")
2833
val sourceModuleInfos = loader.loadSourceModuleInfos()
@@ -39,4 +44,4 @@ class TrimIndentTest {
3944
)
4045
}
4146

42-
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SOURCE
2+
// FILE: Main.kt [MainKt#main]
3+
val s = """
4+
5+
hello
6+
7+
""".trimIndent()
8+
9+
val s1 = """
10+
$s
11+
""".trimIndent()
12+
13+
14+
val s2 = """
15+
hello
16+
$s
17+
world
18+
""".trimIndent()
19+
fun main() {
20+
println(s)
21+
println(s1)
22+
println(s2)
23+
}
24+
// EXPECT
25+
// FILE: MainKt.main.stdout
26+
27+
hello
28+
29+
30+
hello
31+
32+
hello
33+
34+
hello
35+
36+
world
37+
// FILE: Main.kt.ir
38+
val s: String = "\nhello\n"
39+
val s1: String = "${s}"
40+
val s2: String = " hello\n${s}\n world"
41+
fun main() {
42+
println(s)
43+
println(s1)
44+
println(s2)
45+
}

0 commit comments

Comments
 (0)