We want to add a special case formatting to multil...
# ktfmt
n
We want to add a special case formatting to multiline strings with
trimIndent()
/
trimMargin()
and wanted to hear your thoughts about it. We'll use the poll results to guide our approach. Feel free to suggest alternatives as well, but do keep in mind this has to work with all the different use cases, which the represented code here for sure does not cover. Option 1
Copy code
val a =
    """
        a multiline
          string
        """
        .trimIndent()
        .nextMethod()

val b =
    """
        |a multiline
        |  string
        |"""
        .trimMargin()
        .nextMethod()
Option 2
Copy code
val a =
    """
    a multiline
      string
    """
        .trimIndent()
        .nextMethod()

val b =
    """
    |a multiline
    |  string
    |"""
        .trimMargin()
        .nextMethod()
Option 3
Copy code
val a =
    """
    a multiline
      string
    """
    .trimIndent()
        .nextMethod()

val b =
    """
    |a multiline
    |  string
    |"""
    .trimMargin()
        .nextMethod()
Option 4
Copy code
val a =
    """
    a multiline
      string
    """.trimIndent()
        .nextMethod()

val b =
    """
    |a multiline
    |  string
    |""".trimMargin()
        .nextMethod()
1️⃣ 1
2️⃣ 3
3️⃣ 1
4️⃣ 3
p
iirc
.trimIndent()
and
.trimMargin()
are processed at compile time, so I've always seen them as "part" of the terminating
"""
and prefer them attached for that reason
s
iirc
.trimIndent()
and
.trimMargin()
are processed at compile time
I don't think so, since they prevent the string from being a constant IIRC
p
I believe it's annotated @kotlin.internal.IntrinsicConstEvaluation but still isn't considered const
n
From my understanding it does process at compile time in case it's a simple string, without interpolation. In other words:
Copy code
val a = """
    |this is processed
    |at compile time
    |""".trimMargin()


val b = """
    |this is processed
    |at runtime because of this $variableHere
    |""".trimMargin()
👍 2
FWIW, Google Java Format does the equivalent to option #2 there
s
Option 2 looks pretty bad to me imo
p
especially as I assume it would look in a call:
Copy code
val option4 =
    myCall(
        """
        a multiline
          string
        """.trimIndent())

val option2 =
    myCall(
        """
        a multiline
          string
        """
            .trimIndent())
n
I am with you guys here, but right now option 4 doesn't have enough votes for me to push back. For context I'm running the same poll internally and there option #2 is winning by a good margin at the moment.
Poll closes on Wednesday though, so there's time
p
I mean ... 100% of the community poll responses are in favour of option 4 😉
💯 1
😆 1
n
I mean... You are not wrong lol 😂
s
I may decide my vote is on behalf of the whole Android Studio team, since we use ktfmt :P
Yolo