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

ktlint_function_signature_body_expression_wrapping=always behavior question #2872

Closed
andrewparmet opened this issue Nov 18, 2024 · 4 comments · Fixed by #2873
Closed

ktlint_function_signature_body_expression_wrapping=always behavior question #2872

andrewparmet opened this issue Nov 18, 2024 · 4 comments · Fixed by #2873

Comments

@andrewparmet
Copy link

Expected Behavior

I'd like to format my multiline expression functions like so:

fun foo() =
    "bar"

fun foo(
    bar: BarBarBarBarBarBarBarBarBarBarBarBar,
    baz: BarBarBarBarBarBarBarBarBarBarBarBar,
    baf: BarBarBarBarBarBarBarBarBarBarBarBar,
    bag: BarBarBarBarBarBarBarBarBarBarBarBar,
) =
    Foo(
        bar = bar,
        baz = baz
    )

Observed Behavior

Instead the second declaration is formatted as:

fun foo(
    bar: BarBarBarBarBarBarBarBarBarBarBarBar,
    baz: BarBarBarBarBarBarBarBarBarBarBarBar,
    baf: BarBarBarBarBarBarBarBarBarBarBarBar,
    bag: BarBarBarBarBarBarBarBarBarBarBarBar,
) = Foo(
    bar = bar,
    baz = baz
)

Steps to Reproduce

My editorconfig has ktlint_function_signature_body_expression_wrapping = always and a max line length to force this parameter list to wrap. The expression wrapping works when the function declaration is a single line (the first function declaration) but not when the function declaration spans lines (the second). Is it possible to achieve what I'm looking for? Thanks!

Your Environment

  • Version of ktlint used: 1.4.1
  • Relevant parts of the .editorconfig settings:
        "max_line_length" to 120,
        "ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than" to 1,
        "ktlint_function_signature_body_expression_wrapping" to "always",
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): Spotless v7 beta 3
  • Version of Gradle used (if applicable): 8.10
  • Operating System and version: Mac
@paul-dingemans
Copy link
Collaborator

If I recall correctly it is a deliberate exception to not wrap in case the closing parenthesis starts on a newline and has no return type. Reasoning was that the body expression looks a bit "lost" as its indentation is equal to the start position when lines are merged (of course given the widely accepted standard of 4 spaces for indentation):

) =
    Foo(

vs

) = Foo(

I do understand that this exception feels weird when .editorconfig property ktlint_function_signature_body_expression_wrapping is set to always. Let me double check this.

@andrewparmet
Copy link
Author

Thanks - all of our code is consistently formatted with wrapped expression function bodies and this forces us to be inconsistent in the majority of cases (where we omit the return type).

@andrewparmet
Copy link
Author

@paul-dingemans Thanks for the quick fix! Do you know the release schedule for this change?

@paul-dingemans
Copy link
Collaborator

If all goes well, this week.

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