pinterest / ktlint

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

Indent + parameter wrapping rules cause " Format was not able to resolve all violations" warning with long lines #2488

Closed vinaygopinath closed 6 months ago

vinaygopinath commented 6 months ago

When the parameter-wrapping and indent rules are enabled, and a function parameter exceeds the max line length, ktlint -F shows the warning

KtLintRuleEngine -- Format was not able to resolve all violations which (theoretically) can be autocorrected in file <absolute file path> in 3 consecutive runs of format

Sample code

class MyClass @Inject constructor(
  val someReallyLongFieldNameUsedInMyClass: SomeReallyLongDependencyClass
)

Expected Behavior

I believe the expected formatted output is

class MyClass @Inject constructor(
  val someReallyLongFieldNameUsedInMyClass:
    SomeReallyLongDependencyClass
)

Observed Behavior

The actual formatted output is

class MyClass @Inject constructor(
  val someReallyLongFieldNameUsedInMyClass:
  SomeReallyLongDependencyClass // I wonder if the class name *not* being indented is the cause of the issue?
)

Your Environment

vinaygopinath commented 6 months ago

On second thought, I think the warning is due to the interaction between

  1. standard:parameter-list-spacing which expects the field name and its class to be separated by a colon and a space, as in exactly in the unformatted code
    class MyClass @Inject constructor(
     val someReallyLongFieldNameUsedInMyClass: SomeReallyLongDependencyClass
    )
  2. standard:parameter-wrapping which, when the field name and class name exceed the max line length, complains about a "Missing newline before SomeReallyLongDependencyClass"
  3. standard:max-line-length
  4. and standard:indent
paul-dingemans commented 6 months ago

Technically I could not reproduce your problem because you most likely have changed the class names to shorter names. But with decreasing the max_line_length to 74, I was able to reproduce it.

paul-dingemans commented 6 months ago

Problem is indeed caused by rules parameter-list-spacing and parameter-wrapping. They disagree with each other about how to format the parameter.