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

intellij_idea coding style can not format block comments to line comments at the end of a statement #2936

Open
pohlondrej opened this issue Feb 5, 2025 · 1 comment
Milestone

Comments

@pohlondrej
Copy link

Expected Behavior

Ktlint successfully formats a block comment at the end of a statement into a line comment:

fun main() {
    println("test") // test
}

Observed Behavior

Ktlint fails with:

14:36:33.661 [pool-1-thread-1] WARN com.pinterest.ktlint.rule.engine.internal.CodeFormatter -- Format was not able to resolve all violations which (theoretically) can be autocorrected in file Main.kt

Steps to Reproduce

.editorconfig

root = true

[*.{kt,kts}]
ktlint_code_style = intellij_idea

Note: This bug only occurs when ktlint_code_style is set to intellij_idea. When it's set to ktlint_official, ktlint formats the file correctly.

Main.kt

fun main() {
    println("test") /* test */
}

Environment

@paul-dingemans
Copy link
Collaborator

Tnx for reporting. There is something strange going on, and your example perfectly demonstrates that. A quick investigation lead to following observations that I will have to look into later:

  • Running ktlint without format and code style intellij_idea on the code example leads to violation:

    Foo.kt:2:21: A single line block comment after a code element on the same line must be replaced with an EOL comment (standard:comment-wrapping)
    

    Although this remark seems correct, it is odd that it is reported by rule comment-wrapping. The fix applied by this rule is changing some whitespace element, but not the comment markers. So this confirms that the format does not work. The reported violation has to be validated, and description should match with the fix that can be applied.

  • Running ktlint without format and code style ktlint_official on the code example leads to violations:

    Foo.kt:2:21: A single line block comment after a code element on the same line must be replaced with an EOL comment (standard:comment-wrapping)
    Foo.kt:2:21: Replace the block comment with an EOL comment (standard:no-single-line-block-comment)
    

15:57:42.748 [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Lint has found errors than can be autocorrected using 'ktlint --format'
```
Now we have two rules reporting a violation about the block comment. The violation reported by no-single-line-block-comment has the autocorrect fix that actually replaces the block comment by an EOL comment. This rule is by default only enabled in the `ktlint_official` code style, but it can be enabled explicitly for other code styles as well.

@paul-dingemans paul-dingemans added this to the 1.6 milestone Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants