-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrapping inside long statements is weird #1755
Comments
So there's multiple statements here not just one which is why it gets weird when you try to use Traditionally, constructs like |
KotlinPoet prioritizes correctness, which we believe is more important in generated code than getting the formatting 100% right (though the library makes a decent effort at producing reasonably formatted code). If formatting is important, we usually recommend post-processing KotlinPoet's output with a formatter tool, such as ktlint. |
The example may be a bit wrong, since I work on proprietary code I have to make stuff up. My problems usually start when I have to call a statement, and within that I have to embed a lot of stuff, like other calls or lambdas. We can't embed that kind of stuff within an addStatement. And simply having too many parameters alone is a reason to avoid addStatement because of its wrapping behavior. But I agree that I prefer to use addStatement/beginControlFlow wherever possible. At our company we have a massive amount of builds, and we have many code generators in the build pipeline (Bazel genrule), and we were asked to not format code as part of the builds because the total resource usage and effect on build time can be big if we add it all up. So we put some effort (not too much) into making the generated code look reasonable enough by default. I think in this case using constructs like ⇥%L⇤ really makes a difference in the output. So I mainly wanted to suggest mentioning it in the official documentation. Because I'm not even sure if it's part of the public API. On top of that it would be great if addStatement wrapped at commas instead of wrapping at the column limit. Other than these two suggestions I accept that this is pretty open ended and there are no big benefits to be had here. |
It is part of the public API, see docs for
I don't think it'll be easy. We don't want to simply always wrap after a comma, don't think it'll be correct in many cases. Indentation is another concern which is not easy to get right. All in all, given that there are tools specializing in formatting Kotlin code, I don't think it's worth investing time into KotlinPoet's built in formatting rules (except for fixing obvious bugs and low-effort high-impact improvements). Completely understand your company's limitations though, it's a tradeoff. |
I frequently have to generate very big function bodies where some individual statements are huge. And it's actually pretty difficult to generate reasonably looking output. The fix for this might be as simple as providing examples in the documentation.
Let me describe the problem first.
Generating code:
It prints:
The first function looks quite horrible, because the wrapping has no idea about semantics, it just breaks in the middle of the deepest level of nesting if it feels like it. My naive attempt at fixing that is the second code block where I do the wrapping by hand, using Kotlin's lovely triple-quoted string. But it still looks bad because of addStatement's behavior. It indents all lines after the first, but in Kotlin the last line of a block should be indented the same as the first line. My third attempt is to use add instead of addStatement and also using the indentation magic characters. The output of that looks very good.
The proper fix for addStatement's bad wrapping would be to count opening and closing braces and parentheses but I understand it could be difficult. So we could just say that addStatement has this limitation and people should live with it and use add instead.
The problem with add is that when I add my own indentation, it's not compatible with KotlinPoet's indentation. But I figured that if I use indentation magic characters around composition points, it works really great. One fix would be to detect the current indentation when embedding a nested CodeBlock into another nested CodeBlock, but again it could be difficult. So instead it could be just added to the documentation how people should deal with the indentation.
The text was updated successfully, but these errors were encountered: