Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug on parameter's class that reached maximum line #1324

Closed
gbusiri opened this issue Dec 24, 2021 · 2 comments · Fixed by #2060
Closed

Bug on parameter's class that reached maximum line #1324

gbusiri opened this issue Dec 24, 2021 · 2 comments · Fixed by #2060

Comments

@gbusiri
Copy link

gbusiri commented Dec 24, 2021

Expected Behavior

No error.

Observed Behavior

Error.

Steps to Reproduce

I have parameter lambda code like this, let say my parameter is reached maximum limit of line :
image

There is error detected on open bracket (. I already clicked Format with ktlint, but still error.

Your Environment

  • Version of ktlint used: 0.43.2
  • Version of Gradle used (if applicable): AGP 7
  • Operating System and version: MacOS Big Sur
@paul-dingemans
Copy link
Collaborator

Next time, please post your code instead of an image. It is more convenient when trying to reproduce a problem.

The code below is comparable to your example:

// Assume, the line breaks at the pipe symbol on the right                  -> |
val foo1: ((s1: String, s2: String, s3: String, s4: String, s5: String, s6: String) -> Unit)? = null

The ktlint --verbose --relative command results in:

src/main/kotlin/Issue1324.kt:1:1: Exceeded max line length (80) (cannot be auto-corrected) (max-line-length)
src/main/kotlin/Issue1324.kt:1:13: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:25: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:37: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:49: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:61: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:73: Parameter should be on a separate line (unless all parameters can fit a single line) (parameter-list-wrapping)
src/main/kotlin/Issue1324.kt:1:83: Missing newline before ")" (parameter-list-wrapping)

From this it can be seen that both the max-line-length and the parameter-list-wrapping report violations in the original code.

Auto-correct by the parameter-list-wrapping resolves the max line length violation and results in code below:

// Assume, next line exceeds the max line length                            -> |
val foo1: (
        (
            s1: String,
            s2: String,
            s3: String,
            s4: String,
            s5: String,
            s6: String
        ) -> Unit
    )? = null

but that same rule immediately reports now another lint error as you reported as well:

src/main/kotlin/Issue1324.kt:3:5: Unnecessary newline before "(" (parameter-list-wrapping)

So far, I have found no way to refactor the code manually in such a way that the code is accepted by both ktlint as well as by the IntelliJ default code formatting which nowadays is a requirement when we make (bugfixes on) rules. For example, the previous code sample will be transformed by IntelliJ default formatting to following:

// Assume, next line exceeds the max line length                            -> |
val foo1: (
    (
    s1: String,
    s2: String,
    s3: String,
    s4: String,
    s5: String,
    s6: String
) -> Unit
)? = null

This is horrible code.

For now, I see no other way for you to disable both rules and stick with one really long line:

val foo1: ((s1: String, s2: String, s3: String, s4: String, s5: String, s6: String) -> Unit)? = null // ktlint-disable max-line-length parameter-list-wrapping

Maybe, someone else has a briljant idea how to the resulting code should look like in such a way that both ktlint as well as IntelliJ default formatting do not alter that code. Next step would then be to fix the rules do that the code will be formatted as such.

@paul-dingemans paul-dingemans added the conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter label Jun 26, 2022
@paul-dingemans paul-dingemans added this to the 1.0 (Yeah!) milestone Apr 16, 2023
paul-dingemans added a commit that referenced this issue May 27, 2023
… type reference exceeds the maximum line length `parameter-list-wrapping`

Closes #1324
@paul-dingemans
Copy link
Collaborator

For a function type wrapped in a nullable type it is perfectly acceptable to have a opening parenthesis starting on a new line. So the violation should indeed not have been reported. The conflicting formatting is caused by the indent rule. This will not be fixed (e.g. "uglified" to be compatible with IntelliJ IDEA formatting).

@paul-dingemans paul-dingemans removed conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter ktlint-official-codestyle labels May 27, 2023
paul-dingemans added a commit that referenced this issue May 28, 2023
* Prevent incorrect reporting of violations in case a nullable function type reference exceeds the maximum line length `parameter-list-wrapping`

Closes #1324
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants