pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.18k stars 505 forks source link

Indentation for enum entries with multi-line initializers is wrong #518

Closed bethcutler closed 5 years ago

bethcutler commented 5 years ago

Example code, formatted as we would like it to be:

enum class Color(val displayName: String, val value: Int) {
  RED(
    displayName = "Red",
    value = 1
  ),
  BLUE(
    displayName = "Blue",
    value = 2
  );
}

In this case, we get "Unexpected indentation" errors on every line from 3 to 9 (expecting 2 less indentation). We are using 2-space indentation here.

If formatted instead so that the initializer arguments are on one line there are no errors:

enum class Color(val displayName: String, val value: Int) {
  RED(displayName = "Red", value = 1),
  BLUE(displayName = "Blue", value = 2);
}
bethcutler commented 5 years ago

Slightly wrong in my previous comment, there is no error on line 6 (the "BLUE" entry). If formatted like this, there are no errors:

enum class Color(val displayName: String, val value: Int) {
  RED(
  displayName = "Red",
  value = 1
),
  BLUE(
  displayName = "Blue",
  value = 2
);
}
AdamMc331 commented 4 years ago

Has this been released yet? I updated to "0.34.2" but I'm still seeing KtLint format everything back one indentation: https://gist.github.com/AdamMc331/8c6a9ea40ba36803aef3993acfb1e7ce

Tapchicoma commented 4 years ago

@AdamMc331 according to changelog it was released: https://github.com/pinterest/ktlint/blob/master/CHANGELOG.md#0342---2019-07-22

You need to enable experimental rules.

AdamMc331 commented 4 years ago

I have experimental rules on. I tried changing the KtLint verison number in the gradle plugin, but I wonder if I made a mistake there. I will double check, but I can also just upgrade to the fancy new 9.0.0 which targets the latest KtLint and maybe that will resolve my issue. :D

Tapchicoma commented 4 years ago

You can check used ktlint version by running:

$ ./gradlew dependencies --configuration ktlint
AdamMc331 commented 4 years ago

Can confirm this was user error, I made a mistake in my gradle file. So sorry! Thanks for checking in though.