Skip to content

Commit

Permalink
Wrap the expression body in case .editorconfig property `ktlint_fun…
Browse files Browse the repository at this point in the history
…ction_signature_body_expression_wrapping` is set to `always` (#2873)

Closes #2872
  • Loading branch information
paul-dingemans authored Dec 3, 2024
1 parent c50c237 commit fcccb9f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ public class FunctionSignatureRule :
}
val mergeWithFunctionSignature =
when {
functionBodyExpressionWrapping == always -> {
false
}

firstLineOfBodyExpression.length < maxLengthRemainingForFirstLineOfBodyExpression -> {
(functionBodyExpressionWrapping == default && !functionBodyExpressionNodes.isMultilineStringTemplate()) ||
(functionBodyExpressionWrapping == multiline && functionBodyExpressionLines.size == 1) ||
Expand All @@ -588,7 +592,8 @@ public class FunctionSignatureRule :
!whiteSpaceBeforeFunctionBodyExpression.textContains('\n')
) {
if (node.isMultilineFunctionSignatureWithoutExplicitReturnType(lastNodeOfFunctionSignatureWithBodyExpression) &&
firstLineOfBodyExpression.length + 1 <= maxLengthRemainingForFirstLineOfBodyExpression
firstLineOfBodyExpression.length + 1 <= maxLengthRemainingForFirstLineOfBodyExpression &&
functionBodyExpressionWrapping != always
) {
if (whiteSpaceBeforeFunctionBodyExpression == null ||
whiteSpaceBeforeFunctionBodyExpression.text != " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,20 +773,25 @@ class FunctionSignatureRuleTest {
) {
val code =
"""
fun f(a: Any, b: Any): String = "some-result"
fun f1(a: Any, b: Any): String = "some-result"
fun f2(a: Any, b: Any) = "some-result"
""".trimIndent()
val formattedCode =
"""
fun f(a: Any, b: Any): String =
fun f1(a: Any, b: Any): String =
"some-result"
fun f2(a: Any, b: Any) =
"some-result"
""".trimIndent()
functionSignatureWrappingRuleAssertThat(code)
.withEditorConfigOverride(CODE_STYLE_PROPERTY to codeStyleValue)
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to always)
.addAdditionalRuleProvider { IndentationRule() }
.hasLintViolation(1, 33, "Newline expected before expression body")
.isFormattedAs(formattedCode)
.hasLintViolations(
LintViolation(1, 34, "Newline expected before expression body"),
LintViolation(2, 26, "Newline expected before expression body"),
).isFormattedAs(formattedCode)
}

@ParameterizedTest(name = "bodyExpressionWrapping: {0}")
Expand Down Expand Up @@ -1008,6 +1013,7 @@ class FunctionSignatureRuleTest {
@ParameterizedTest(name = "bodyExpressionWrapping: {0}")
@EnumSource(
value = FunctionBodyExpressionWrapping::class,
names = ["default", "multiline"],
)
fun `Given a multiline function signature without explicit return type and start of body expression on next line then keep first line of body expression body on the same line as the last line of the function signature`(
bodyExpressionWrapping: FunctionBodyExpressionWrapping,
Expand Down Expand Up @@ -1042,6 +1048,7 @@ class FunctionSignatureRuleTest {
@ParameterizedTest(name = "bodyExpressionWrapping: {0}")
@EnumSource(
value = FunctionBodyExpressionWrapping::class,
names = ["default", "multiline"],
)
fun `Given a multiline function signature without explicit return type and start of body expression on same line as last line of function signature then do not reformat`(
bodyExpressionWrapping: FunctionBodyExpressionWrapping,
Expand All @@ -1062,6 +1069,35 @@ class FunctionSignatureRuleTest {
.hasNoLintViolationsExceptInAdditionalRules()
}

@Test
fun `Issue 2872 - Given that expression bodies have to be wrapped always, a multiline function signature without explicit return type and start of body expression on same line as last line of function signature then do reformat`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun functionSignatureTooLongForSingleLine(
a: Any,
b: Any
) = "some-result"
.uppercase()
""".trimIndent()
val formattedCode =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun functionSignatureTooLongForSingleLine(
a: Any,
b: Any
) =
"some-result"
.uppercase()
""".trimIndent()
functionSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to always)
.addAdditionalRuleProvider { IndentationRule() }
.hasLintViolation(5, 5, "Newline expected before expression body")
.isFormattedAs(formattedCode)
}

@Nested
inner class `Given a single line function signature with an annotated parameter` {
@Test
Expand Down

0 comments on commit fcccb9f

Please sign in to comment.