0

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.

HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • Do you know that behavior of code formatting is configurable in the settings? -> Editor -> Code Style – Marcin Orlowski Feb 27 '23 at 14:27
  • Certainly, but I could not find a setting for this specific problem. But I strongly suspect there is a setting to adjust this, which is why I am asking this question. – HugoRune Feb 27 '23 at 14:30
  • 1
    I suspect there isn't a setting for this one because it's a rare case--lambda in a super-constructor call. Maybe it will look better if you start the lambda on the second line, and move the closing parenthesis to the next line. – Tenfour04 Feb 27 '23 at 14:59

2 Answers2

2

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.

ocos
  • 1,901
  • 1
  • 10
  • 12
1

I tried to format your snippet in Android Studio Giraffe 2022.3.1 Canary 6 and it remains intact: enter image description here

I suggest you to try a fresh installation of Android Studio/IntelliJ together with a fresh app using Kotest and see if the issue resurfaces.

If it doesn't then you can compare the following files of the two projects to figure out the difference: $projectRootDir/.idea/codeStyles/Project.xml

Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37