Hey guys, I have been writing some small plugins u...
# ksp
t
Hey guys, I have been writing some small plugins using KSP and generating code with strings, I just wonder if a DSL exists to generate code in an easy way ? something like
Copy code
fun generateCode(): String {
  return function("foo") {
    parameter("a", "Int")
    if (ksClassDeclaration.annotations.includes(something)) {
      parameter("b", "String")
    }
    // ...
  }
}
h
For generating code, you can use kotlinpoet.
very nice 1
➕ 3
e
I'm doing exactly that right now but the repo hasn't been formalized yet 😅 github.com/diskria-libs/poetesse This will work as a declarative code generation and unification of KotlinPoet and JavaPoet with support for type system interop between them. I use this internally in my KSP and it looks like this:
👀 1
👍 1
t
Interesting, I also ended up creating something similar for my internal plugins, here's an example that produces a function call
Here's some code that I used to create some Compose code
e
looks cool! Also, I didn’t even know about
%M
until I saw your screenshot! So thanks for that revelation too haha
🙌 1