ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

ktlint indentation rule has problems with disable check on a single line

Open zsqw123 opened this issue 1 year ago • 2 comments

Behavior

Like the code below, I want to disable the format of on the first .append method, but that doesn't seem to work

fun main() {
    StringBuilder()
    .append("1") // ktlint-disable
    .append("1")

    // expect:
    StringBuilder()
    .append("1") // ktlint-disable
        .append("1")

    // but:
    StringBuilder()
        .append("1") // ktlint-disable
        .append("1")
}

Steps to Reproduce

Using the code mentioned in Behavior, and run ktlint -F

Your Environment

  • Version of ktlint used: 0.47
  • No .editorconfig
  • Version of Gradle used (if applicable): No
  • Operating System and version: MacOS 12.5

Possible reasons

ktlint's rules use startOffset for determining whether to disable, but PsiWhiteSpace's startOffset is on the previous line when it contains \n, which seems to lead to this error

zsqw123 avatar Sep 11 '22 06:09 zsqw123

Reference to PR #1646 is invalid. This issue is not fixed by that PR.

paul-dingemans avatar Sep 16 '22 19:09 paul-dingemans

I am not in favor of changing this behavior as I am considering to remove the ktlint-disable/ktlint-enable directives as the @Suppress("ktlint:all") is more native.

paul-dingemans avatar Sep 16 '22 19:09 paul-dingemans