Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

New rule proposal: Separate the class header and body when the class header is long #2788

Closed
3flex opened this issue Sep 6, 2024 · 1 comment

Comments

@3flex
Copy link
Contributor

3flex commented Sep 6, 2024

Expected Behavior

Class header & body are separated after a long class header. Compliant examples from Kotlin coding conventions:

class MyFavouriteVeryLongClassHolder :
    MyLongHolder<MyFavouriteVeryLongClass>(),
    SomeOtherInterface,
    AndAnotherOne {

    fun foo() { /*...*/ }
}

In the above example, there is a blank line between the opening brace and the class body.

class MyFavouriteVeryLongClassHolder :
    MyLongHolder<MyFavouriteVeryLongClass>(),
    SomeOtherInterface,
    AndAnotherOne
{
    fun foo() { /*...*/ }
}

In the above example, there is no blank line between the opening brace and the class body, but the opening brace is on its own line.

ktlint should enforce this rule.

Observed Behavior

As far as I can tell, ktlint does not support this. Some non-compliant examples from the docs (dev-snapshot) follow.

Class signature

class Foo7(
    val bar1: Bar,
    val bar2: Bar,
) : FooBar(
        bar1,
        bar2,
    ),
    BarFoo1,
    BarFoo2 {
    // body
}

In this example, the // body sits against the header. There are other examples in class signature code samples.

No blank lines in list

class FooBar :
    Foo,
    Bar {
    // body
}

In this example, body sits against the class header. I would call this a "long" class header as it's multi-line.

I note that this proposal conflicts with "No empty first line at start in class body" so would have to be disabled or enabled only in part when ktlint_official rule style is used.

Steps to Reproduce

Your Environment

  • Version of ktlint used:
  • Relevant parts of the .editorconfig settings
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
  • Version of Gradle used (if applicable):
  • Operating System and version:
@paul-dingemans
Copy link
Collaborator

Ktlint indeed does not comply with Kotlin Coding Conventions in this regard. Allowing the first line in a class to be blank is not consistent with disallowing the first line in a function to be blank. Also, it would be inconsistent to allow a blank line as first line in the class, but disallowing a blank line as last line in the class. Wrapping the opening brace to a separate line is not consistent with the way braces are used in all other block like situations.

Let's move this issue to the discussion section about ktlint configurability. Maybe the rule no-empty-class-body can be replaced with a new rule that allows following options:

  • Disallow blank line at start of class
  • Enforce blank line at start of class
  • Enforce blank line at start of class for long class header only
  • Wrap opening curly brace for long class header only

@pinterest pinterest locked and limited conversation to collaborators Sep 9, 2024
@paul-dingemans paul-dingemans converted this issue into discussion #2790 Sep 9, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants