David Kubecka
05/03/2024, 12:23 PMfunction-signature
rule: Is there a setting that would only format expressions starting on the same line as the function header (to prevent this)
- private fun buildProductNumber(product: ProductDTO) = when (product) {
- is AccountDTO -> buildFullAccountNumber(product)
- is LoanDTO -> buildFullLoanNumber(product)
- }
but leave any other formatting intact if the body already starts on the new line (even if it would fit on a single line)?
- fun List<StatementDefinitionDTO>.toApi() =
- StatementDefinitionsResponse(statementDefinitions = map { it.toApi() })
Paul Dingemans
05/03/2024, 2:19 PMDavid Kubecka
05/03/2024, 2:24 PMmultiline
mode, i.e. force wrapping only when multiline and keep anything else intact - especially do not force single line).Paul Dingemans
05/03/2024, 2:32 PMfun foo(bar: String) = bar
fun foo(bar: String) =
bar
David Kubecka
05/03/2024, 2:33 PMPaul Dingemans
05/03/2024, 2:37 PMDavid Kubecka
05/03/2024, 2:44 PMprivate fun <T, R, EMW, E : ApiError> responseDtoOr(
response: T,
status: GenericStatus<EMW>,
dtoMapper: (T) -> R,
errorBuilder: (List<EMW>) -> E,
): Result<R, E> = if (status.isOk) Ok(dtoMapper(response)) else Err(errorBuilder(status.errors))
Paul Dingemans
05/05/2024, 9:05 AMktlint_function_signature_body_expression_wrapping
for default
and multiline
. As you said, you see value in wrapping multiline expression which means that you best can use multiline
as setting for this configuration.David Kubecka
05/05/2024, 5:16 PMprivate fun <T, R, EMW, E : ApiError> responseDtoOr(
response: T,
status: GenericStatus<EMW>,
dtoMapper: (T) -> R,
errorBuilder: (List<EMW>) -> E,
): Result<R, E> =
if (status.isOk) Ok(dtoMapper(response)) else Err(errorBuilder(status.errors))
Perhaps, in the multiline mode, the single line body rule could trigger only in case the function header also fits on a single line. WDYT?Paul Dingemans
05/08/2024, 7:18 AMprivate fun foo1(foo1: String, foo2: String) = foo1 + foo2
private fun foo2(foo1: String, foo2: String): String = foo1 + foo2
private fun fooooooooooo3(foo1: String, foo2: String) = foo1 + foo2
private fun foo4(foo1: String, foo2: String, foo3: String) = foo1 + foo2 + foo3
private fun foo5(foo1: String, foo2: String, foo3: String): String = foo1 + foo2 + foo3
and .editorconfig
settings:
max_line_length = 66
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 3
the code will be formatted as:
// Signature is allowed on single line as it has less than 3 parameters.
// Expression body and function signature fit on single line
private fun foo1(foo1: String, foo2: String) = foo1 + foo2
// ---new behavior---
// Signature is allowed on single line as it has less than 3 parameters.
// The expression body is wrapped to a new line because the function
// signature has an explicit return type
private fun foo2(foo1: String, foo2: String): String =
foo1 + foo2
// Signature is allowed on single line as it has less than 3 parameters.
// Expression body does not fit on same line as function signature fit
// and will be wrapped to next line
private fun fooooooooooo3(foo1: String, foo2: String) =
foo1 + foo2
// Signature will be forced to multiline as it has too many parameters
// The body expression starts on same line as the function signature
// does not have a return type
private fun foo4(
foo1: String,
foo2: String,
foo3: String,
) = foo1 + foo2 + foo3
// ---new behavior---
// Signature will be forced to multiline as it has too many parameters
// The expression body is wrapped to a new line because the function
// signature has an explicit return type
private fun foo5(
foo1: String,
foo2: String,
foo3: String,
): String =
foo1 + foo2 + foo3
David Kubecka
05/08/2024, 3:37 PMprivate fun foo4(
foo1: String,
foo2: String,
foo3: String,
) =
foo1 + foo2 + foo3
Paul Dingemans
05/08/2024, 4:19 PMDavid Kubecka
05/08/2024, 4:36 PMPaul Dingemans
05/08/2024, 5:50 PM