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

Styling for context receiver #1672

Closed
chao2zhang opened this issue Oct 10, 2022 · 2 comments · Fixed by #1693
Closed

Styling for context receiver #1672

chao2zhang opened this issue Oct 10, 2022 · 2 comments · Fixed by #1693

Comments

@chao2zhang
Copy link
Contributor

Context receiver was discussed in #1436 and introduced in Kotlin 1.6.20 https://kotlinlang.org/docs/whatsnew1620.html#prototype-of-context-receivers-for-kotlin-jvm

Expected Rule behavior

Ensure that we have context receiver separated from other declarations based on the code examples in https://kotlinlang.org/docs/whatsnew1620.html#prototype-of-context-receivers-for-kotlin-jvm
Of course when context receiver is added to the coding convention, there is a chance that Kotlin coding convention might suggest against the above.

Additional information

  • Current version of ktlint:
  • Styleguide section: There are a few references but no official style guide yet. This means that this rule could wait until context receiver is promoted to stable feature.
@paul-dingemans
Copy link
Collaborator

Absens of coding convention is not really a problem. For now I will look at default IntelliJ IDEA formatter and the sources you have provided.

@paul-dingemans
Copy link
Collaborator

For now, I have identified a few code sample on which special indentation is needed:

Context receiver with a list of parameters on separate lines
Proposed formatting in KtLint:

context(
    Foo,
    Bar
)
fun fooBar()

Default IntelliJ IDEA formatter however does not indent the Foo and Bar types, resulting in:

context(
Foo,
Bar
)
fun fooBar()

Context receiver with a generic type
Proposed formatting in KtLint:

context(
    FooBar<
        Foo,
        Bar
        >
)
fun fooBar()

Default IntelliJ IDEA formatter however does not de-indent the closing ">" , resulting in:

context(
FooBar<
    Foo,
    Bar
    >
)
fun fooBar()

Although, this can be fixed in KtLint quite easily, I propose to use the IntelliJ IDEA default formatting for now to keep it in sync with other TYPE_ARGUMENT_LISTs which are formatted in this way.

Context receiver combined with a modifier
Proposed formatting in KtLint:

context(Comparator<T>)
public fun <T> T.compareTo(other: T) = compare(this, other)

Default IntelliJ IDEA formatter however indents modifiers, resulting in:

context(Comparator<T>)
    public fun <T> T.compareTo(other: T) = compare(this, other)

A comparable example on a property has already been submitted as https://youtrack.jetbrains.com/issue/KTIJ-21072/Provide-proper-indentation-on-declarations-with-context-receivers

Please let me know, if you have other examples of context receivers for which special indentation is required.

paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Nov 2, 2022
paul-dingemans added a commit that referenced this issue Nov 5, 2022
* Format context receiver
* Add experimental rule "context-receiver-wrapping"

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

Successfully merging a pull request may close this issue.

2 participants