pinterest / ktlint

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

A multiline expression should start on a new line (standard:multiline-expression-wrapping) #2349

Closed vanniktech closed 11 months ago

vanniktech commented 12 months ago

The following code gets flagged:

val yearsAgo = dailyQueryWrapper.dailyPictureQueries.yearsAgo(
      monthDates = localDates
        .filter { it.atTime(it.yearsAgoReminderTime()) >= localDateTime } // Only allow for the future!
        .map { LocalDateAdapter.encode(it).toString().drop(4) },
      year = currentYearString,
    ).executeAsList()

And then reformatted to:

val yearsAgo =
  dailyQueryWrapper.dailyPictureQueries.yearsAgo(
    monthDates =
      localDates
        .filter { it.atTime(it.yearsAgoReminderTime()) >= localDateTime } // Only allow for the future!
        .map { LocalDateAdapter.encode(it).toString().drop(4) },
    year = currentYearString,
  ).executeAsList()

Why though? My understanding is that the 1st code is correctly formatted.

vanniktech commented 12 months ago

This might be the same occurrence?

Screenshot 2023-11-12 at 17 50 47

I don't why a new line was entered after the ->

paul-dingemans commented 12 months ago

Why though? My understanding is that the 1st code is correctly formatted.

Yes, it is correctly formatted depending on the chosen code style. Starting from ktlint 1.x the default code style of ktlint is changed from intellij_idea to ktlint_official. So, if you have not set the code style explicitly in your .editorconfig, the code sample above which was accepted by 0.x is now not accepted anymore. The multiline-expression-wrapping ensures a more consistent code style throughout a project as multiline expressions are wrapped all in the same way.

With other code styles, both samples below are accepted:

val yearsAgo = dailyQueryWrapper.dailyPictureQueries.yearsAgo(
      monthDates = localDates
        .filter { it.atTime(it.yearsAgoReminderTime()) >= localDateTime } // Only allow for the future!
        .map { LocalDateAdapter.encode(it).toString().drop(4) },
      year = currentYearString,
    ).executeAsList()

and

val yearsAgo =
  dailyQueryWrapper.dailyPictureQueries.yearsAgo(
    monthDates =
      localDates
        .filter { it.atTime(it.yearsAgoReminderTime()) >= localDateTime } // Only allow for the future!
        .map { LocalDateAdapter.encode(it).toString().drop(4) },
    year = currentYearString,
  ).executeAsList()

Advantage is that you can decide on a per-case base what style is more readable. Disadvantage it can result in different formatting for the same code block in the same code base.

vanniktech commented 11 months ago

Huh. Using ktlint_code_style=intellij_idea helped me in that case. I would have expected the change of the default to be in the 1.0.0 release notes. Thanks for your immediate help!

paul-dingemans commented 11 months ago

Huh. Using ktlint_code_style=intellij_idea helped me in that case. I would have expected the change of the default to be in the 1.0.0 release notes. Thanks for your immediate help!

It was mentioned in the release notes. But it might got lost between all other changes. Screenshot 2023-11-19 at 11 58 53