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

[1.3.0] Running -format adds extra spaces to class & function definition #2688

Closed
mateuszkwiecinski opened this issue Jun 4, 2024 · 4 comments · Fixed by #2715
Closed

[1.3.0] Running -format adds extra spaces to class & function definition #2688

mateuszkwiecinski opened this issue Jun 4, 2024 · 4 comments · Fixed by #2715
Milestone

Comments

@mateuszkwiecinski
Copy link
Contributor

Expected Behavior

Running format with autoCorrect produces valid codestyle

Observed Behavior

Running check after successful format fails

Steps to Reproduce

Given the following snippet:

class KotlinClass{

    private fun hi(){
        println("OK")
    }
}

runing ktlint -F produces:

class KotlinClass  {

    private fun hi()  {
        println("OK")
    }
}

(notice the double spaces before {)

which then fails on regular ktlint run with

.../test/KotlinClass.kt:1:19: Unnecessary long whitespace (standard:no-multi-spaces)
.../test/KotlinClass.kt:1:20: Expected a single space before class body (standard:class-signature)
.../test/KotlinClass.kt:3:22: Unnecessary long whitespace (standard:no-multi-spaces)
.../test/KotlinClass.kt:3:23: Expected a single white space before start of function body (standard:function-start-of-body-spacing)
.../test/KotlinClass.kt:3:23: Expected a single space before body block (standard:function-signature)
22:35:13.777 [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Lint has found errors than can be autocorrected using 'ktlint --format'

Summary error count (descending) by rule:
  standard:no-multi-spaces: 2
  standard:class-signature: 1
  standard:function-signature: 1
  standard:function-start-of-body-spacing: 1

Your Environment

  • Version of ktlint used: 1.3.0
  • Relevant parts of the .editorconfig settings
root = true

[*]
insert_final_newline = true

[*.{kt,kts}]
max_line_length = 140
indent_size = 4
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ktlint_code_style = intellij_idea
@paul-dingemans
Copy link
Collaborator

Tnx for reporting.

This problem is present since Ktlint version 1.0.x:

 $ ktlint-1.0.1 --stdin -F
07:59:48.997 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
class KotlinClass{

    private fun hi(){
        println("OK")
    }
}
class KotlinClass  {

    private fun hi()  {
        println("OK")
    }
}

It works fine in version 0.50.0

 $ ktlint-0.50.0 --stdin -F
08:00:03.120 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
class KotlinClass{

    private fun hi(){
        println("OK")
    }
}
08:00:20.335 [main] WARN com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine - Format was not able to resolve all violations which (theoretically) can be autocorrected in file <stdin> in 3 consecutive runs of format.
class KotlinClass {

    private fun hi() {
        println("OK")
    }
}

@paul-dingemans paul-dingemans added this to the 1.3.1 milestone Jun 5, 2024
@mateuszkwiecinski
Copy link
Contributor Author

This problem is present since Ktlint version 1.0.x:

Huh, interesting 😅 I started observing this only when upgrading to 1.3.0, I had an old smoke test which started failing just now - which suggests something recent made the issue more apparent/more easily reproducible? 👀

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Jun 5, 2024

Maybe it is caused by promoting sone experimental rules to standard. If your smoke test runs with non-experimental only this could explain the issue.

@paul-dingemans
Copy link
Collaborator

Maybe it is caused by promoting sone experimental rules to standard. If your smoke test runs with non-experimental only this could explain the issue.

After some more investigation, it turns out that the problem is caused by moving the standard:curly-spacing rule.

Disabling that rule, results in the expected code:

root = true

[*.{kt,kts}]
ktlint_standard = enabled
ktlint_standard_curly-spacing = disabled

results in the expected code:

class KotlinClass {
    private fun hi() {
        println("OK")
    }
}

It is not clear to me why you smoketest did not fail with previous 1.0.x to 1.2.x versions. The class-signature was a experimental rule which is now promoted to non-experimental. The curly-spacing rule was already non-experimental since first release of ktlint.

Anyways, it is still a bug to be resolved.

paul-dingemans added a commit that referenced this issue Jun 25, 2024
…mposite node

In such cases insert the whitespace just before or after the composite element (recursively if needed)

Closes #2688
paul-dingemans added a commit that referenced this issue Jun 25, 2024
…mposite node

In such cases insert the whitespace just before or after the composite element (recursively if needed)

Closes #2688
paul-dingemans added a commit that referenced this issue Jun 25, 2024
…mposite element (#2715)

Prevent inserting whitespace element as first or last element in a composite node

In such cases insert the whitespace just before or after the composite element (recursively if needed)

Closes #2688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants