Skip to content

CodeBlock.endFlowControl shouldn't insert a new line by default #1330

Open
@Egorand

Description

Probably a relict of JavaPoet where a control flow would almost certainly be followed by new line. With Kotlin, new line by default makes code like below look bad:

@Test fun controlFlowsAsExpressions() {
  val controlFlows = buildList { 
    repeat(2) { 
      add(
        CodeBlock.builder()
          .beginControlFlow("if (num %% 2 == 0)")
          .addStatement("println(%S)", "foo")
          .nextControlFlow("else")
          .addStatement("println(%S)", "bar")
          .endControlFlow()
          .build()
      )
    }
  }
  val listOfControlFlows = controlFlows
    .joinToCode(prefix = "listOf(⇥\n", separator = ",\n", suffix = ",⇤\n)")
  assertThat(listOfControlFlows.toString()).isEqualTo(
    """
    listOf(
      if (num % 2 == 0) {
        println("foo")
      } else {
        println("bar")
      }
      ,
      if (num % 2 == 0) {
        println("foo")
      } else {
        println("bar")
      }
      ,
    )
    """.trimIndent()
  )
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions