Comments (4)
So there's multiple statements here not just one which is why it gets weird when you try to use addStatement
which was designed to emit a single statement.
Traditionally, constructs like if
are meant to be created with beginControlFlow
/nextControlFlow
/endControlFlow
. This automatically emits the correct indentation changes as well as handles the curly braces. It's nothing you can't do yourself, but it is meant to simplify things because KotlinPoet is not a compiler or lexer/parser and tries really hard to know nothing about what's going on in a statement (aside from where to break it when long).
from kotlinpoet.
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.
from kotlinpoet.
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.
from kotlinpoet.
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.
It is part of the public API, see docs for CodeBlock
!
On top of that it would be great if addStatement wrapped at commas instead of wrapping at the column limit.
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.
from kotlinpoet.
Related Issues (20)
- CodeWriter generates wrong nullable type in function body HOT 2
- When using a property initializer in a primary constructor, the annotations don't have the correct use-site target HOT 4
- Chained constructor call is not formatted correctly
- Move Gradle Wrapper validation to its own action
- Can't generate @get:JvmName annotation for property HOT 5
- Can we add IDE highlighting to addStatement or similar statements HOT 4
- kotlin.* imports in generated classes HOT 1
- There is no way to add format strings to the code HOT 2
- Support at least few Kotlin API targets via apiVersion to simplify migrations
- Split the KotlinPoet documentation page into multiple subpages
- addKdoc not working as expected when property is involved HOT 2
- com.squareup:kotlinpoet 1.15.3 missing class files. HOT 3
- callSuperConstructor does not generate code for its parameters HOT 2
- Code generated with ksp interop's toAnnotationSpec() generates code that fails to compile when passed an OptIn annotation HOT 1
- Integration with okio's file system HOT 4
- Aliased imports not working for nested classes and generate unused imports and usage of FQNs HOT 2
- Aliased imports not used when generating code in same package for which the aliased import is defined HOT 1
- Reference Kotlinpoet created class in another class.
- Kotlin Keywords escaped in Class and Property names HOT 6
- Comments outside of code block HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinpoet.