ktlint
ktlint copied to clipboard
Reformat to over-lines code with `max_line_length` limited
Expected Behavior
No need too many lines.
Observed Behavior
Original:
fun fooooooooooooo(key: String, default: Boolean) = Unit
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy { fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true) }
}
Reformay manually:
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true)
}
}
Steps to Reproduce
Reformatd by ktlint:
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
fooooooooooooo(
"fooooooooooooooooooooooooooooooooooooooooooooo",
true,
)
}
}
Your Environment
- Version of ktlint used: 0.47.1
- Relevant parts of the
.editorconfigsettings
root = true
[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.{kt,kts}]
ij_kotlin_imports_layout = *
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
max_line_length = 80
- Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): CLI
- Version of Gradle used (if applicable): None
- Operating System and version: MacOS 12.5.1
I can not reproduce the issue. For me the parameter wrapping is not activated. I have to reduce the max_line_length to 73 before the behavior is triggered. Please note that whenever you change the .editorconfig file with IntelliJ, that you need to save that file explicitly before invoking ktlint. I have been fooled by that many times.
I've tried again on my other Mac, with the same result. Maybe I hadn't described this issue clearly, I meant ktlint formats
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy { fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true) }
}
to
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
fooooooooooooo(
"fooooooooooooooooooooooooooooooooooooooooooooo",
true,
)
}
}
but the
class Bar {
val barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr by lazy {
fooooooooooooo("fooooooooooooooooooooooooooooooooooooooooooooo", true)
}
}
is ok, can pass the lint. If I enlarge max_line_length to 100, will still have the second result.
Ah, now I see what you mean. I consider this as an enhancement on the wrapping rule. If a line exceeds the max_line_length and it contains a BLOCK element type, it should break before start and after end of the block.
~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "lazy"
~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " "
~.psi.KtLambdaArgument (LAMBDA_ARGUMENT)
~.psi.KtLambdaExpression (LAMBDA_EXPRESSION)
~.psi.KtFunctionLiteral (FUNCTION_LITERAL)
~.c.i.p.impl.source.tree.LeafPsiElement (LBRACE) "{"
~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " <--- add line break
~.psi.KtBlockExpression (BLOCK)
...
~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " <--- add line break
~.c.i.p.impl.source.tree.LeafPsiElement (RBRACE) "}"
In addition to this, the wrapping run needs to run before the argument-list-wrapping / function-signature rule.