gmazzo/gradle-buildconfig-plugin

Kotlin output is line wrapping strings

ToxicBakery opened this issue · 12 comments

I assume this happens in Java as well but it appears the plugin is introducing new lines, possibly as some form of automatic line-wrapping on spaces.

Simple example:

val alphabet = (65..90)
    .map { it.toChar() }
    .joinToString()

buildConfigField("String", "Example", "\"$alphabet\"")

Results in:

object BuildConfig {
    const val Example: String = "A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V,
            W, X, Y, Z"
}

Note the newline breaks the string and the comma after V is at index 99 making me believe that something is forcing 100 character wrapping.

This is the culprit https://square.github.io/kotlinpoet/#spaces-wrap-by-default

Changing the previous example

val alphabet = (65..90)
    .map { it.toChar() }
    .joinToString(separator = "")

buildConfigField("String", "Example", "\"$alphabet\"")

now produces the expected result

object BuildConfig {
    const val Example: String =
            "A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z"
}

So not really a bug in the library but something that you may want to elevate in the readme documentation that users must leverage that special character for spaces or ensure they stay below the line wrap limit.

I've changed a few things in the public API and internal to work properly with Gradle 8. Now it uses the "%L" literal of CodeBlock. Can you check if this issue is now solve in version 3.0.2?

I can say that it is not solved in 3.0.3.

Do you know of a simple fix for now?

The simple fix is what I show in my 2nd post, if you string replace spaces with the · character, your output will not wrap other than your explicit \n characters. This is a feature of the underlying library used by this plugin, Kotlin Poet.

This should probably be in the readme though if you haven't already added it. Seems your fix didn't work which admittedly I haven't gone back to try yet as my workaround is still working in our project.

Oh hey @ToxicBakery, thank's a lot! I just didn't get your solution, it now works!

The simple fix is what I show in my 2nd post, if you string replace spaces with the · character, your output will not wrap other than your explicit \n characters. This is a feature of the underlying library used by this plugin, Kotlin Poet.

This should probably be in the readme though if you haven't already added it. Seems your fix didn't work which admittedly I haven't gone back to try yet as my workaround is still working in our project.

Feel free to open a PR for the README.md, you know exactly what we should put on it 😅

Would you also be OK with just adding some .replace(' ', '·') here and here?

I don't think the plugin itself should take over replacement as that will eventually lead to new issues related to enhancing control over that replacement. It's better, imo, to leave it to the users to decide how they want to handle this.

🤷 If you say so. The documentation you proposed to add looks very helpful 👍