Skip to content
New issue

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

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

Already on GitHub? # to your account

Intellij disagree with ktlint for code with for #1350

Closed
fzyzcjy opened this issue Jan 26, 2022 · 3 comments · Fixed by #1464
Closed

Intellij disagree with ktlint for code with for #1350

fzyzcjy opened this issue Jan 26, 2022 · 3 comments · Fixed by #1464
Labels
conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter indentation rule
Milestone

Comments

@fzyzcjy
Copy link

fzyzcjy commented Jan 26, 2022

Again, thank you so much for the linter! Just a small bug: The following code is formatted in intellij idea. But ktlint says it has several formatting errors.

p.s. the code seems artificial, just because I see ktlint bug in real code and I replace all variable names with fake names (with the same length) to avoid leaking internal code.

(base) ➜  app git:(feat/2588) ✗ cat ~/temp/a.kt && java -jar /Users/tom/opt/ktlint-0.44.0-20220123.092956-49-all.jar ~/temp/a.kt --debug
class A {
    private fun hi() {
        for (
        helloWorldHelloWorldAbcd in Lists.partition(
            abcdefgabcdefgabcdefgab,
            hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
        )
        ) {
            hello()
        }
    }
}
[DEBUG] Discovered reporter with "baseline" id.
[DEBUG] Discovered reporter with "checkstyle" id.
[DEBUG] Discovered reporter with "json" id.
[DEBUG] Discovered reporter with "html" id.
[DEBUG] Discovered reporter with "plain" id.
[DEBUG] Discovered reporter with "sarif" id.
[DEBUG] Initializing "plain" reporter with {verbose=false, color=false, color_name=DARK_GRAY}
[DEBUG] Rule with id 'standard:max-line-length' should run after the rule with id 'experimental:trailing-comma'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
[DEBUG] Rules will be executed in order below (unless disabled):
           - standard:filename, 
           - standard:final-newline, 
           - standard:chain-wrapping, 
           - standard:colon-spacing, 
           - standard:comma-spacing, 
           - standard:comment-spacing, 
           - standard:curly-spacing, 
           - standard:dot-spacing, 
           - standard:import-ordering, 
           - standard:keyword-spacing, 
           - standard:modifier-order, 
           - standard:no-blank-line-before-rbrace, 
           - standard:no-consecutive-blank-lines, 
           - standard:no-empty-class-body, 
           - standard:no-line-break-after-else, 
           - standard:no-line-break-before-assignment, 
           - standard:no-multi-spaces, 
           - standard:no-semi, 
           - standard:no-trailing-spaces, 
           - standard:no-unit-return, 
           - standard:no-unused-imports, 
           - standard:no-wildcard-imports, 
           - standard:op-spacing, 
           - standard:parameter-list-wrapping, 
           - standard:paren-spacing, 
           - standard:range-spacing, 
           - standard:string-template, 
           - standard:indent, 
           - standard:max-line-length
[DEBUG] Checking /Users/tom/temp/a.kt
Resolving .editorconfig files for /Users/tom/temp/a.kt file path
Loaded .editorconfig: []
/Users/tom/temp/a.kt:1:1: class A should be declared in a file named A.kt (cannot be auto-corrected)
/Users/tom/temp/a.kt:4:1: Unexpected indentation (8) (should be 12)
/Users/tom/temp/a.kt:5:1: Unexpected indentation (12) (should be 16)
/Users/tom/temp/a.kt:6:1: Unexpected indentation (12) (should be 16)
/Users/tom/temp/a.kt:7:1: Unexpected indentation (8) (should be 12)
[DEBUG] 769ms / 1 file(s) / 5 error(s)
@paul-dingemans
Copy link
Collaborator

This issue is not related to issue mentioned before.

You're right that it conflicts with IntelliJ formatting. Although, I do like the ktlint style much more, it should indeed being considered as a bug because we do no want ktlint to produce code that is not accepted by the IntelliJ default formatting.

class A {
    private fun hi() {
        for (
            helloWorldHelloWorldAbcd in Lists.partition(
                abcdefgabcdefgabcdefgab,
                hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
            )
        ) {
            hello()
        }
    }
}

@paul-dingemans paul-dingemans added bug indentation rule conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter labels Jan 26, 2022
@fzyzcjy
Copy link
Author

fzyzcjy commented Jan 26, 2022

I agree with you

@paul-dingemans paul-dingemans added this to the 0.46.0 milestone Mar 17, 2022
paul-dingemans pushed a commit to paul-dingemans/ktlint that referenced this issue May 8, 2022
…ntil https://youtrack.jetbrains.com/issue/IDEA-293691/Format-Kotlin-for-loop is fixed. The wrapping rule no longer forces the for-statement to be wrapped if only the expression contains a newline.

Closes pinterest#1350
@paul-dingemans paul-dingemans mentioned this issue May 8, 2022
5 tasks
@paul-dingemans
Copy link
Collaborator

This issue should be fixed in IntelliJ IDEA, see https://youtrack.jetbrains.com/issue/IDEA-293691/Format-Kotlin-for-loop. For the time being, the code will be accepted without errors. I have also fixed the wrapping so that it is allowed to write the code as follows which is also accepted by default formatter of IntelliJ:

class A {
    private fun hi() {
        for (helloWorldHelloWorldAbcd in Lists.partition(
                abcdefgabcdefgabcdefgab,
                hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
        )) {
            hello()
        }
    }
}```

paul-dingemans added a commit that referenced this issue May 20, 2022
Default formatting of IntelliJ is ugly and needs to be fixed (https://youtrack.jetbrains.com/issue/IDEA-293691/Format-Kotlin-for-loop). The wrapping rule no longer forces the for-statement to be wrapped if only the expression contains a newline. (#1464)

Closes #1350
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter indentation rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants