-
Notifications
You must be signed in to change notification settings - Fork 286
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
kotlinpoet will fail if it returns an expression that is too long #1252
Comments
My current solution is by giving a temporary variable and returning it, but I think this is supposed to be a bug inside kotlinpoet: fun ccc(): LoooooooooooooooooooooooooooooongClass {
val a = LoooooooooooooooooooooooooooooongClass()
return a
} |
KotlinPoet is designed to wrap any space in the statements you pass to it to achieve better formatting, hence the space inside .addStatement("return $myLongClass()") with: .addStatement("return·$myLongClass()") Let us know if that worked so I could close this issue. Thanks! |
Thanks! But I think the framework should avoid such incorrect formatting internally, and I released my library without even being aware of this problem when I didn't test such long expressions, which is risky for the users of my library |
The only way to avoid it would be to disable automatic wrapping entirely. Kotlin is whitespace sensitive in far too many areas and often it is not statically known whether wrapping is safe. |
In kotlin expressions, I think it is actually a rare case that an expression like this cannot be followed by a newline. My thought was whether this problem could be avoided by a special treatment of However, if this is difficult or not convenient, then I will remember to use |
Yeah, we have at least one use case where we special-case We made the decision to wrap spaces by default right before 1.0 (0.x versions did not wrap by default and had a special "wrapping space" modifier), in hindsight this might be more harm than good. |
I have three thoughts here: |
Option 3 is what I'd prefer - let's special-case statements that start with kotlinpoet/kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt Lines 233 to 246 in 9af3f67
Would you be interested in sending us a PR? |
Use %S for string literals. |
When the generated
return someExpression()
expression is too long (over 100), it will cause kotlinpoet to automatically insert a newline and the IDE check will pop up a[RETURN_TYPE_MISMATCH]
error and the compilation will fail.And when I tried to change generate
columnLimit
, I found it very difficult to change it becauseCodeWriter
is an internal class.My SymbolProcessor and FileSpec generation code is as follows, which simply generates a long return expression:
versions:
My test code is here: zsqw123/KotlinPoetIssue
The text was updated successfully, but these errors were encountered: