-
Notifications
You must be signed in to change notification settings - Fork 509
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
Parameter wrapping should take priority over expression wrapping #2454
Comments
This can be achieved by setting |
No setting of To clarify. For For Given this input: // Assume that the last allowed character is
// at the X character on the right X
fun foo(
bar: String = "loooooooooong",
): Foo = Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) The parameter is joined first. // Assume that the last allowed character is
// at the X character on the right X
fun foo(bar: String = "loooooooooong"): Foo = Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) Then the expression is wrapped since it doesn't fit. // Assume that the last allowed character is
// at the X character on the right X
fun foo(bar: String = "loooooooooong"): Foo =
Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) Expected would be that the given should stay the same since joining it would make the line too long. The same way the following should wrap the parameter before wrapping the expression body. // Assume that the last allowed character is
// at the X character on the right X
fun foo(bar: String = "loooooooooong"): Foo = Foo.baz(a = "looooooooooooooooooooooooooooooooooooong", b = "looooooooooooooooooooooooooooooooooooong") |
Let me investigate this once more. |
Can you double check that you are using the configuration settings correctly? I see in this post several references to settings which are missing the Given
the code below:
is formatted as:
|
I have not set Imo it should still prefer to break the argument over breaking the expression since the line is too long. |
On response to my suggestion:
you said that:
You can achieve what you want, by setting it to 1. |
I'm trying to say that it should be wrapped based on line length, not because of the parameter number. With the default setting (2) both of the following should be correctly formatted. // Assume that the last allowed character is
// at the X character on the right X
fun foo(
bar: String = "loooooooooong",
): Foo = Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) // Assume that the last allowed character is
// at the X character on the right X
fun foo(bar: String = "short"): Foo = Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) |
Please provide your When I use:
code is formatted as you requested. Note that |
Minimal example
|
You need to overwrite
|
Actually while producing a minimal example I might've accidentally omitted that part and also forgot another crucial overwrite,
Still, my examples show violations: // Assume that the last allowed character is
// at the X character on the right X
fun foo(
bar: String = "loooooooooong",
): Foo = Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
)
|
I still can not reproduce this. With
But, I do note that your
Without this property, you might actually pick-up an |
I tried it with and without root. Did you run check or format? fun foo(bar: String = "loooooooooong"): Foo =
Foo.baz(
a = "looooooooooooooooooooooooooooooooooooong",
b = "looooooooooooooooooooooooooooooooooooong",
) |
Did you mean "below" instead of "above"? Please try to be more explicit, because I getting confused and need to guess. So please, specify:
|
Sorry for the confusion. I wrote tests instead. If After trying a few more examples, though, I think my expected cases would cause too much complexity. For this to work I think it's better to leave the issue closed, but for completeness sake and future reference I'll leave the expectation and test cases here. Ideally both of the following would be allowed: fun foo(
bar: String
): Foo = Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
fun foo(bar: String): String =
Foo.baz(a = "a", b = "b")
Test cases class ExampleTest {
private val argumentListWrappingRuleAssertThat =
assertThatRuleBuilder { ArgumentListWrappingRule() }
.addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider())
.assertThat()
@Test
fun `Expected signature_body_expression_wrapping=default (unformatted)`() {
val code =
"""
fun foo(bar: String): Foo = Foo.baz(a = "looooooooooooooooooooong", b = "looooooooooooooooooooong")
""".trimIndent()
val formattedCode =
"""
fun foo(
bar: String
): Foo = Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
argumentListWrappingRuleAssertThat(code)
.addAdditionalRuleProviders(
{ FunctionSignatureRule() },
{ IndentationRule() },
).withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 35)
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to "default")
.isFormattedAs(formattedCode)
}
@Test
fun `Expected signature_body_expression_wrapping=default (pre-formatted)`() {
val code =
"""
fun foo(
bar: String
): Foo = Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
argumentListWrappingRuleAssertThat(code)
.addAdditionalRuleProviders(
{ FunctionSignatureRule() },
{ IndentationRule() },
).withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 35)
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to "default")
.hasNoLintViolations()
}
@Test
fun `Current signature_body_expression_wrapping=default (pre-formatted)`() {
val code =
"""
fun foo(
bar: String
): Foo = Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
val formattedCode =
"""
fun foo(bar: String): Foo =
Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
argumentListWrappingRuleAssertThat(code)
.addAdditionalRuleProviders(
{ FunctionSignatureRule() },
{ IndentationRule() },
).withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 35)
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to "default")
.hasLintViolationsForAdditionalRule(
LintViolation(2, 5, "No whitespace expected between opening parenthesis and first parameter name", true),
LintViolation(2, 16, "No whitespace expected between last parameter and closing parenthesis", true),
LintViolation(3, 10, "Newline expected before expression body", true),
).isFormattedAs(formattedCode)
}
@Test
fun `Current & Expected signature_body_expression_wrapping=multiline`() {
val code =
"""
fun foo(
bar: String
): Foo = Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
val formattedCode =
"""
fun foo(bar: String): Foo =
Foo.baz(
a = "looooooooooooooooooooong",
b = "looooooooooooooooooooong"
)
""".trimIndent()
argumentListWrappingRuleAssertThat(code)
.addAdditionalRuleProviders(
{ FunctionSignatureRule() },
{ IndentationRule() },
).withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 35)
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to "multiline")
.hasLintViolationsForAdditionalRule(
LintViolation(2, 5, "No whitespace expected between opening parenthesis and first parameter name", true),
LintViolation(2, 16, "No whitespace expected between last parameter and closing parenthesis", true),
LintViolation(3, 10, "Newline expected before expression body", true),
).isFormattedAs(formattedCode)
}
@Test
fun `Current & Expected where argument-list-wrapping would lead to additional breaks signature_body_expression_wrapping=default (pre-formatted)`() {
val code =
"""
fun foo(bar: String): String =
Foo.baz(a = "a", b = "b")
""".trimIndent()
argumentListWrappingRuleAssertThat(code)
.addAdditionalRuleProviders(
{ FunctionSignatureRule() },
{ IndentationRule() },
).withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 35)
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to "default")
.hasNoLintViolations()
}
} |
It took a while before I had a chance to look at this again. It is not that difficult to achieve now you already provided as set of tests that would statisfy you. The |
function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than
should take priority overfunction_signature_body_expression_wrapping
.This can save horizontal space at the cost of 1 line vertical space.
Expected Behavior
Current Behavior
Additional error:
Additional information
The text was updated successfully, but these errors were encountered: