From 4a7e9d7867a4000cbba65735490aa55ecf0a2a8b Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Mon, 14 Oct 2024 17:17:54 +0200 Subject: [PATCH] Fix wrapping of expression body when `max_line_length` not set (#2833) This fixes the wrapping of the expression for code style `intellij_idea` which does not set the `max_line_length` property by default. But it also fixes the wrapping in case the `max_line_length` property contains value `unset` for any of the code styles. Closes #2827 --- .../standard/rules/FunctionSignatureRule.kt | 1 + .../rules/FunctionSignatureRuleTest.kt | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt index 5417905292..b56b62e85f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt @@ -240,6 +240,7 @@ public class FunctionSignatureRule : } else { fixWhiteSpacesInValueParameterList(node, emit, multiline = true, dryRun = false) } + fixFunctionBodyExpression(node, emit, MAX_LINE_LENGTH_PROPERTY_OFF) } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt index 0deeadb929..3ab138c6bd 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt @@ -8,6 +8,7 @@ import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.Companion.FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.Companion.FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.FunctionBodyExpressionWrapping +import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.FunctionBodyExpressionWrapping.always import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.FunctionBodyExpressionWrapping.default import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER @@ -743,14 +744,8 @@ class FunctionSignatureRuleTest { .hasNoLintViolations() } - @ParameterizedTest(name = "bodyExpressionWrapping: {0}") - @EnumSource( - value = FunctionBodyExpressionWrapping::class, - names = ["always"], - ) - fun `Given that the function signature and a single line body expression body fit on the same line then do not reformat function signature but move the body expression to a separate line`( - bodyExpressionWrapping: FunctionBodyExpressionWrapping, - ) { + @Test + fun `Given that the function signature and a single line body expression body fit on the same line then do not reformat function signature but move the body expression to a separate line`() { val code = """ // $MAX_LINE_LENGTH_MARKER $EOL_CHAR @@ -765,12 +760,35 @@ class FunctionSignatureRuleTest { functionSignatureWrappingRuleAssertThat(code) .setMaxLineLength() .withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset") - .withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to bodyExpressionWrapping) + .withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to always) .addAdditionalRuleProvider { IndentationRule() } .hasLintViolation(2, 33, "Newline expected before expression body") .isFormattedAs(formattedCode) } + @ParameterizedTest(name = "code style: {0}") + @EnumSource(value = CodeStyleValue::class) + fun `Issue 2827 - Given that no max_line_length set, and the function signature is a single line body expression body that does fit on the same line then wrap the body expression to a separate line`( + codeStyleValue: CodeStyleValue, + ) { + val code = + """ + fun f(a: Any, b: Any): String = "some-result" + """.trimIndent() + val formattedCode = + """ + fun f(a: Any, b: Any): String = + "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) + } + @ParameterizedTest(name = "bodyExpressionWrapping: {0}") @EnumSource( value = FunctionBodyExpressionWrapping::class,