Hello, I'm currently working on a plugin to genera...
# squarelibraries
n
Hello, I'm currently working on a plugin to generate ressources for android projets, and I'm using Kotlin Poet to generate .kt files containing compose colors (parsed from a Txt). I have a quick question and didn't want to make an issue for that as it probably not the wanted behavior -> when generating properties to a file, it adds a newline between each property :
Copy code
import androidx.compose.ui.graphics.Colors

public val white: Color = Color(0xFFFFFFFF)

public val black: Color = Color(0xFF000000)
Is there anyway to prevent this ? (or better, control the newlines, to make section ?) I'm using
Copy code
addProperty(
    PropertySpec.builder("white", ClassName("androidx.compose.ui.graphics", "Color"))
        .initializer("Color(0xFFFFFFFF)")
        .build()
)
m
From https://kotlinlang.slack.com/archives/C5HT9AL7Q/p1606874307175700: KotlinPoet prioritizes correctess and explicitness over readability
If you want to tweak the formatting, you might be able to do so with other tools such as ktlint or maybe editing the PSI or something else.
I've been wanting to try this for some time but in the end, dealing with the extra newlines was easier than dealing with PSI 😅
n
lol I agree about that, but it might be easy to remove the lines after generation in my case, it's a good idea
👍 1
And I didn't know ktlint could be use to format, I really need to check on that too ! Thanks !
m
Not sure about ktlint being able to reformat TBH, I just know it was mentioned a couple of times and it knows about syntax so there could be some interesting things there but I'm not sure if anyone has done so successfully yet
"An anti-bikeshedding Kotlin linter with built-in formatter" Looks like it can format
n
btw, I feel dirty but :
Copy code
val writer = StringWriter()
writer.use {
    file.writeTo(it)
}
writer.toString().replace(")\n\n", ")\n")
->
Copy code
import androidx.compose.ui.graphics.Color

public val white: Color = Color(0xFFFFFFFF)
public val white: Color = Color(0xFFFFFFFF)
🙂 1
r
ktlint can format that indeed, it's just a source file as any other in a codebase
if you look on how to use it programmatically - you just include ktlint dependencies like this, and then use the format function from the KtLint object - the ExperimentalParams class has a
text
property, that you can use to pass your generated code
❤️ 1
n
I was looking for a library, thanks !