https://kotlinlang.org logo
#announcements
Title
# announcements
s

Sam Garfinkel

11/22/2019, 6:44 PM
Is there anyway to ensure a common indent for interpolated multi-line strings? Here’s an example:
Copy code
val inner = """
    - List Entry 1
    - List Entry 2
""".trimIndent()

val outer = """
Outer:
    $inner
""".trimIndent()

fun main() {
    println(outer)
}
What I get:
Copy code
Outer:
    - List Entry 1
- List Entry 2
What I want (expect):
Copy code
Outer:
    - List Entry 1
    - List Entry 2
c

Casey Brooks

11/22/2019, 6:50 PM
You could add pipes to mark the start of indent in the first string, and then call
.trimMargin()
instead of
.trimIndent()
on the second, trimming to the pipes from the first https://pl.kotl.in/AIX3d_2C5
s

Shawn

11/22/2019, 6:53 PM
rough way of doing it perhaps
Copy code
val inner = """
    - List Entry 1
    - List Entry 2
""".trimIndent()

val outer = """
    |Outer:
    |${inner.prependIndent("    ")}
""".trimMargin()
s

Sam Garfinkel

11/22/2019, 7:04 PM
Right maybe I should reword the question--is there anyway to make the interpolation apply the local indentation of where the interpolation occurs to each interpolated line. If you’ve ever used Xtend, that’s how its template expressions work. The issue with the first solution is that it requires each interpolated string to be correctly indented for any place it occurs.
c

Casey Brooks

11/22/2019, 7:12 PM
There is a difference with that, where XTend is a template, and it does processing in combining values into template expressions. In Kotlin multiline strings, interpolation is literally just concatenating plain Strings together, there’s no template-like logic that could be done under-the-hood with them
1
s

Sam Garfinkel

11/22/2019, 7:14 PM
Ah, that’s unfortunate. I thought maybe during bytecode generation there was some mechanism for doing that.
3 Views