android-studiokotlinintellij-ideacode-formattingkotest

Format Kotlin with minimal indent for code blocks in constructor parameters, like Foo({ bar })


When formatting this code block (from the Kotest documentation), Jetbrains Intellij or Android Studio adds a lot of unnecessary white space.

Original code:

class MyTests : FreeSpec({
     "String.length" - {
         "should return the length of the string" {
             "sammy".length shouldBe 5
             "".length shouldBe 0
         }
     }
})

After formating with ctrl+alt+L, this becomes

class MyTests : FreeSpec({
                             "String.length" - {
                                 "should return the length of the string" {
                                     "sammy".length shouldBe 5
                                     "".length shouldBe 0
                                 }
                             }
                         })

Is there a formatting setting to adjust this behavior?

I strongly suspect that there is a working combination of settings in Editor -> Code Style -> Kotlin, but after trying out various combinations I could not figure it out.


Solution

  • You may duplicate a formating scheme to alter safely or just do the following things.

    Editor > Code Style > Kotlin > Wrapping and Braces > Function call arguments

    Uncheck Align when multiline

    I also use Kotest FreeSpec. This was the tweak I made to have nice formatting.